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

feat(extension/tasklist): Preserve TaskList item source positions to … #452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading