@@ -7,24 +7,34 @@ local str = require('render-markdown.str')
7
7
local ts = require (' render-markdown.ts' )
8
8
local util = require (' render-markdown.util' )
9
9
10
- --- @class render.md.handler.MarkdownInline : render.md.Handler
11
- local M = {}
10
+ --- @class render.md.handler.buf.MarkdownInline
11
+ --- @field private buf integer
12
+ --- @field private config render.md.BufferConfig
13
+ local Handler = {}
14
+ Handler .__index = Handler
12
15
13
- --- @param root TSNode
14
16
--- @param buf integer
17
+ --- @return render.md.handler.buf.MarkdownInline
18
+ function Handler .new (buf )
19
+ local self = setmetatable ({}, Handler )
20
+ self .buf = buf
21
+ self .config = state .get_config (buf )
22
+ return self
23
+ end
24
+
25
+ --- @param root TSNode
15
26
--- @return render.md.Mark[]
16
- function M .parse (root , buf )
17
- local config = state .get_config (buf )
27
+ function Handler :parse (root )
18
28
local marks = {}
19
- context .get (buf ):query (root , state .inline_query , function (capture , node )
20
- local info = ts .info (node , buf )
29
+ context .get (self . buf ):query (root , state .inline_query , function (capture , node )
30
+ local info = ts .info (node , self . buf )
21
31
logger .debug_node_info (capture , info )
22
32
if capture == ' code' then
23
- list .add_mark (marks , M . code (config , info ))
33
+ list .add_mark (marks , self : code (info ))
24
34
elseif capture == ' shortcut' then
25
- list .add_mark (marks , M . shortcut (config , buf , info ))
35
+ list .add_mark (marks , self : shortcut (info ))
26
36
elseif capture == ' link' then
27
- list .add_mark (marks , M . link (config , buf , info ))
37
+ list .add_mark (marks , self : link (info ))
28
38
else
29
39
logger .unhandled_capture (' inline' , capture )
30
40
end
@@ -33,11 +43,10 @@ function M.parse(root, buf)
33
43
end
34
44
35
45
--- @private
36
- --- @param config render.md.BufferConfig
37
46
--- @param info render.md.NodeInfo
38
47
--- @return render.md.Mark ?
39
- function M . code (config , info )
40
- local code = config .code
48
+ function Handler : code (info )
49
+ local code = self . config .code
41
50
if not code .enabled then
42
51
return nil
43
52
end
@@ -58,37 +67,33 @@ function M.code(config, info)
58
67
end
59
68
60
69
--- @private
61
- --- @param config render.md.BufferConfig
62
- --- @param buf integer
63
70
--- @param info render.md.NodeInfo
64
71
--- @return render.md.Mark ?
65
- function M . shortcut (config , buf , info )
66
- local callout = component .callout (config , info .text , ' exact' )
72
+ function Handler : shortcut (info )
73
+ local callout = component .callout (self . config , info .text , ' exact' )
67
74
if callout ~= nil then
68
- return M . callout (config , buf , info , callout )
75
+ return self : callout (info , callout )
69
76
end
70
- local checkbox = component .checkbox (config , info .text , ' exact' )
77
+ local checkbox = component .checkbox (self . config , info .text , ' exact' )
71
78
if checkbox ~= nil then
72
- return M . checkbox (config , info , checkbox )
79
+ return self : checkbox (info , checkbox )
73
80
end
74
- local line = vim .api .nvim_buf_get_lines (buf , info .start_row , info .start_row + 1 , false )[1 ]
81
+ local line = vim .api .nvim_buf_get_lines (self . buf , info .start_row , info .start_row + 1 , false )[1 ]
75
82
if line :find (' [' .. info .text .. ' ]' , 1 , true ) ~= nil then
76
- return M . wiki_link (config , info )
83
+ return self : wiki_link (info )
77
84
end
78
85
return nil
79
86
end
80
87
81
88
--- @private
82
- --- @param config render.md.BufferConfig
83
- --- @param buf integer
84
89
--- @param info render.md.NodeInfo
85
90
--- @param callout render.md.CustomComponent
86
91
--- @return render.md.Mark ?
87
- function M . callout (config , buf , info , callout )
92
+ function Handler : callout (info , callout )
88
93
--- Support for overriding title: https://help.obsidian.md/Editing+and+formatting/Callouts#Change+the+title
89
94
--- @return string , string ?
90
95
local function custom_title ()
91
- local content = ts .parent (buf , info , ' inline' )
96
+ local content = ts .parent (self . buf , info , ' inline' )
92
97
if content ~= nil then
93
98
local line = str .split (content .text , ' \n ' )[1 ]
94
99
if # line > # callout .raw and vim .startswith (line :lower (), callout .raw :lower ()) then
@@ -100,7 +105,7 @@ function M.callout(config, buf, info, callout)
100
105
return callout .rendered , nil
101
106
end
102
107
103
- if not config .quote .enabled then
108
+ if not self . config .quote .enabled then
104
109
return nil
105
110
end
106
111
local text , conceal = custom_title ()
@@ -120,16 +125,12 @@ function M.callout(config, buf, info, callout)
120
125
end
121
126
122
127
--- @private
123
- --- @param config render.md.BufferConfig
124
128
--- @param info render.md.NodeInfo
125
129
--- @param checkbox render.md.CustomComponent
126
130
--- @return render.md.Mark ?
127
- function M .checkbox (config , info , checkbox )
128
- if not config .checkbox .enabled then
129
- return nil
130
- end
131
+ function Handler :checkbox (info , checkbox )
131
132
-- Requires inline extmarks
132
- if not util .has_10 then
133
+ if not self . config . checkbox . enabled or not util .has_10 then
133
134
return nil
134
135
end
135
136
--- @type render.md.Mark
@@ -148,20 +149,16 @@ function M.checkbox(config, info, checkbox)
148
149
end
149
150
150
151
--- @private
151
- --- @param config render.md.BufferConfig
152
152
--- @param info render.md.NodeInfo
153
153
--- @return render.md.Mark ?
154
- function M .wiki_link (config , info )
155
- local link = config .link
156
- if not link .enabled then
157
- return nil
158
- end
154
+ function Handler :wiki_link (info )
159
155
-- Requires inline extmarks
160
- if not util .has_10 then
156
+ if not self . config . link . enabled or not util .has_10 then
161
157
return nil
162
158
end
163
159
local text = info .text :sub (2 , - 2 )
164
- local elements = str .split (text , ' |' )
160
+ local parts = str .split (text , ' |' )
161
+ local icon , highlight = self :destination_info (parts [1 ])
165
162
--- @type render.md.Mark
166
163
return {
167
164
conceal = true ,
@@ -170,37 +167,38 @@ function M.wiki_link(config, info)
170
167
opts = {
171
168
end_row = info .end_row ,
172
169
end_col = info .end_col + 1 ,
173
- virt_text = { { link . hyperlink .. elements [ # elements ], link . highlight } },
170
+ virt_text = { { icon .. parts [ # parts ], highlight } },
174
171
virt_text_pos = ' inline' ,
175
172
conceal = ' ' ,
176
173
},
177
174
}
178
175
end
179
176
180
177
--- @private
181
- --- @param config render.md.BufferConfig
182
- --- @param buf integer
183
178
--- @param info render.md.NodeInfo
184
179
--- @return render.md.Mark ?
185
- function M .link (config , buf , info )
186
- local link = config .link
187
- if not link .enabled then
188
- return nil
189
- end
180
+ function Handler :link (info )
181
+ local link = self .config .link
190
182
-- Requires inline extmarks
191
- if not util .has_10 then
183
+ if not link . enabled or not util .has_10 then
192
184
return nil
193
185
end
194
- local icon = nil
195
- if vim .tbl_contains ({ ' inline_link' , ' full_reference_link' }, info .type ) then
196
- icon = link .hyperlink
186
+ local icon , highlight
187
+ if info .type == ' inline_link' then
188
+ local destination = ts .child (self .buf , info , ' link_destination' )
189
+ if destination ~= nil then
190
+ icon , highlight = self :destination_info (destination .text )
191
+ else
192
+ icon , highlight = link .hyperlink , link .highlight
193
+ end
194
+ elseif info .type == ' full_reference_link' then
195
+ icon , highlight = link .hyperlink , link .highlight
197
196
elseif info .type == ' image' then
198
- icon = link .image
199
- end
200
- if icon == nil then
197
+ icon , highlight = link .image , link .highlight
198
+ else
201
199
return nil
202
200
end
203
- context .get (buf ):add_link (info , icon )
201
+ context .get (self . buf ):add_link (info , icon )
204
202
--- @type render.md.Mark
205
203
return {
206
204
conceal = true ,
@@ -209,10 +207,32 @@ function M.link(config, buf, info)
209
207
opts = {
210
208
end_row = info .end_row ,
211
209
end_col = info .end_col ,
212
- virt_text = { { icon , link . highlight } },
210
+ virt_text = { { icon , highlight } },
213
211
virt_text_pos = ' inline' ,
214
212
},
215
213
}
216
214
end
217
215
216
+ --- @private
217
+ --- @param destination string
218
+ --- @return string , string
219
+ function Handler :destination_info (destination )
220
+ local link_component = component .link (self .config , destination )
221
+ if link_component == nil then
222
+ return self .config .link .hyperlink , self .config .link .highlight
223
+ else
224
+ return link_component .icon , link_component .highlight
225
+ end
226
+ end
227
+
228
+ --- @class render.md.handler.MarkdownInline : render.md.Handler
229
+ local M = {}
230
+
231
+ --- @param root TSNode
232
+ --- @param buf integer
233
+ --- @return render.md.Mark[]
234
+ function M .parse (root , buf )
235
+ return Handler .new (buf ):parse (root )
236
+ end
237
+
218
238
return M
0 commit comments