Created
April 24, 2019 10:13
-
-
Save asbjornu/4bbab38b6d65cd7b832669db68ef1828 to your computer and use it in GitHub Desktop.
Markdig checklist extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChecklistExtension : IMarkdownExtension | |
{ | |
public void Setup(MarkdownPipelineBuilder pipeline) | |
{ | |
if (pipeline.InlineParsers.Contains<TaskListInlineParser>()) | |
return; | |
if (pipeline.InlineParsers.Contains<ChecklistInlineParser>()) | |
return; | |
// Insert the parser after the code span parser | |
var checklistInlineParser = new ChecklistInlineParser(); | |
pipeline.InlineParsers.InsertBefore<LinkInlineParser>(checklistInlineParser); | |
} | |
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) | |
{ | |
var htmlRenderer = renderer as HtmlRenderer; | |
if (htmlRenderer != null) | |
{ | |
htmlRenderer.ObjectRenderers.AddIfNotAlready<HtmlChecklistItemRenderer>(); | |
} | |
var normalizeRenderer = renderer as NormalizeRenderer; | |
if (normalizeRenderer != null) | |
{ | |
normalizeRenderer.ObjectRenderers.AddIfNotAlready<NormalizeTaskListRenderer>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChecklistInlineParser : TaskListInlineParser | |
{ | |
public override bool Match(InlineProcessor processor, ref StringSlice slice) | |
{ | |
bool match = base.Match(processor, ref slice); | |
if (match) | |
{ | |
var listItemBlock = (ListItemBlock) processor.Block.Parent; | |
var listBlock = (ListBlock) listItemBlock.Parent; | |
var taskList = (TaskList) processor.Inline; | |
processor.Inline = new ChecklistItem(taskList, listBlock.Line, listItemBlock.Line); | |
} | |
return match; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChecklistItem : LeafInline | |
{ | |
private readonly TaskList taskList; | |
public ChecklistItem(TaskList taskList, int listBlockLineNumber, int listItemLineNumber) | |
{ | |
this.taskList = taskList ?? throw new ArgumentNullException(nameof(taskList)); | |
Id = $"checklist-{listBlockLineNumber}-item-{listItemLineNumber}"; | |
} | |
public bool Checked => this.taskList.Checked; | |
public string Id { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var checklistExtension = new ChecklistExtension(); | |
var pipelineBuilder = new MarkdownPipelineBuilder(); | |
pipelineBuilder.Extensions.Add(checklistExtension); | |
pipelineBuilder.DisableHtml(); | |
var pipeline = pipelineBuilder.Build(); | |
using (var stringWriter = new StringWriter()) | |
{ | |
var document = Markdown.ToHtml("…", stringWriter, pipeline); | |
var html = stringWriter.ToString(); | |
// How do I get to the leaf nodes within 'document'? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment