diff --git a/src/commonmark-rules.js b/src/commonmark-rules.js index 5bc99b28..77a4c529 100644 --- a/src/commonmark-rules.js +++ b/src/commonmark-rules.js @@ -233,19 +233,15 @@ rules.code = { }, replacement: function (content) { - if (!content.trim()) return '' + if (!content) return '' + content = content.replace(/\r?\n|\r/g, ' ') + var extraSpace = /^`|^ .*?[^ ].* $|`$/.test(content) ? ' ' : '' var delimiter = '`' - var leadingSpace = '' - var trailingSpace = '' - var matches = content.match(/`+/gm) - if (matches) { - if (/^`/.test(content)) leadingSpace = ' ' - if (/`$/.test(content)) trailingSpace = ' ' - while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`' - } + var matches = content.match(/`+/gm) || [] + while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`' - return delimiter + leadingSpace + content + trailingSpace + delimiter + return delimiter + extraSpace + content + extraSpace + delimiter } } diff --git a/test/index.html b/test/index.html index 4dd70aa0..0cdb3a2c 100644 --- a/test/index.html +++ b/test/index.html @@ -69,7 +69,7 @@
`starting with a backtick
-
`` `starting with a backtick``
+
`` `starting with a backtick ``