Don't wrap everything in p tags

Preserves the old behaviour of not wrapping everything in p tags,
but also returns valid markup if the resulting markdown contains
multiple paragraphs (previously it stripped the <p> from the start
and the </p> from the end, leaving closing and opening paragraph tags
in the middle of the returned markup).

Also turn on the 'xhtml' option so marked uses self-closing tags
for br's, hr's and so forth.
pull/21833/head
David Baker 2016-09-22 18:17:02 +01:00
parent 0eddea1937
commit ca240b0b85
1 changed files with 12 additions and 1 deletions

View File

@ -30,6 +30,14 @@ renderer.link = function(href, title, text) {
} }
return marked.Renderer.prototype.apply(this, arguments); return marked.Renderer.prototype.apply(this, arguments);
} }
const PARAGRAPH_SUFFIX = '<br/><br/>';
// suffix paragraphs with double line breaks instead of
// wrapping them in 'p' tags: this makes it much easier
// for us to just strip one set of these off at the end,
// leaving valid markup if there were multiple paragraphs.
renderer.paragraph = function(text) {
return text + PARAGRAPH_SUFFIX;
}
// marked only applies the default options on the high // marked only applies the default options on the high
// level marked() interface, so we do it here. // level marked() interface, so we do it here.
@ -42,6 +50,7 @@ const marked_options = Object.assign({}, marked.defaults, {
sanitize: true, sanitize: true,
smartLists: true, smartLists: true,
smartypants: false, smartypants: false,
xhtml: true, // return self closing tags (ie. <br /> not <br>)
}); });
const real_parser = new marked.Parser(marked_options); const real_parser = new marked.Parser(marked_options);
@ -109,6 +118,8 @@ export default class Markdown {
} }
toHTML() { toHTML() {
return real_parser.parse(this._copyTokens()); return real_parser.parse(this._copyTokens()).slice(
0, 0 - PARAGRAPH_SUFFIX.length
);
} }
} }