javascriptmarkdown

How to prevent Markdown from wrapping the generated HTML in a <p> element?


Update: The bounty is for a solution using the “marked” library.


This Markdown code:

*foo*

will produce this HTML code:

<p><em>foo</em></p>

Live demo: https://jsbin.com/luganot/edit?js,console

However, I'm already injecting the generated HTML code into an inline context, like so:

<p> text [inject generated HTML here] text </p>

so I don't want the <p> element to wrap around the generated HTML code. I just want the * delimiters to be converted to an <em>, element, and so on.

Is there a way to tell the Markdown converter to not produce the <p> wrapper? Currently, I'm doing a .slice(3,-4) on the generated HTML string, which does remove the <p>, and </p> tags, but this is obviously not a solution I'd like to keep for the long-term.


Solution

  • You can skip the block-lexing part and use inlineLexer instead.

    html = marked.inlineLexer(markdown, [], options);
    
    //example
    marked.inlineLexer('*foo*', []) // will output '<em>foo</em>'