Skip to content

Commit 481924b

Browse files
mpcabdbep
authored andcommitted
helpers: Fix broken TaskList in Markdown
As per the referenced issue, if the task list in Markdown has nothing before it, it will be rendered wrongly: ``` --- title: "My First Post" date: 2017-07-29T20:21:57+02:00 draft: true --- * [ ] TaskList ``` is rendered as: ``` <ul> class="task-list" <li><input type="checkbox" disabled class="task-list-item"> TaskList</li> </ul> ``` The problem lies in the `List` function of `HugoHTMLRenderer`, it had a hardocded index of `4` for the first `>` of the list, it is used to insert the class into the text before the closing bracket, but that hardcoded index is only right when there is a newline before the opening bracket, which is the case when there is anything in the document before the task list, but if there is nothing, then there is no newline, and the correct index of the first `>` will be `3`. To fix that we're changing the hardcoded index to be dynamic by using `bytes.Index` to find it properly. We're also adding a test case to make sure this is tested against. Fixes #3710
1 parent 09907d3 commit 481924b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

‎helpers/content_renderer.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ func (r *HugoHTMLRenderer) List(out *bytes.Buffer, text func() bool, flags int)
101101
if out.Len() > marker {
102102
list := out.Bytes()[marker:]
103103
if bytes.Contains(list, []byte("task-list-item")) {
104+
// Find the index of the first >, it might be 3 or 4 depending on whether
105+
// there is a new line at the start, but this is safer than just hardcoding it.
106+
closingBracketIndex := bytes.Index(list, []byte(">"))
104107
// Rewrite the buffer from the marker
105108
out.Truncate(marker)
109+
// Safely assuming closingBracketIndex won't be -1 since there is a list
106110
// May be either dl, ul or ol
107-
list := append(list[:4], append([]byte(` class="task-list"`), list[4:]...)...)
111+
list := append(list[:closingBracketIndex], append([]byte(` class="task-list"`), list[closingBracketIndex:]...)...)
108112
out.Write(list)
109113
}
110114
}

‎helpers/content_renderer_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ END
118118
{`- [x] On1`, false, `<ul>
119119
<li>[x] On1</li>
120120
</ul>
121+
`},
122+
{`* [ ] Off
123+
124+
END`, true, `<ul class="task-list">
125+
<li><input type="checkbox" disabled class="task-list-item"> Off</li>
126+
</ul>
127+
128+
<p>END</p>
121129
`},
122130
} {
123131
blackFridayConfig := c.NewBlackfriday()

0 commit comments

Comments
 (0)