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

Commit 7414d20

Browse files
fix(transpiler): handle nested tags inside <pre> (#56)
Fixed #56 and added some test. Only consider the actual text offset and skip all the tags inside <pre> tags to solve the nested tag issue. This allows svelte code blocks to be correctly converted.
1 parent a9ece8d commit 7414d20

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

lua/nvim-devdocs/transpiler.lua

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ local tag_mappings = {
2424
span = {},
2525
nav = {},
2626
header = {},
27-
div = {},
27+
div = { left = "\n", right = "\n" },
2828
section = { right = "\n" },
2929
p = { right = "\n\n" },
3030
ul = { right = "\n" },
@@ -86,6 +86,9 @@ local inline_tags = {
8686

8787
local skipable_tags = {
8888
"input",
89+
"use",
90+
"svg",
91+
"button",
8992

9093
-- exceptions, (parent) table -> child
9194
"tr",
@@ -336,14 +339,36 @@ function transpiler:eval_child(node, parent_node)
336339

337340
-- checks if there should be additional spaces/characters between two elements
338341
if sibling then
339-
local c_row_end, c_col_end = node:end_()
340-
local s_row_start, s_col_start = sibling:start()
342+
local start_row, start_col = node:end_()
343+
local target_row, target_col = sibling:start()
341344

342345
-- The <pre> HTML element represents preformatted text
343346
-- which is to be presented exactly as written in the HTML file
344347
if self:has_parent_tag(node, "pre") or attributes.class == "_rfc-pre" then
345-
local row, col = c_row_end, c_col_end
346-
while row ~= s_row_start or col ~= s_col_start do
348+
local child = sibling:named_child()
349+
350+
-- skip all the tags to get the actual text offset, see #56
351+
while child do
352+
local child_sibling = child:next_named_sibling()
353+
if child:type() == "start_tag" and child_sibling then
354+
local c_row, c_col = child:end_()
355+
local s_row, s_col = child_sibling:start()
356+
357+
if c_row == s_row and c_col == s_col then
358+
child = child_sibling:named_child()
359+
else
360+
start_row, start_col = c_row, c_col
361+
target_row, target_col = s_row, s_col
362+
363+
if child_sibling:type() == "text" or "entity" then break end
364+
end
365+
else
366+
break
367+
end
368+
end
369+
370+
local row, col = start_row, start_col
371+
while row ~= target_row or col ~= target_col do
347372
local char = self:get_text_range(row, col, row, col + 1)
348373
if char ~= "" then
349374
result = result .. char
@@ -355,7 +380,7 @@ function transpiler:eval_child(node, parent_node)
355380
end
356381
else
357382
local is_inline = is_inline_tag(tag_name) or not tag_name -- is text
358-
if is_inline and c_col_end ~= s_col_start then result = result .. " " end
383+
if is_inline and start_col ~= target_col then result = result .. " " end
359384
end
360385
end
361386

test/transpiler_spec.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ console.log("Hello World")
6262
padding: 0;
6363
}
6464
```
65+
]],
66+
},
67+
{
68+
desc = "<pre> with tag children",
69+
input =
70+
[[<pre class="language-svelte"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
71+
<span class="token keyword">import</span> <span class="token punctuation">{</span> getAllContexts <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'svelte'</span><span class="token punctuation">;</span>
72+
73+
<span class="token keyword">const</span> contexts <span class="token operator">=</span> <span class="token function">getAllContexts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
74+
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span></code><button type="button" class="_pre-clip" title="Copy to clipboard" aria-label="Copy to clipboard"><svg><use xlink:href="#icon-copy"></use></svg></button></pre>]],
75+
expected = [[
76+
77+
```svelte
78+
<script>
79+
import { getAllContexts } from 'svelte';
80+
81+
const contexts = getAllContexts();
82+
</script>
83+
```
6584
]],
6685
},
6786
{

0 commit comments

Comments
 (0)