diff --git a/lib/rules_core/linkify.js b/lib/rules_core/linkify.js index 33c77b9..f3fd9d7 100644 --- a/lib/rules_core/linkify.js +++ b/lib/rules_core/linkify.js @@ -17,7 +17,8 @@ function isLinkClose(str) { module.exports = function linkify(state) { - var i, j, l, tokens, token, currentToken, nodes, ln, text, pos, lastPos, level, htmlLinkLevel, url, fullUrl, + var i, j, l, tokens, token, currentToken, nodes, ln, text, pos, lastPos, + level, htmlLinkLevel, url, fullUrl, urlText, blockTokens = state.tokens, links; @@ -74,6 +75,20 @@ module.exports = function linkify(state) { fullUrl = state.md.normalizeLink(url); if (!state.md.validateLink(fullUrl)) { continue; } + urlText = links[ln].text; + + // Linkifier might send raw hostnames like "example.com", where url + // starts with domain name. So we prepend http:// in those cases, + // and remove it afterwards. + // + if (!links[ln].schema) { + urlText = state.md.normalizeLinkText('http://' + urlText).replace(/^http:\/\//, ''); + } else if (links[ln].schema === 'mailto:' && !/^mailto:/i.test(urlText)) { + urlText = state.md.normalizeLinkText('mailto:' + urlText).replace(/^mailto:/, ''); + } else { + urlText = state.md.normalizeLinkText(urlText); + } + pos = links[ln].index; if (pos > lastPos) { @@ -89,7 +104,7 @@ module.exports = function linkify(state) { nodes.push(token); token = new state.Token('text', '', 0); - token.content = state.md.normalizeLinkText(links[ln].text); + token.content = urlText; token.level = level; nodes.push(token); diff --git a/test/fixtures/markdown-it/normalize.txt b/test/fixtures/markdown-it/normalize.txt index c59165d..296b296 100644 --- a/test/fixtures/markdown-it/normalize.txt +++ b/test/fixtures/markdown-it/normalize.txt @@ -80,3 +80,21 @@ test http://☃.net/ foo .

test http://☃.net/ foo

. + +. +test //xn--n3h.net/ foo +. +

test //☃.net/ foo

+. + +. +test xn--n3h.net foo +. +

test ☃.net foo

+. + +. +test xn--n3h@xn--n3h.net foo +. +

test xn–n3h@☃.net foo

+.