When doing .ToHtml() how to avoid the outer <p> element? #557

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

Originally created by @thepra on GitHub (Sep 12, 2022).

Can't quite figure out how to get rid of that <p> element in every parsed markdown.
My context is that I want to put the parsed markdown in the <summary> element, but <p> make the parsed text go to a new line, making an ugly looking summary.

Or even better it would nice to know how to choose with which outer HTML element parse the markdown to HTML.

Originally created by @thepra on GitHub (Sep 12, 2022). Can't quite figure out how to get rid of that `<p>` element in every parsed markdown. My context is that I want to put the parsed markdown in the `<summary>` element, but `<p>` make the parsed text go to a new line, making an ugly looking summary. Or even better it would nice to know how to choose with which outer HTML element parse the markdown to HTML.
claunia added the question label 2026-01-29 14:39:39 +00:00
Author
Owner

@xoofx commented on GitHub (Sep 12, 2022):

Use String.Replace or a library like AngleSharp (for more advanced scenarios). There is no plan to customize in Markdig the HtmlRenderer for such scenarios.

@xoofx commented on GitHub (Sep 12, 2022): Use `String.Replace` or a library like [AngleSharp](https://github.com/AngleSharp/AngleSharp) (for more advanced scenarios). There is no plan to customize in Markdig the HtmlRenderer for such scenarios.
Author
Owner

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

You can also write a custom renderer based on the built-in ParagraphRenderer.

Something like

var pipeline = new MarkdownPipelineBuilder()
    .UseAdvancedExtensions()
    .Use(new MyParagraphExtension("summary"))
    .Build();
MyParagraphExtension.cs
public sealed class MyParagraphExtension : IMarkdownExtension
{
    private readonly CustomTagParagraphRenderer _renderer;

    public MyParagraphExtension(string tag)
    {
        _renderer = new CustomTagParagraphRenderer(tag);
    }

    public void Setup(MarkdownPipelineBuilder pipeline) { }

    public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
    {
        if (renderer is HtmlRenderer)
        {
            renderer.ObjectRenderers.ReplaceOrAdd<HtmlObjectRenderer<ParagraphBlock>>(_renderer);
        }
    }

    private sealed class CustomTagParagraphRenderer : HtmlObjectRenderer<ParagraphBlock>
    {
        private readonly string _openingTag;
        private readonly string _closingTag;

        public CustomTagParagraphRenderer(string tag)
        {
            _openingTag = $"<{tag}";
            _closingTag = $"</{tag}>";
        }

        protected override void Write(HtmlRenderer renderer, ParagraphBlock obj)
        {
            if (!renderer.ImplicitParagraph && renderer.EnableHtmlForBlock)
            {
                if (!renderer.IsFirstInContainer)
                {
                    renderer.EnsureLine();
                }

                renderer.Write(_openingTag);
                renderer.WriteAttributes(obj);
                renderer.Write('>');
            }
            renderer.WriteLeafInline(obj);
            if (!renderer.ImplicitParagraph)
            {
                if (renderer.EnableHtmlForBlock)
                {
                    renderer.WriteLine(_closingTag);
                }

                renderer.EnsureLine();
            }
        }
    }
}

@MihaZupan commented on GitHub (Sep 12, 2022): You can also write a custom renderer based on the built-in [`ParagraphRenderer`](https://github.com/xoofx/markdig/blob/master/src/Markdig/Renderers/Html/ParagraphRenderer.cs). Something like ```c# var pipeline = new MarkdownPipelineBuilder() .UseAdvancedExtensions() .Use(new MyParagraphExtension("summary")) .Build(); ``` <details> <summary>MyParagraphExtension.cs</summary> ```c# public sealed class MyParagraphExtension : IMarkdownExtension { private readonly CustomTagParagraphRenderer _renderer; public MyParagraphExtension(string tag) { _renderer = new CustomTagParagraphRenderer(tag); } public void Setup(MarkdownPipelineBuilder pipeline) { } public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { if (renderer is HtmlRenderer) { renderer.ObjectRenderers.ReplaceOrAdd<HtmlObjectRenderer<ParagraphBlock>>(_renderer); } } private sealed class CustomTagParagraphRenderer : HtmlObjectRenderer<ParagraphBlock> { private readonly string _openingTag; private readonly string _closingTag; public CustomTagParagraphRenderer(string tag) { _openingTag = $"<{tag}"; _closingTag = $"</{tag}>"; } protected override void Write(HtmlRenderer renderer, ParagraphBlock obj) { if (!renderer.ImplicitParagraph && renderer.EnableHtmlForBlock) { if (!renderer.IsFirstInContainer) { renderer.EnsureLine(); } renderer.Write(_openingTag); renderer.WriteAttributes(obj); renderer.Write('>'); } renderer.WriteLeafInline(obj); if (!renderer.ImplicitParagraph) { if (renderer.EnableHtmlForBlock) { renderer.WriteLine(_closingTag); } renderer.EnsureLine(); } } } } ``` </details>
Author
Owner

@jjxtra commented on GitHub (Jan 10, 2023):

This still puts in a

and ending

even for text without newlines. How do I turn that behavior off?

@jjxtra commented on GitHub (Jan 10, 2023): This still puts in a <p> and ending </p> even for text without newlines. How do I turn that behavior off?
Author
Owner

@xoofx commented on GitHub (Jan 10, 2023):

even for text without newlines. How do I turn that behavior off?

string.StartWith + replace and/or trim.

@xoofx commented on GitHub (Jan 10, 2023): > even for text without newlines. How do I turn that behavior off? string.StartWith + replace and/or trim.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#557