Markdown in JS without enclosing <p>?
The marked library allows you to define your own renderer, which allows you to define the output for paragraphs.
You can pass in your own renderer by using:
marked.setOptions({
renderer: new MyRenderer(),
});
var output = marked('This **text** will be rendered with MyRenderer');
This will require you to define methods for blockquote
, html
, paragraph
and all the other methods that the default marked.Renderer
defines.
Here is an example:
function MyRenderer(options) {
this.options = options || {};
}
MyRenderer.prototype.paragraph = function(text) {
return 'This comes from my own renderer: ' + text + '\n';
};
However, this requires some efforts, so the quickest way to get rid of the paragraphs (<p>
tags) is to change the code of the existing Renderer in the marked.js
file:
Replace:
Renderer.prototype.paragraph = function(text) {
return '<p>' + text + '</p>\n';
};
With:
Renderer.prototype.paragraph = function(text) {
return text + '\n';
};
markdown-it has md.renderInline() method which allows to do that.