Skip to content

Commit

Permalink
feat(extension/tasklist): Preserve TaskList item source positions to …
Browse files Browse the repository at this point in the history
…allow Tasks to be Accomplished later.
  • Loading branch information
movsb committed May 28, 2024
1 parent c15e394 commit 32a63fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 14 additions & 0 deletions extension/ast/tasklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
}
}
5 changes: 3 additions & 2 deletions extension/tasklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ 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
}
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) {
Expand Down

0 comments on commit 32a63fe

Please sign in to comment.