Obtain HTML for Markdown fragments? #561

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

Originally created by @pm64 on GitHub (Sep 19, 2022).

I'm new to this API, so please accept my apologies in advance for what is likely a dumb question.

I'm trying to use Markdig to generate just the HTML corresponding to individual sections of Markdown that are part of a larger document.

For example, given the input string "hello *this* is a test", I want to receive "hello ", "<em>this</em>", and " is a test" (or similar) as separate HTML fragments, each of which can be correlated back to the corresponding Markdown fragment ("hello ", "*this*", and " is a test", respectively). Is something like that possible?

Originally created by @pm64 on GitHub (Sep 19, 2022). I'm new to this API, so please accept my apologies in advance for what is likely a dumb question. I'm trying to use Markdig to generate just the HTML corresponding to individual sections of Markdown that are part of a larger document. For example, given the input string "hello \*this\* is a test", I want to receive "hello ", "\<em\>this\</em\>", and " is a test" (or similar) as separate HTML fragments, each of which can be correlated back to the corresponding Markdown fragment ("hello ", "\*this\*", and " is a test", respectively). Is something like that possible?
claunia added the question label 2026-01-29 14:39:45 +00:00
Author
Owner

@MihaZupan commented on GitHub (Sep 19, 2022):

Here's a sample that should let you get started

using Markdig;
using Markdig.Renderers;
using Markdig.Syntax;

// Setup
MarkdownPipeline pipeline = new MarkdownPipelineBuilder()
    .UseAdvancedExtensions()
    .UsePreciseSourceLocation()
    .Build();

var writer = new StringWriter();

var renderer = new HtmlRenderer(writer);
pipeline.Setup(renderer);

// Repeatable
string markdown = "hello *this* is a test";
MarkdownDocument document = Markdown.Parse(markdown, pipeline);

foreach (MarkdownObject obj in document.Descendants())
{
    renderer.Render(obj);
    string html = writer.ToString();
    writer.GetStringBuilder().Length = 0; // Reset writer

    Console.WriteLine($"{obj.GetType().Name} at {obj.ToPositionText()}: {html}");
}

This will print

ParagraphBlock at $0, 0, 0-21: <p>hello <em>this</em> is a test</p>

LiteralInline at $0, 0, 0-5: hello
EmphasisInline at $0, 6, 6-11: <em>this</em>
LiteralInline at $0, 7, 7-10: this
LiteralInline at $0, 12, 12-21:  is a test

Notice <em>this</em> and this on separate lines - this is because EmphasisInline contains the LiteralInline as a child node.
Take a look at https://github.com/xoofx/markdig/blob/master/doc/parsing-ast.md for some info about how to deal with the AST.

@MihaZupan commented on GitHub (Sep 19, 2022): Here's a sample that should let you get started ```c# using Markdig; using Markdig.Renderers; using Markdig.Syntax; // Setup MarkdownPipeline pipeline = new MarkdownPipelineBuilder() .UseAdvancedExtensions() .UsePreciseSourceLocation() .Build(); var writer = new StringWriter(); var renderer = new HtmlRenderer(writer); pipeline.Setup(renderer); // Repeatable string markdown = "hello *this* is a test"; MarkdownDocument document = Markdown.Parse(markdown, pipeline); foreach (MarkdownObject obj in document.Descendants()) { renderer.Render(obj); string html = writer.ToString(); writer.GetStringBuilder().Length = 0; // Reset writer Console.WriteLine($"{obj.GetType().Name} at {obj.ToPositionText()}: {html}"); } ``` This will print ``` ParagraphBlock at $0, 0, 0-21: <p>hello <em>this</em> is a test</p> LiteralInline at $0, 0, 0-5: hello EmphasisInline at $0, 6, 6-11: <em>this</em> LiteralInline at $0, 7, 7-10: this LiteralInline at $0, 12, 12-21: is a test ``` Notice `<em>this</em>` and `this` on separate lines - this is because `EmphasisInline` contains the `LiteralInline` as a child node. Take a look at https://github.com/xoofx/markdig/blob/master/doc/parsing-ast.md for some info about how to deal with the AST.
Author
Owner

@pm64 commented on GitHub (Sep 19, 2022):

@MihaZupan, this is exactly what I was looking for. Immensely appreciate all you do, thank you!

@pm64 commented on GitHub (Sep 19, 2022): @MihaZupan, this is exactly what I was looking for. Immensely appreciate all you do, thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#561