Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <br> at the beginning or end of <b> and <i> #480

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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