Skip to content

Commit 73f4145

Browse files
authoredJan 13, 2025··
Merge pull request #135 from kinensake/fix-code-block-in-list
Fix render nested code block in list item
2 parents 3d2ff94 + 87f20d3 commit 73f4145

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
 

‎plugin/commonmark/render_list.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/JohannesKaufmann/dom"
1010
"github.com/JohannesKaufmann/html-to-markdown/v2/converter"
1111
"github.com/JohannesKaufmann/html-to-markdown/v2/internal/textutils"
12+
"github.com/JohannesKaufmann/html-to-markdown/v2/marker"
1213
"golang.org/x/net/html"
1314
)
1415

@@ -43,20 +44,27 @@ func (c commonmark) getPrefixFunc(n *html.Node, sliceLength int) func(int) strin
4344

4445
func renderMultiLineListItem(w converter.Writer, content []byte, indentCount int) {
4546
lines := bytes.Split(content, []byte("\n"))
47+
indent := bytes.Repeat([]byte(" "), indentCount)
48+
49+
indentedCodeBlockNewline := append(marker.BytesMarkerCodeBlockNewline, indent...)
4650

4751
for i := range lines {
52+
// Add indent to code block newlines
53+
line := bytes.ReplaceAll(lines[i], marker.BytesMarkerCodeBlockNewline, indentedCodeBlockNewline)
54+
4855
if i != 0 {
4956
// The first line is already indented through the prefix,
5057
// all other lines need the correct amount of spaces.
51-
w.Write(bytes.Repeat([]byte(" "), indentCount))
58+
w.Write(indent)
5259
}
53-
w.Write(lines[i])
60+
w.Write(line)
5461

5562
if i < len(lines)-1 {
5663
w.WriteRune('\n')
5764
}
5865
}
5966
}
67+
6068
func (c commonmark) renderListContainer(ctx converter.Context, w converter.Writer, n *html.Node) converter.RenderStatus {
6169
children := dom.AllChildNodes(n)
6270
items := make([][]byte, 0, len(children))

‎plugin/commonmark/testdata/GoldenFiles/list.in.html

+32
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,38 @@
243243
</ul>
244244

245245

246+
<hr />
247+
248+
249+
<!-- with code block in item -->
250+
<ul>
251+
<li>
252+
item:
253+
<pre>line 1
254+
line 2</pre>
255+
</li>
256+
<li>item 2</li>
257+
</ul>
258+
259+
<!-- with code block in nested item -->
260+
<ul>
261+
<li>
262+
item 1:
263+
<ul>
264+
<li>
265+
nested item 1:
266+
<pre>line 1
267+
line 2</pre>
268+
</li>
269+
<li>nested item 2</li>
270+
</ul>
271+
</li>
272+
<li>item 2</li>
273+
</ul>
274+
275+
276+
<hr />
277+
246278

247279
<!--------------------------------------
248280
Special Characters

‎plugin/commonmark/testdata/GoldenFiles/list.out.md

+29
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,35 @@ text between
171171

172172
End Line
173173

174+
* * *
175+
176+
<!-- with code block in item -->
177+
178+
- item:
179+
180+
```
181+
line 1
182+
line 2
183+
```
184+
- item 2
185+
186+
<!--THE END-->
187+
188+
<!-- with code block in nested item -->
189+
190+
- item 1:
191+
192+
- nested item 1:
193+
194+
```
195+
line 1
196+
line 2
197+
```
198+
- nested item 2
199+
- item 2
200+
201+
* * *
202+
174203
<!--------------------------------------
175204
Special Characters
176205
--------------------------------------->

0 commit comments

Comments
 (0)
Please sign in to comment.