var source = []byte(`
- [ ] Task1
- [ ] Task2
`)
func main() {
md := goldmark.New(
goldmark.WithExtensions(extension.TaskList),
goldmark.WithParserOptions(
parser.WithASTTransformers(util.Prioritized(&PreserveTaskListSourcePosition{}, 1000)),
),
)
doc := md.Parser().Parse(text.NewReader(source))
md.Renderer().Render(os.Stdout, source, doc)
// <ul>
// <li><input disabled="" type="checkbox" data-source-position="[4,5]"> Task1</li>
// <li><input disabled="" type="checkbox" data-source-position="[16,17]"> Task2</li>
// </ul>
// This piece of code should be implemented by the Server
// to respond to a Click event on the Web page.
// We assume that we got a request to finish
// the task at `data-source-position="[16,17]"`.
{
var taskPos = `[16,17]`
var start, stop int
_, _ = fmt.Sscanf(taskPos, `[%d,%d]`, &start, &stop)
_ = stop
// mark it accomplished
source[start] = 'x'
doc = md.Parser().Parse(text.NewReader(source))
md.Renderer().Render(os.Stdout, source, doc)
// <ul>
// <li><input disabled="" type="checkbox" data-source-position="[4,5]"> Task1</li>
// <li><input checked="" disabled="" type="checkbox" data-source-position="[16,17]"> Task2</li>
// </ul>
// TODO: Save the new source and rendered content.
}
}
…allow Tasks to be Accomplished later (by updating markdown source and re-rendering).
This is for someone who may want to use Goldmark as a better TODO list implementation.
Reference AST Transformer
Reference Renderer (patched)
Finally, the Use case
Result in diff: