MarkdownDocument to HTML #70

Closed
opened 2026-01-29 14:24:58 +00:00 by claunia · 2 comments
Owner

Originally created by @Weasy666 on GitHub (Nov 30, 2016).

Is there an easy way to convert a MarkdownDocument to HTML? Like

var doc = Markdown.Parse("some markdown text")
var html = doc.ToHtml()

I feel plain stupid but i couldn't find a way to convert a MarkdownDocument to a HTML string.

I can't use the Markdown.ToHtml() with the markdown text as a string, because i have some Relative URLs like ../pics/picture.png and need to convert them to Absolute URLs.
I'm doing this with the MarkdownDocument.Descendants().OfType<LinkInline>() and modifing the Url property. But after doing that i'm not able to convert the MarkdownDocument to HTML.

It would be great if you could help me with this matter.

Originally created by @Weasy666 on GitHub (Nov 30, 2016). Is there an easy way to convert a MarkdownDocument to HTML? Like ``` C# var doc = Markdown.Parse("some markdown text") var html = doc.ToHtml() ``` I feel plain stupid but i couldn't find a way to convert a MarkdownDocument to a HTML string. I can't use the Markdown.ToHtml() with the markdown text as a string, because i have some Relative URLs like `../pics/picture.png` and need to convert them to Absolute URLs. I'm doing this with the `MarkdownDocument.Descendants().OfType<LinkInline>()` and modifing the Url property. But after doing that i'm not able to convert the MarkdownDocument to HTML. It would be great if you could help me with this matter.
claunia added the question label 2026-01-29 14:24:58 +00:00
Author
Owner

@xoofx commented on GitHub (Nov 30, 2016):

Use HtmlRenderer directly (as it is done by the Markdown.ToHtml method), something like this:

var markdownDocument = Markdown.Parse(...)
// process markdownDocument
[...]
// Output
var writer = new StringWriter();
// Create a HTML Renderer and setup it with the pipeline
var renderer = new HtmlRenderer(writer);
pipeline.Setup(renderer);
// Renders markdown to HTML (to the writer)
renderer.Render(markdownDocument);
// Gets the rendered string
var result = writer.ToString();
@xoofx commented on GitHub (Nov 30, 2016): Use `HtmlRenderer` directly (as it is done by the `Markdown.ToHtml` method), something like this: ```csharp var markdownDocument = Markdown.Parse(...) // process markdownDocument [...] // Output var writer = new StringWriter(); // Create a HTML Renderer and setup it with the pipeline var renderer = new HtmlRenderer(writer); pipeline.Setup(renderer); // Renders markdown to HTML (to the writer) renderer.Render(markdownDocument); // Gets the rendered string var result = writer.ToString(); ```
Author
Owner

@Weasy666 commented on GitHub (Nov 30, 2016):

Works like a charm. Thank you!

@Weasy666 commented on GitHub (Nov 30, 2016): Works like a charm. Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#70