Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 945
Šime Vidas Avatar asked Dec 08 '12 22:12

Šime Vidas


1 Answers

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>'
like image 153
ThisIzKp Avatar answered Sep 28 '22 11:09

ThisIzKp