Browse Source

Disable replacements inside autolinks

close https://github.com/markdown-it/markdown-it/issues/272
pull/293/head
Alex Kocharin 8 years ago
parent
commit
9335394a99
  1. 26
      lib/rules_core/replacements.js
  2. 2
      test/fixtures/markdown-it/linkify.txt
  3. 6
      test/fixtures/markdown-it/normalize.txt

26
lib/rules_core/replacements.js

@ -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, '±')
@ -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++;
}
}
}

2
test/fixtures/markdown-it/linkify.txt

@ -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
test/fixtures/markdown-it/normalize.txt

@ -32,7 +32,7 @@ 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):
@ -40,7 +40,7 @@ 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:
@ -96,5 +96,5 @@ test xn--n3h.net foo
.
test xn--n3h@xn--n3h.net foo
.
<p>test <a href="mailto:xn--n3h@xn--n3h.net">xnn3h@☃.net</a> foo</p>
<p>test <a href="mailto:xn--n3h@xn--n3h.net">xn--n3h@☃.net</a> foo</p>
.

Loading…
Cancel
Save