feat(extension/tasklist): Preserve TaskList item source positions to allow Tasks to be Accomplished later.

This commit is contained in:
movsb 2024-05-28 18:06:09 +08:00
parent c15e394c27
commit 32a63fedc1
2 changed files with 17 additions and 2 deletions

View file

@ -2,12 +2,18 @@ package ast
import ( import (
"fmt" "fmt"
gast "github.com/yuin/goldmark/ast" gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
) )
// A TaskCheckBox struct represents a checkbox of a task list. // A TaskCheckBox struct represents a checkbox of a task list.
type TaskCheckBox struct { type TaskCheckBox struct {
gast.BaseInline gast.BaseInline
// Segment is a position in a source text.
Segment text.Segment
IsChecked bool IsChecked bool
} }
@ -33,3 +39,11 @@ func NewTaskCheckBox(checked bool) *TaskCheckBox {
IsChecked: checked, 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,
}
}

View file

@ -47,7 +47,7 @@ func (s *taskCheckBoxParser) Parse(parent gast.Node, block text.Reader, pc parse
if _, ok := parent.Parent().(*gast.ListItem); !ok { if _, ok := parent.Parent().(*gast.ListItem); !ok {
return nil return nil
} }
line, _ := block.PeekLine() line, lineSegment := block.PeekLine()
m := taskListRegexp.FindSubmatchIndex(line) m := taskListRegexp.FindSubmatchIndex(line)
if m == nil { if m == nil {
return 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] value := line[m[2]:m[3]][0]
block.Advance(m[1]) block.Advance(m[1])
checked := value == 'x' || value == 'X' 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) { func (s *taskCheckBoxParser) CloseBlock(parent gast.Node, pc parser.Context) {