From 32a63fedc159ee3ddbe780783211cdd9b79a052c Mon Sep 17 00:00:00 2001 From: movsb Date: Tue, 28 May 2024 18:06:09 +0800 Subject: [PATCH] feat(extension/tasklist): Preserve TaskList item source positions to allow Tasks to be Accomplished later. --- extension/ast/tasklist.go | 14 ++++++++++++++ extension/tasklist.go | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/extension/ast/tasklist.go b/extension/ast/tasklist.go index 670cc14..2f7a3a6 100644 --- a/extension/ast/tasklist.go +++ b/extension/ast/tasklist.go @@ -2,12 +2,18 @@ package ast import ( "fmt" + gast "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/text" ) // A TaskCheckBox struct represents a checkbox of a task list. type TaskCheckBox struct { gast.BaseInline + + // Segment is a position in a source text. + Segment text.Segment + IsChecked bool } @@ -33,3 +39,11 @@ func NewTaskCheckBox(checked bool) *TaskCheckBox { IsChecked: checked, } } + +// NewTaskCheckBoxSegment returns a new TaskCheckBox node with the given source position. +func NewTaskCheckBoxSegment(checked bool, segment text.Segment) *TaskCheckBox { + return &TaskCheckBox{ + IsChecked: checked, + Segment: segment, + } +} diff --git a/extension/tasklist.go b/extension/tasklist.go index 4467ebf..6a5e98e 100644 --- a/extension/tasklist.go +++ b/extension/tasklist.go @@ -47,7 +47,7 @@ func (s *taskCheckBoxParser) Parse(parent gast.Node, block text.Reader, pc parse if _, ok := parent.Parent().(*gast.ListItem); !ok { return nil } - line, _ := block.PeekLine() + line, lineSegment := block.PeekLine() m := taskListRegexp.FindSubmatchIndex(line) if m == nil { return nil @@ -55,7 +55,8 @@ func (s *taskCheckBoxParser) Parse(parent gast.Node, block text.Reader, pc parse value := line[m[2]:m[3]][0] block.Advance(m[1]) checked := value == 'x' || value == 'X' - return ast.NewTaskCheckBox(checked) + segment := text.NewSegment(lineSegment.Start+m[2], lineSegment.Start+m[3]) + return ast.NewTaskCheckBoxSegment(checked, segment) } func (s *taskCheckBoxParser) CloseBlock(parent gast.Node, pc parser.Context) {