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

[AutoIdentifiers] Not created when rendering a MarkdownDocument #646

Closed
hangar18rip opened this issue Jun 24, 2022 · 3 comments
Closed

[AutoIdentifiers] Not created when rendering a MarkdownDocument #646

hangar18rip opened this issue Jun 24, 2022 · 3 comments
Labels

Comments

@hangar18rip
Copy link

hangar18rip commented Jun 24, 2022

Hi,

I get the following issue with the AutoIdentifierExtension.

Repro step

Using this code:

var pipeline = new MarkdownPipelineBuilder().UseAutoIdentifiers(AutoIdentifierOptions.GitHub).Build();
var input = "# Hey Isabelle, nice to meet you";

var res = Markdig.Markdown.ToHtml(input, pipeline);
Trace.TraceInformation(res);

var doc = Markdig.Markdown.Parse(input);
var res2 = Markdig.Markdown.ToHtml(doc, pipeline);
Trace.TraceInformation(res2);

It gives me this result:

res = <h1 id=\"hey-isabelle-nice-to-meet-you\">Hey Isabelle, nice to meet you</h1>\n
res2 = <h1>Hey Isabelle, nice to meet you</h1>\n

The content of res is the expected result.

Workaround

var doc = MarkdownParser.Parse(content, pipeline, null);

or

var doc = Markdig.Markdown.Parse(content, pipeline);
@MihaZupan
Copy link
Collaborator

You have to pass the pipeline to Markdown.Parse as well

@hangar18rip
Copy link
Author

thanks @MihaZupan, I have it now but this was not very intuitive ;) Why should I pass the pipeline to both parse the document AND to render it. Maybe it can be a future improvment 😉

@xoofx xoofx added the invalid label Jun 26, 2022
@xoofx
Copy link
Owner

xoofx commented Jun 26, 2022

I have it now but this was not very intuitive ;) Why should I pass the pipeline to both parse the document AND to render it. Maybe it can be a future improvement

The pipeline contains both the configured parsers and the configured renderers.

For the case of the auto-identifier extension, the extension hooks into existing parsers.

public void Setup(MarkdownPipelineBuilder pipeline)
{
var headingBlockParser = pipeline.BlockParsers.Find<HeadingBlockParser>();
if (headingBlockParser != null)
{
// Install a hook on the HeadingBlockParser when a HeadingBlock is actually processed
headingBlockParser.Closed -= HeadingBlockParser_Closed;
headingBlockParser.Closed += HeadingBlockParser_Closed;
}
var paragraphBlockParser = pipeline.BlockParsers.FindExact<ParagraphBlockParser>();
if (paragraphBlockParser != null)
{
// Install a hook on the ParagraphBlockParser when a HeadingBlock is actually processed as a Setex heading
paragraphBlockParser.Closed -= HeadingBlockParser_Closed;
paragraphBlockParser.Closed += HeadingBlockParser_Closed;
}
}

In that case, the renderer is not modified (because the heading renderer is registered by default), but for others, they require to register their renderers as well:

public void Setup(MarkdownPipelineBuilder pipeline)
{
pipeline.BlockParsers.AddIfNotAlready<AbbreviationParser>();
}
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
{
if (renderer is HtmlRenderer htmlRenderer && !htmlRenderer.ObjectRenderers.Contains<HtmlAbbreviationRenderer>())
{
// Must be inserted before CodeBlockRenderer
htmlRenderer.ObjectRenderers.Insert(0, new HtmlAbbreviationRenderer());
}

@xoofx xoofx closed this as completed Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants