Skip to content

Commit

Permalink
Follow CommonMark spec more precisely and make the code span rule eve…
Browse files Browse the repository at this point in the history
…n more robust for future Turndown development. Fix #316.
  • Loading branch information
martincizek authored and Michal Bartoš committed Nov 30, 2020
1 parent 8ba0446 commit 68a22b6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/commonmark-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

<div class="case" data-name="code starting with a backtick">
<div class="input"><code>`starting with a backtick</code></div>
<pre class="expected">`` `starting with a backtick``</pre>
<pre class="expected">`` `starting with a backtick ``</pre>
</div>

<div class="case" data-name="code containing markdown syntax">
Expand Down

0 comments on commit 68a22b6

Please sign in to comment.