@@ -21,6 +21,7 @@ local tag_mappings = {
21
21
h5 = { left = " ##### " , right = " \n\n " },
22
22
h6 = { left = " ###### " , right = " \n\n " },
23
23
span = {},
24
+ nav = {},
24
25
header = {},
25
26
div = {},
26
27
section = { right = " \n " },
@@ -82,8 +83,6 @@ local inline_tags = {
82
83
" kbd" ,
83
84
}
84
85
85
- local is_inline_tag = function (tag_name ) return vim .tbl_contains (inline_tags , tag_name ) end
86
-
87
86
local skipable_tags = {
88
87
" input" ,
89
88
@@ -95,7 +94,16 @@ local skipable_tags = {
95
94
" tbody" ,
96
95
}
97
96
97
+ local monospace_tags = {
98
+ " code" ,
99
+ " tt" ,
100
+ " samp" ,
101
+ " kbd" ,
102
+ }
103
+
104
+ local is_inline_tag = function (tag_name ) return vim .tbl_contains (inline_tags , tag_name ) end
98
105
local is_skipable_tag = function (tag_name ) return vim .tbl_contains (skipable_tags , tag_name ) end
106
+ local is_monospace_tag = function (tag_name ) return vim .tbl_contains (monospace_tags , tag_name ) end
99
107
100
108
---- ------------------------------------------------------------
101
109
148
156
149
157
--- @param node TSNode
150
158
function transpiler :get_node_tag_name (node )
159
+ if not node then return " " end
160
+
151
161
local tag_name = nil
152
162
local child = node :named_child ()
153
163
@@ -159,6 +169,19 @@ function transpiler:get_node_tag_name(node)
159
169
return tag_name
160
170
end
161
171
172
+ --- @param node TSNode
173
+ function transpiler :has_parent_tag (node , tag_name )
174
+ local current = node :parent ()
175
+
176
+ while current do
177
+ local parent_tag_name = self :get_node_tag_name (current )
178
+ if parent_tag_name == tag_name then return true end
179
+ current = current :parent ()
180
+ end
181
+
182
+ return false
183
+ end
184
+
162
185
--- @param node TSNode
163
186
function transpiler :get_node_attributes (node )
164
187
if not node then return {} end
@@ -230,6 +253,8 @@ function transpiler:eval(node)
230
253
local tag_node = node :named_child ()
231
254
local tag_type = tag_node :type ()
232
255
local tag_name = self :get_node_tag_name (node )
256
+ local parent_node = node :parent ()
257
+ local parent_tag_name = self :get_node_tag_name (parent_node )
233
258
234
259
if tag_type == " start_tag" then
235
260
local children = self :filter_tag_children (node )
@@ -240,6 +265,7 @@ function transpiler:eval(node)
240
265
end
241
266
242
267
if is_skipable_tag (tag_name ) then return " " end
268
+ if is_monospace_tag (tag_name ) and self :has_parent_tag (node , " pre" ) then return result end
243
269
244
270
if tag_name == " a" then
245
271
result = string.format (" [%s](%s)" , result , attributes .href )
@@ -249,6 +275,9 @@ function transpiler:eval(node)
249
275
result = string.format (" \n " , attributes .alt , attributes .src )
250
276
elseif tag_name == " pre" and attributes [" data-language" ] then
251
277
result = " \n ```" .. attributes [" data-language" ] .. " \n " .. result .. " \n ```\n "
278
+ elseif tag_name == " pre" and attributes [" class" ] then
279
+ local language = attributes [" class" ]:match (" language%-(.+)" )
280
+ result = " \n ```" .. language .. " \n " .. result .. " \n ```\n "
252
281
elseif tag_name == " abbr" then
253
282
result = string.format (" %s(%s)" , result , attributes .title )
254
283
elseif tag_name == " iframe" then
@@ -258,9 +287,6 @@ function transpiler:eval(node)
258
287
elseif tag_name == " table" then
259
288
result = self :eval_table (node ) .. " \n "
260
289
elseif tag_name == " li" then
261
- local parent_node = node :parent ()
262
- local parent_tag_name = self :get_node_tag_name (parent_node )
263
-
264
290
if parent_tag_name == " ul" then result = " - " .. result .. " \n " end
265
291
if parent_tag_name == " ol" then
266
292
local siblings = self :filter_tag_children (parent_node )
@@ -295,7 +321,6 @@ function transpiler:eval_child(node, parent_node)
295
321
local result = self :eval (node )
296
322
local tag_name = self :get_node_tag_name (node )
297
323
local sibling = node :next_named_sibling ()
298
- local parent_tag = self :get_node_tag_name (parent_node )
299
324
local attributes = self :get_node_attributes (parent_node )
300
325
301
326
-- checks if there should be additional spaces/characters between two elements
@@ -305,7 +330,7 @@ function transpiler:eval_child(node, parent_node)
305
330
306
331
-- The <pre> HTML element represents preformatted text
307
332
-- which is to be presented exactly as written in the HTML file
308
- if parent_tag == " pre" or attributes .class == " _rfc-pre" then
333
+ if self : has_parent_tag ( node , " pre" ) or attributes .class == " _rfc-pre" then
309
334
local row , col = c_row_end , c_col_end
310
335
while row ~= s_row_start or col ~= s_col_start do
311
336
local char = self :get_text_range (row , col , row , col + 1 )
0 commit comments