Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit f67314f

Browse files
fix(transpiler): spacing between elements
correctly handle spacing between HTML elements in markdown
1 parent 81c910c commit f67314f

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

lua/nvim-devdocs/transpiler.lua

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ local normalize_html = function(str)
1313
return str
1414
end
1515

16+
local is_inline_tag = function(tag_name)
17+
-- stylua: ignore
18+
local inline_tags = {
19+
"span", "a", "strong", "em", "abbr", "code", "i",
20+
"s", "sub", "sup", "mark", "small", "var", "kbd",
21+
}
22+
23+
return vim.tbl_contains(inline_tags, tag_name)
24+
end
25+
1626
local tag_mappings = {
1727
h1 = { left = "# ", right = "\n\n" },
1828
h2 = { left = "## ", right = "\n\n" },
@@ -36,10 +46,13 @@ local tag_mappings = {
3646
samp = { left = "`", right = "`" },
3747
var = { left = "`", right = "`" },
3848
kbd = { left = "`", right = "`" },
49+
mark = { left = "`", right = "`" },
3950
b = { left = "`", right = "`" },
4051
strong = { left = "**", right = "**" },
41-
em = { left = " _", right = "_ " },
42-
small = { left = " _", right = "_ " },
52+
i = { left = "_", right = "_" },
53+
s = { left = "~~", right = "~~" },
54+
em = { left = "_", right = "_" },
55+
small = { left = "_", right = "_" },
4356
sup = { left = "^", right = "^" },
4457
blockquote = { left = "> " },
4558
summary = { left = "<", right = ">" },
@@ -126,8 +139,13 @@ M.html_to_md = function(html)
126139

127140
---@param node TSNode
128141
function transpiler:get_node_tag_name(node)
129-
local tag_node = node:named_child():named_child()
130-
local tag_name = self:get_node_text(tag_node)
142+
local tag_name = nil
143+
local child = node:named_child()
144+
145+
if child then
146+
local tag_node = child:named_child()
147+
tag_name = self:get_node_text(tag_node)
148+
end
131149

132150
return tag_name
133151
end
@@ -189,11 +207,7 @@ M.html_to_md = function(html)
189207
local children = self:filter_tag_children(node)
190208

191209
for _, child in ipairs(children) do
192-
if tag_name == "pre" then
193-
result = result .. self:eval_pre_child(child)
194-
else
195-
result = result .. self:eval(child)
196-
end
210+
result = result .. self:eval_child(child, tag_name)
197211
end
198212
end
199213

@@ -236,30 +250,35 @@ M.html_to_md = function(html)
236250
end
237251
end
238252

239-
result = result:gsub("\n\n\n+", "\n\n")
240-
241253
return result
242254
end
243255

244256
---@param node TSNode
245-
function transpiler:eval_pre_child(node)
257+
function transpiler:eval_child(node, parent_tag)
246258
local result = self:eval(node)
259+
local tag_name = self:get_node_tag_name(node)
247260
local sibling = node:next_named_sibling()
248261

262+
-- check if there should be additional spaces/characters between two elements
249263
if sibling then
250264
local c_row_end, c_col_end = node:end_()
251265
local s_row_start, s_col_start = sibling:start()
252-
local row, col = c_row_end, c_col_end
253266

254-
while row ~= s_row_start or col ~= s_col_start do
255-
local char = self:get_text_range(row, col, row, col + 1)
256-
if char ~= "" then
257-
result = result .. char
258-
col = col + 1
259-
else
260-
result = result .. "\n"
261-
row, col = row + 1, 0
267+
if parent_tag == "pre" then
268+
local row, col = c_row_end, c_col_end
269+
while row ~= s_row_start or col ~= s_col_start do
270+
local char = self:get_text_range(row, col, row, col + 1)
271+
if char ~= "" then
272+
result = result .. char
273+
col = col + 1
274+
else
275+
result = result .. "\n"
276+
row, col = row + 1, 0
277+
end
262278
end
279+
else
280+
local is_inline = is_inline_tag(tag_name) or not tag_name -- is text
281+
if is_inline and c_col_end ~= s_col_start then result = result .. " " end
263282
end
264283
end
265284

@@ -364,6 +383,8 @@ M.html_to_md = function(html)
364383
end
365384
end)
366385

386+
self.result = self.result:gsub("\n\n\n+", "\n\n")
387+
367388
return self.result
368389
end
369390

0 commit comments

Comments
 (0)