Skip to content

Commit

Permalink
Disable replacements inside autolinks
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Aug 16, 2016
1 parent 126bc6b commit 9335394
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
26 changes: 22 additions & 4 deletions lib/rules_core/replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,32 @@ function replaceFn(match, name) {
}

function replace_scoped(inlineTokens) {
var i, token;
var i, token, inside_autolink = 0;

for (i = inlineTokens.length - 1; i >= 0; i--) {
token = inlineTokens[i];
if (token.type === 'text') {

if (token.type === 'text' && !inside_autolink) {
token.content = token.content.replace(SCOPED_ABBR_RE, replaceFn);
}

if (token.type === 'link_open' && token.info === 'auto') {
inside_autolink--;
}

if (token.type === 'link_close' && token.info === 'auto') {
inside_autolink++;
}
}
}

function replace_rare(inlineTokens) {
var i, token;
var i, token, inside_autolink = 0;

for (i = inlineTokens.length - 1; i >= 0; i--) {
token = inlineTokens[i];
if (token.type === 'text') {

if (token.type === 'text' && !inside_autolink) {
if (RARE_RE.test(token.content)) {
token.content = token.content
.replace(/\+-/g, '±')
Expand All @@ -64,6 +74,14 @@ function replace_rare(inlineTokens) {
.replace(/(^|[^-\s])--([^-\s]|$)/mg, '$1\u2013$2');
}
}

if (token.type === 'link_open' && token.info === 'auto') {
inside_autolink--;
}

if (token.type === 'link_close' && token.info === 'auto') {
inside_autolink++;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/markdown-it/linkify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ typorgapher should not break href
.
http://example.com/(c)
.
<p><a href="http://example.com/(c)">http://example.com/©</a></p>
<p><a href="http://example.com/(c)">http://example.com/(c)</a></p>
.
6 changes: 3 additions & 3 deletions test/fixtures/markdown-it/normalize.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ Invalid punycode:
.
<http://xn--xn.com/>
.
<p><a href="http://xn--xn.com/">http://xnxn.com/</a></p>
<p><a href="http://xn--xn.com/">http://xn--xn.com/</a></p>
.

Invalid punycode (non-ascii):

.
<http://xn--γ.com/>
.
<p><a href="http://xn--xn---emd.com/">http://xnγ.com/</a></p>
<p><a href="http://xn--xn---emd.com/">http://xn--γ.com/</a></p>
.

Two slashes should start a domain:
Expand Down Expand Up @@ -96,5 +96,5 @@ test xn--n3h.net foo
.
test [email protected] foo
.
<p>test <a href="mailto:[email protected]">xnn3h@☃.net</a> foo</p>
<p>test <a href="mailto:[email protected]">xn--n3h@☃.net</a> foo</p>
.

0 comments on commit 9335394

Please sign in to comment.