Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix task list checkbox toggle to work with YAML front matter #25184

Merged
merged 7 commits into from
Jun 13, 2023
Merged
4 changes: 3 additions & 1 deletion modules/markup/markdown/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func IsSummary(node ast.Node) bool {
// TaskCheckBoxListItem is a block that represents a list item of a markdown block with a checkbox
type TaskCheckBoxListItem struct {
*ast.ListItem
IsChecked bool
IsChecked bool
SourcePosition int
}

// KindTaskCheckBoxListItem is the NodeKind for TaskCheckBoxListItem
Expand All @@ -86,6 +87,7 @@ var KindTaskCheckBoxListItem = ast.NewNodeKind("TaskCheckBoxListItem")
func (n *TaskCheckBoxListItem) Dump(source []byte, level int) {
m := map[string]string{}
m["IsChecked"] = strconv.FormatBool(n.IsChecked)
m["SourcePosition"] = strconv.FormatInt(int64(n.SourcePosition), 10)
ast.DumpHelper(n, source, level, m, nil)
}

Expand Down
12 changes: 6 additions & 6 deletions modules/markup/markdown/goldmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
newChild := NewTaskCheckBoxListItem(listItem)
newChild.IsChecked = taskCheckBox.IsChecked
newChild.SetAttributeString("class", []byte("task-list-item"))
segments := newChild.FirstChild().Lines()
if segments.Len() > 0 {
segment := segments.At(0)
newChild.SourcePosition = rc.MetaLength + segment.Start
}
v.AppendChild(v, newChild)
}
}
Expand Down Expand Up @@ -457,12 +462,7 @@ func (r *HTMLRenderer) renderTaskCheckBoxListItem(w util.BufWriter, source []byt
} else {
_, _ = w.WriteString("<li>")
}
_, _ = w.WriteString(`<input type="checkbox" disabled=""`)
segments := node.FirstChild().Lines()
if segments.Len() > 0 {
segment := segments.At(0)
_, _ = w.WriteString(fmt.Sprintf(` data-source-position="%d"`, segment.Start))
}
_, _ = w.WriteString(fmt.Sprintf(`<input type="checkbox" disabled="" data-source-position="%d"`, n.SourcePosition))
jtran marked this conversation as resolved.
Show resolved Hide resolved
if n.IsChecked {
_, _ = w.WriteString(` checked=""`)
}
Expand Down
9 changes: 9 additions & 0 deletions modules/markup/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,22 @@ func actualRender(ctx *markup.RenderContext, input io.Reader, output io.Writer)
}
buf = giteautil.NormalizeEOL(buf)

// Preserve original length.
bufWithMetadataLength := len(buf)

rc := &RenderConfig{
Meta: renderMetaModeFromString(string(ctx.RenderMetaAs)),
Icon: "table",
Lang: "",
}
buf, _ = ExtractMetadataBytes(buf, rc)

metaLength := bufWithMetadataLength - len(buf)
if metaLength < 0 {
metaLength = 0
}
rc.MetaLength = metaLength

pc.Set(renderConfigKey, rc)

if err := converter.Convert(buf, lw, parser.WithContext(pc)); err != nil {
Expand Down
37 changes: 37 additions & 0 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,40 @@ func TestMathBlock(t *testing.T) {

}
}

func TestTaskList(t *testing.T) {
testcases := []struct {
testcase string
expected string
}{
{
// data-source-position should take into account YAML frontmatter.
`---
foo: bar
---
- [ ] task 1`,
`<details><summary><i class="icon table"></i></summary><table>
<thead>
Comment on lines +531 to +536
Copy link
Contributor

@wxiaoguang wxiaoguang Jun 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there could be a include_toc: false in the meta section, to disable the <details><summary> part, then the output could be much simpler and clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean add include_toc: false to the YAML? I can't get this to affect the details/summary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. IIRC the render should work that way. But it is not a must. 😄 maybe there are some other tricks behind it.

<tr>
<th>foo</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
</tr>
</tbody>
</table>
</details><ul>
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="19"/>task 1</li>
</ul>
`,
},
}

for _, test := range testcases {
res, err := RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, test.testcase)
assert.NoError(t, err, "Unexpected error in testcase: %q", test.testcase)
assert.Equal(t, test.expected, res, "Unexpected result in testcase %q", test.testcase)
}
}
3 changes: 3 additions & 0 deletions modules/markup/markdown/renderconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type RenderConfig struct {
TOC string // "false": hide, "side"/empty: in sidebar, "main"/"true": in main view
Lang string
yamlNode *yaml.Node

// Used internally. Cannot be controlled by frontmatter.
MetaLength int
jtran marked this conversation as resolved.
Show resolved Hide resolved
}

func renderMetaModeFromString(s string) markup.RenderMetaMode {
Expand Down