Skip to content

Commit

Permalink
Fix <br> at the beginning or end of <b> and <i>
Browse files Browse the repository at this point in the history
Fixes bug mixmark-io#436
  • Loading branch information
za3k committed Jul 30, 2024
1 parent cc73387 commit 5c76897
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/commonmark-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,13 @@ <h2>This is a header.</h2>
<pre class="expected">Text with no space after the period. _Text in em with leading/trailing spaces_ **text in strong with trailing space**</pre>
</div>

<!-- Two trailing newlines are for the line break -->
<div class="case" data-name="newline at the end of a strong element">
<div class="input"><strong>Text with a trailing break<br></strong>before continuation</div>
<pre class="expected">**Text with a trailing break**
before continuation</pre>
</div>

<div class="case" data-name="whitespace in nested inline elements">
<div class="input">Text at root <strong><a href="http://www.example.com">link text with trailing space in strong </a></strong>more text at root</div>
<pre class="expected">Text at root **[link text with trailing space in strong](http://www.example.com)** more text at root</pre>
Expand Down

0 comments on commit 5c76897

Please sign in to comment.