Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thepra opened this issue Sep 12, 2022 · 4 comments
Closed

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

thepra opened this issue Sep 12, 2022 · 4 comments
Labels

Comments

@thepra
Copy link

thepra commented 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.

@xoofx xoofx added the question label Sep 12, 2022
@xoofx
Copy link
Owner

xoofx commented 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.

@MihaZupan
Copy link
Collaborator

MihaZupan commented 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();
            }
        }
    }
}

@xoofx xoofx closed this as completed Sep 22, 2022
@jjxtra
Copy link

jjxtra commented Jan 10, 2023

This still puts in a

and ending

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

@xoofx
Copy link
Owner

xoofx commented Jan 10, 2023

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

string.StartWith + replace and/or trim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants