mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
Add TODO list support for Blackfriday
* Add CSS class to TODO list and list items * Add a flag to turn task list support off Fixes #2269
This commit is contained in:
committed by
GitHub
parent
76bf2dcdd2
commit
eaf2f9bce5
@@ -51,6 +51,7 @@ type Blackfriday struct {
|
||||
HrefTargetBlank bool
|
||||
SmartDashes bool
|
||||
LatexDashes bool
|
||||
TaskLists bool
|
||||
PlainIDAnchors bool
|
||||
SourceRelativeLinksEval bool
|
||||
SourceRelativeLinksProjectFolder string
|
||||
@@ -68,6 +69,7 @@ func NewBlackfriday(c ConfigProvider) *Blackfriday {
|
||||
"smartDashes": true,
|
||||
"latexDashes": true,
|
||||
"plainIDAnchors": true,
|
||||
"taskLists": true,
|
||||
"sourceRelativeLinks": false,
|
||||
"sourceRelativeLinksProjectFolder": "/docs/content",
|
||||
}
|
||||
|
@@ -72,6 +72,44 @@ func (renderer *HugoHTMLRenderer) Image(out *bytes.Buffer, link []byte, title []
|
||||
}
|
||||
}
|
||||
|
||||
// ListItem adds task list support to the Blackfriday renderer.
|
||||
func (renderer *HugoHTMLRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
||||
if !renderer.Config.TaskLists {
|
||||
renderer.Renderer.ListItem(out, text, flags)
|
||||
return
|
||||
}
|
||||
|
||||
switch {
|
||||
case bytes.HasPrefix(text, []byte("[ ] ")):
|
||||
text = append([]byte(`<input type="checkbox" disabled class="task-list-item">`), text[3:]...)
|
||||
|
||||
case bytes.HasPrefix(text, []byte("[x] ")) || bytes.HasPrefix(text, []byte("[X] ")):
|
||||
text = append([]byte(`<input type="checkbox" checked disabled class="task-list-item">`), text[3:]...)
|
||||
}
|
||||
|
||||
renderer.Renderer.ListItem(out, text, flags)
|
||||
}
|
||||
|
||||
// List adds task list support to the Blackfriday renderer.
|
||||
func (renderer *HugoHTMLRenderer) List(out *bytes.Buffer, text func() bool, flags int) {
|
||||
if !renderer.Config.TaskLists {
|
||||
renderer.Renderer.List(out, text, flags)
|
||||
return
|
||||
}
|
||||
marker := out.Len()
|
||||
renderer.Renderer.List(out, text, flags)
|
||||
if out.Len() > marker {
|
||||
list := out.Bytes()[marker:]
|
||||
if bytes.Contains(list, []byte("task-list-item")) {
|
||||
// Rewrite the buffer from the marker
|
||||
out.Truncate(marker)
|
||||
// May be either dl, ul or ol
|
||||
list := append(list[:4], append([]byte(` class="task-list"`), list[4:]...)...)
|
||||
out.Write(list)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HugoMmarkHTMLRenderer wraps a mmark.Renderer, typically a mmark.html
|
||||
// Enabling Hugo to customise the rendering experience
|
||||
type HugoMmarkHTMLRenderer struct {
|
||||
|
@@ -88,3 +88,44 @@ func TestCodeFence(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlackfridayTaskList(t *testing.T) {
|
||||
for i, this := range []struct {
|
||||
markdown string
|
||||
taskListEnabled bool
|
||||
expect string
|
||||
}{
|
||||
{`
|
||||
TODO:
|
||||
|
||||
- [x] On1
|
||||
- [X] On2
|
||||
- [ ] Off
|
||||
|
||||
END
|
||||
`, true, `<p>TODO:</p>
|
||||
|
||||
<ul class="task-list">
|
||||
<li><input type="checkbox" checked disabled class="task-list-item"> On1</li>
|
||||
<li><input type="checkbox" checked disabled class="task-list-item"> On2</li>
|
||||
<li><input type="checkbox" disabled class="task-list-item"> Off</li>
|
||||
</ul>
|
||||
|
||||
<p>END</p>
|
||||
`},
|
||||
{`- [x] On1`, false, `<ul>
|
||||
<li>[x] On1</li>
|
||||
</ul>
|
||||
`},
|
||||
} {
|
||||
blackFridayConfig := NewBlackfriday(viper.GetViper())
|
||||
blackFridayConfig.TaskLists = this.taskListEnabled
|
||||
ctx := &RenderingContext{Content: []byte(this.markdown), PageFmt: "markdown", Config: blackFridayConfig}
|
||||
|
||||
result := string(RenderBytes(ctx))
|
||||
|
||||
if result != this.expect {
|
||||
t.Errorf("[%d] got \n%v but expected \n%v", i, result, this.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user