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

Using the Same Source Markdown... Markdown.ToHtml() != document.ToHtml() #811

Open
nepgituser opened this issue Jul 28, 2024 · 2 comments
Open
Labels

Comments

@nepgituser
Copy link

Hello. Yesterday, I tested converting an MD file to HTML (html1 below). This worked fine. Then I decided to try parsing the markdown so I could make changes to it. Before spending a bunch of time on that (new to Markdig), I wanted to make sure that I could get it to output correctly (html2 below). This did not work. Below is the code I'm using. I would expect html1 and html2 to be identical, however, they are not.

string markdown = File.ReadAllText($@"{mdPath}\Documentation.md");

var document = Markdown.Parse(markdown);
var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();

string html1 = Markdown.ToHtml(markdown, pipeline);
string html2 = document.ToHtml(pipeline);

If the Documentation.md file in the example above only contains "# General" (no quotes), html1 is <h1 id="general">General</h1> and html2 is <h1>General</h1>.

The ToHtml source formats are different (markdown vs MarkdownDocument), but both ultimately came from the same source markdown. And they run through the same pipeline, which I assume is where all the logic is. What am I missing? Why does one H1 have an ID but the other does not?

@xoofx
Copy link
Owner

xoofx commented Jul 28, 2024

You need to pass the pipeline to both Parse and ToHtml or by looking at what is behind ToHtml:

var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
var document = Markdown.Parse(markdown, pipeline);

string html1 = Markdown.ToHtml(markdown, pipeline);
string html2 = document.ToHtml(pipeline);

public static string ToHtml(string markdown, MarkdownPipeline? pipeline = null, MarkdownParserContext? context = null)
{
if (markdown is null) ThrowHelper.ArgumentNullException_markdown();
pipeline = GetPipeline(pipeline, markdown);
var document = MarkdownParser.Parse(markdown, pipeline, context);
return ToHtml(document, pipeline);
}

@xoofx xoofx added the question label Jul 28, 2024
@nepgituser
Copy link
Author

I would have expected one of the pipeline "passes" to be redundant, but as you showed, that is how the code works. Suppose that would come into play if, like I'm trying, the document is changed after the initial parse.

At any rate, thank you for the quick and detailed response.

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

2 participants