Skip to content

Commit

Permalink
Merge pull request #317 from orchitech/code-span-rule-fix
Browse files Browse the repository at this point in the history
Fix space-padding in code span rule
  • Loading branch information
martincizek authored Dec 2, 2020
2 parents d741a1d + 68a22b6 commit 04e499e
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 04e499e

Please sign in to comment.