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

Commit 81c910c

Browse files
fix: #3
evaluate <pre> child separately
1 parent 87be468 commit 81c910c

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

lua/nvim-devdocs/transpiler.lua

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ M.html_to_md = function(html)
9595
result = "",
9696
}
9797

98-
---@param node TSNode
99-
function transpiler:get_node_text(node)
100-
local row_start, col_start = node:start()
101-
local row_end, col_end = node:end_()
98+
function transpiler:get_text_range(row_start, col_start, row_end, col_end)
10299
local extracted_lines = {}
103100

104101
for i = row_start, row_end do
@@ -118,6 +115,15 @@ M.html_to_md = function(html)
118115
return table.concat(extracted_lines, "\n")
119116
end
120117

118+
---@param node TSNode
119+
function transpiler:get_node_text(node)
120+
local row_start, col_start = node:start()
121+
local row_end, col_end = node:end_()
122+
local text = self:get_text_range(row_start, col_start, row_end, col_end)
123+
124+
return text
125+
end
126+
121127
---@param node TSNode
122128
function transpiler:get_node_tag_name(node)
123129
local tag_node = node:named_child():named_child()
@@ -154,6 +160,7 @@ M.html_to_md = function(html)
154160
end
155161

156162
---@param node TSNode
163+
---@return TSNode[]
157164
function transpiler:filter_tag_children(node)
158165
local children = node:named_children()
159166
local filtered = vim.tbl_filter(function(child)
@@ -180,8 +187,13 @@ M.html_to_md = function(html)
180187

181188
if tag_type == "start_tag" then
182189
local children = self:filter_tag_children(node)
190+
183191
for _, child in ipairs(children) do
184-
result = result .. self:eval(child)
192+
if tag_name == "pre" then
193+
result = result .. self:eval_pre_child(child)
194+
else
195+
result = result .. self:eval(child)
196+
end
185197
end
186198
end
187199

@@ -229,6 +241,31 @@ M.html_to_md = function(html)
229241
return result
230242
end
231243

244+
---@param node TSNode
245+
function transpiler:eval_pre_child(node)
246+
local result = self:eval(node)
247+
local sibling = node:next_named_sibling()
248+
249+
if sibling then
250+
local c_row_end, c_col_end = node:end_()
251+
local s_row_start, s_col_start = sibling:start()
252+
local row, col = c_row_end, c_col_end
253+
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
262+
end
263+
end
264+
end
265+
266+
return result
267+
end
268+
232269
---@param node TSNode
233270
function transpiler:eval_table(node)
234271
local result = ""

0 commit comments

Comments
 (0)