From 352d9555bae4377b14fd725d341531e694a3b07a Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Tue, 29 Jul 2025 12:44:01 +0100 Subject: [PATCH] Allow ticking and unticking checkboxes --- internal/conf/templates/list.tmpl.html | 2 +- internal/notes/notes.go | 55 +++++++++++--------------- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/internal/conf/templates/list.tmpl.html b/internal/conf/templates/list.tmpl.html index 6b39588..0fb8edf 100644 --- a/internal/conf/templates/list.tmpl.html +++ b/internal/conf/templates/list.tmpl.html @@ -3,7 +3,7 @@
{{range $note := .notes}} + href="{{$note.URL}}/"> {{$note.Title}} Edit diff --git a/internal/notes/notes.go b/internal/notes/notes.go index 81804eb..999cd83 100644 --- a/internal/notes/notes.go +++ b/internal/notes/notes.go @@ -1,10 +1,11 @@ -// Notes implements a data structure for reasoning about and rendering +// Package notes implements a data structure for reasoning about and rendering // markdown notes package notes import ( "bytes" "encoding/base64" + "errors" "fmt" "html/template" "log" @@ -102,47 +103,37 @@ func (t *toggleCheckboxTransformer) Transform(node *ast.Document, reader text.Re }) } -func (r *markdown.Renderer) renderTaskCheckBox(node ast.Node, entering bool) ast.WalkStatus { - if entering { - var itemPrefix []byte - l := r.rc.lists[len(r.rc.lists)-1] - - if l.list.IsOrdered() { - itemPrefix = append(itemPrefix, []byte(fmt.Sprint(l.num))...) - r.rc.lists[len(r.rc.lists)-1].num += 1 - } - itemPrefix = append(itemPrefix, l.list.Marker, ' ') - // Prefix the current line with the item prefix - r.rc.writer.PushPrefix(itemPrefix, 0, 0) - // Prefix subsequent lines with padding the same length as the item prefix - indentLen := int(max(r.config.NestedListLength, NestedListLengthMinimum)) - indent := bytes.Repeat([]byte{' '}, indentLen) - r.rc.writer.PushPrefix(bytes.Repeat(indent, len(itemPrefix)), 1) - } else { - r.rc.writer.PopPrefix() - r.rc.writer.PopPrefix() +func renderTaskCheckBox(writer util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { + var err error + var _ int + n, ok := node.(*astext.TaskCheckBox) + if !ok { + return ast.WalkContinue, errors.New("not a TaskCheckBox") } - return ast.WalkContinue + if entering { + // Render a box if necessary + if n.IsChecked { + _, err = writer.Write([]byte("[x] ")) + } else { + _, err = writer.Write([]byte("[ ] ")) + } + } + return ast.WalkContinue, err } func (n *Note) ToggleBox(nthBox int) { - log.Printf("Toggling %dth box", nthBox) - checkboxTransformer := toggleCheckboxTransformer{nthBox: nthBox} transformer := util.Prioritized(&checkboxTransformer, 0) renderer := markdown.NewRenderer() - renderer.Register(astext.KindTaskCheckBox, func(writer util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { - }) + renderer.Register(astext.KindTaskCheckBox, renderTaskCheckBox) var buf bytes.Buffer - md := goldmark.New( - goldmark.WithExtensions( - extension.TaskList, - ), - goldmark.WithRenderer(markdown.NewRenderer()), - goldmark.WithParserOptions(parser.WithASTTransformers(transformer)), - ) + md := goldmark.New(goldmark.WithRenderer(renderer)) + + md.Parser().AddOptions(parser.WithInlineParsers( + util.Prioritized(extension.NewTaskCheckBoxParser(), 0), + ), parser.WithASTTransformers(transformer)) md.Convert(n.Body, &buf) n.Body = buf.Bytes()