Insert custom html after paragraph #522

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

Originally created by @hey-red on GitHub (Mar 26, 2022).

There is way to insert html container after paragraph?

var markdownPipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build();

var document = Markdown.Parse("https://github.com/xoofx/markdig", _markdownPipeline);

var autoLinks = document
    .Descendants<LinkInline>()
    .Where(link => !link.IsImage && !link.IsShortcut && link.IsAutoLink);

foreach (var autoLink in autoLinks) 
{
    if (autoLink.Parent?.ParentBlock is ParagraphBlock paragraph)
    {
        // Insert raw html after paragraph
    }
}
Originally created by @hey-red on GitHub (Mar 26, 2022). There is way to insert html container after paragraph? ```C# var markdownPipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); var document = Markdown.Parse("https://github.com/xoofx/markdig", _markdownPipeline); var autoLinks = document .Descendants<LinkInline>() .Where(link => !link.IsImage && !link.IsShortcut && link.IsAutoLink); foreach (var autoLink in autoLinks) { if (autoLink.Parent?.ParentBlock is ParagraphBlock paragraph) { // Insert raw html after paragraph } } ```
claunia added the question label 2026-01-29 14:38:40 +00:00
Author
Owner

@xoofx commented on GitHub (Mar 27, 2022):

Something like that?

foreach (var autoLink in autoLinks) 
{
    if (autoLink.Parent?.ParentBlock is ParagraphBlock paragraph)
    {
        var parent = paragraph.Parent;
        parent.Insert(parent.IndexOf(paragraph) + 1, ...);
    }
}
@xoofx commented on GitHub (Mar 27, 2022): Something like that? ``` foreach (var autoLink in autoLinks) { if (autoLink.Parent?.ParentBlock is ParagraphBlock paragraph) { var parent = paragraph.Parent; parent.Insert(parent.IndexOf(paragraph) + 1, ...); } } ```
Author
Owner

@hey-red commented on GitHub (Mar 27, 2022):

@xoofx
Thanks for reply.
But how I can create Block element from string like "<div>test</div>" or construct Block object in some another way?

@hey-red commented on GitHub (Mar 27, 2022): @xoofx Thanks for reply. But how I can create Block element from string like` "<div>test</div>"` or construct Block object in some another way?
Author
Owner

@xoofx commented on GitHub (Mar 27, 2022):

But how I can create Block element from string like <div>test</div> or construct Block object in some another way?

Parse that with Markdig and see the resulting tree, you would have to replicate it manually.

@xoofx commented on GitHub (Mar 27, 2022): > But how I can create Block element from string like `<div>test</div>` or construct Block object in some another way? Parse that with Markdig and see the resulting tree, you would have to replicate it manually.
Author
Owner

@hey-red commented on GitHub (Mar 27, 2022):

It's works! Thanks for help.

private Block CreateBlockFromRawHtml(string html, MarkdownPipeline markdownPipeline)
{
    html.EnsureNotNullOrWhiteSpace();

    var document = Markdown.Parse(html, markdownPipeline);

    var blockFromDocument = document.First();
    document.Remove(blockFromDocument);

    return blockFromDocument;
}
@hey-red commented on GitHub (Mar 27, 2022): It's works! Thanks for help. ```C# private Block CreateBlockFromRawHtml(string html, MarkdownPipeline markdownPipeline) { html.EnsureNotNullOrWhiteSpace(); var document = Markdown.Parse(html, markdownPipeline); var blockFromDocument = document.First(); document.Remove(blockFromDocument); return blockFromDocument; } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#522