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

Commit 73becaf

Browse files
fix(transpiler): code blocks
Fixed many code blocks related issues like in svelte, deno and zig docs.
1 parent bd282a1 commit 73becaf

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

lua/nvim-devdocs/transpiler.lua

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ local tag_mappings = {
2121
h5 = { left = "##### ", right = "\n\n" },
2222
h6 = { left = "###### ", right = "\n\n" },
2323
span = {},
24+
nav = {},
2425
header = {},
2526
div = {},
2627
section = { right = "\n" },
@@ -82,8 +83,6 @@ local inline_tags = {
8283
"kbd",
8384
}
8485

85-
local is_inline_tag = function(tag_name) return vim.tbl_contains(inline_tags, tag_name) end
86-
8786
local skipable_tags = {
8887
"input",
8988

@@ -95,7 +94,16 @@ local skipable_tags = {
9594
"tbody",
9695
}
9796

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
98105
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
99107

100108
----------------------------------------------------------------
101109

@@ -148,6 +156,8 @@ end
148156

149157
---@param node TSNode
150158
function transpiler:get_node_tag_name(node)
159+
if not node then return "" end
160+
151161
local tag_name = nil
152162
local child = node:named_child()
153163

@@ -159,6 +169,19 @@ function transpiler:get_node_tag_name(node)
159169
return tag_name
160170
end
161171

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+
162185
---@param node TSNode
163186
function transpiler:get_node_attributes(node)
164187
if not node then return {} end
@@ -230,6 +253,8 @@ function transpiler:eval(node)
230253
local tag_node = node:named_child()
231254
local tag_type = tag_node:type()
232255
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)
233258

234259
if tag_type == "start_tag" then
235260
local children = self:filter_tag_children(node)
@@ -240,6 +265,7 @@ function transpiler:eval(node)
240265
end
241266

242267
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
243269

244270
if tag_name == "a" then
245271
result = string.format("[%s](%s)", result, attributes.href)
@@ -249,6 +275,9 @@ function transpiler:eval(node)
249275
result = string.format("![%s](%s)\n", attributes.alt, attributes.src)
250276
elseif tag_name == "pre" and attributes["data-language"] then
251277
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"
252281
elseif tag_name == "abbr" then
253282
result = string.format("%s(%s)", result, attributes.title)
254283
elseif tag_name == "iframe" then
@@ -258,9 +287,6 @@ function transpiler:eval(node)
258287
elseif tag_name == "table" then
259288
result = self:eval_table(node) .. "\n"
260289
elseif tag_name == "li" then
261-
local parent_node = node:parent()
262-
local parent_tag_name = self:get_node_tag_name(parent_node)
263-
264290
if parent_tag_name == "ul" then result = "- " .. result .. "\n" end
265291
if parent_tag_name == "ol" then
266292
local siblings = self:filter_tag_children(parent_node)
@@ -295,7 +321,6 @@ function transpiler:eval_child(node, parent_node)
295321
local result = self:eval(node)
296322
local tag_name = self:get_node_tag_name(node)
297323
local sibling = node:next_named_sibling()
298-
local parent_tag = self:get_node_tag_name(parent_node)
299324
local attributes = self:get_node_attributes(parent_node)
300325

301326
-- checks if there should be additional spaces/characters between two elements
@@ -305,7 +330,7 @@ function transpiler:eval_child(node, parent_node)
305330

306331
-- The <pre> HTML element represents preformatted text
307332
-- 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
309334
local row, col = c_row_end, c_col_end
310335
while row ~= s_row_start or col ~= s_col_start do
311336
local char = self:get_text_range(row, col, row, col + 1)

0 commit comments

Comments
 (0)