diff --git a/src/commonmark-rules.js b/src/commonmark-rules.js index f32a8933..2dde0b55 100644 --- a/src/commonmark-rules.js +++ b/src/commonmark-rules.js @@ -207,12 +207,26 @@ rules.referenceLink = { } } +const WHITESPACE_START = /^(\\?\n| )+/ +const WHITESPACE_END = /(\\?\n| )+$/ rules.emphasis = { filter: ['em', 'i'], replacement: function (content, node, options) { if (!content.trim()) return '' - return options.emDelimiter + content + options.emDelimiter + var startWhitespace = '' + var endWhitespace = '' + var m = WHITESPACE_START.exec(content) + if (m) { + startWhitespace = m[0] + content = content.slice(startWhitespace.length) + } + m = WHITESPACE_END.exec(content) + if (m) { + endWhitespace = m[0] + content = content.slice(0, -endWhitespace.length) + } + return startWhitespace + options.emDelimiter + content + options.emDelimiter + endWhitespace } } @@ -221,7 +235,19 @@ rules.strong = { replacement: function (content, node, options) { if (!content.trim()) return '' - return options.strongDelimiter + content + options.strongDelimiter + var startWhitespace = '' + var endWhitespace = '' + var m = WHITESPACE_START.exec(content) + if (m) { + startWhitespace = m[0] + content = content.slice(startWhitespace.length) + } + m = WHITESPACE_END.exec(content) + if (m) { + endWhitespace = m[0] + content = content.slice(0, -endWhitespace.length) + } + return startWhitespace + options.strongDelimiter + content + options.strongDelimiter + endWhitespace } } diff --git a/test/index.html b/test/index.html index 065de26e..7fd34ac3 100644 --- a/test/index.html +++ b/test/index.html @@ -693,6 +693,13 @@

This is a header.

Text with no space after the period. _Text in em with leading/trailing spaces_ **text in strong with trailing space**
+ +
+
Text with a trailing break
before continuation
+
**Text with a trailing break**  
+before continuation
+
+
Text at root link text with trailing space in strong more text at root
Text at root **[link text with trailing space in strong](http://www.example.com)** more text at root