markdig
markdig copied to clipboard
Ability to render inline
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.
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()
;
}
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