From ca240b0b852a2e980bfc5d4b62ccb379e3cf2a9e Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 22 Sep 2016 18:17:02 +0100 Subject: [PATCH] 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

from the start and the

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. --- src/Markdown.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Markdown.js b/src/Markdown.js index 0151d0ae77..785aa4abfd 100644 --- a/src/Markdown.js +++ b/src/Markdown.js @@ -30,6 +30,14 @@ renderer.link = function(href, title, text) { } return marked.Renderer.prototype.apply(this, arguments); } +const PARAGRAPH_SUFFIX = '

'; +// 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 // level marked() interface, so we do it here. @@ -42,6 +50,7 @@ const marked_options = Object.assign({}, marked.defaults, { sanitize: true, smartLists: true, smartypants: false, + xhtml: true, // return self closing tags (ie.
not
) }); const real_parser = new marked.Parser(marked_options); @@ -109,6 +118,8 @@ export default class Markdown { } toHTML() { - return real_parser.parse(this._copyTokens()); + return real_parser.parse(this._copyTokens()).slice( + 0, 0 - PARAGRAPH_SUFFIX.length + ); } }