Ability to render inline #530

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

Originally created by @TheJayMann on GitHub (Apr 19, 2022).

Is there any ability to have a render inline, something similar to the renderInline function available in markdown-it? The idea being, I want to take a small piece of markdown, convert it into html, then insert the html inside, for example, an <h3> element, which should only contain text and inline elements, and should not contain block elements.

As a workaround, I considered the idea of potentially taking the markdown, removing any newlines in the markdown (as a simple precaution, though there will likely never be any newlines in the markdown text), surrounding them in inline custom containers, parsing the markdown into a MarkdownDocument, then rendering the contents of the ContainerInline within the ParagraphBlock. I got as far as parsing the document, and I'm still working out how I would create a renderer which would render as html only the ContainerInline.

Originally created by @TheJayMann on GitHub (Apr 19, 2022). Is there any ability to have a render inline, something similar to the [renderInline](https://markdown-it.github.io/markdown-it/#MarkdownIt.renderInline) function available in markdown-it? The idea being, I want to take a small piece of markdown, convert it into html, then insert the html inside, for example, an `<h3>` element, which should only contain text and inline elements, and should not contain block elements. As a workaround, I considered the idea of potentially taking the markdown, removing any newlines in the markdown (as a simple precaution, though there will likely never be any newlines in the markdown text), surrounding them in inline custom containers, parsing the markdown into a MarkdownDocument, then rendering the contents of the ContainerInline within the ParagraphBlock. I got as far as parsing the document, and I'm still working out how I would create a renderer which would render as html only the ContainerInline.
claunia added the question label 2026-01-29 14:38:52 +00:00
Author
Owner

@TheJayMann commented on GitHub (Apr 20, 2022):

I do believe I was able to come up with a suitable workaround by making use of the contents of rendering an ATX header.

public static string ToInlineHtml(string inlineMarkdown, MarkdownPipeline? pipeline = null, MarkdownParserContext? context = null) {
    var renderer = new HtmlRenderer(new StringWriter());
    pipeline?.Setup(renderer);
    return
        renderer
        .WriteLeafInline((LeafBlock)Markdown.Parse( "# " + inlineMarkdown.ReplaceLineEndings(" ") + " #", pipeline, context)[0])
        .Writer.ToString()
    ;
}
@TheJayMann commented on GitHub (Apr 20, 2022): I do believe I was able to come up with a suitable workaround by making use of the contents of rendering an ATX header. ```csharp public static string ToInlineHtml(string inlineMarkdown, MarkdownPipeline? pipeline = null, MarkdownParserContext? context = null) { var renderer = new HtmlRenderer(new StringWriter()); pipeline?.Setup(renderer); return renderer .WriteLeafInline((LeafBlock)Markdown.Parse( "# " + inlineMarkdown.ReplaceLineEndings(" ") + " #", pipeline, context)[0]) .Writer.ToString() ; } ```
Author
Owner

@MihaZupan commented on GitHub (Apr 23, 2022):

Playing around with this a bit, you can try using this:

string markdown = "# test *italic* **bold** foo\n\nfoo\n\n**bold**\n\ntest\n";
string html = InlineMarkdown.ToHtml(markdown);
Console.WriteLine(html);

With InlineMarkdown.cs

@MihaZupan commented on GitHub (Apr 23, 2022): Playing around with this a bit, you can try using this: ```c# string markdown = "# test *italic* **bold** foo\n\nfoo\n\n**bold**\n\ntest\n"; string html = InlineMarkdown.ToHtml(markdown); Console.WriteLine(html); ``` With [`InlineMarkdown.cs`](https://gist.github.com/MihaZupan/4a583c6170c74e215fc3dcba14db6a33)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#530