Browse Source

Fix protocol-less urls from linkifier

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.
pull/82/head
Alex Kocharin 10 years ago
parent
commit
141308d1a7
  1. 19
      lib/rules_core/linkify.js
  2. 18
      test/fixtures/markdown-it/normalize.txt

19
lib/rules_core/linkify.js

@ -17,7 +17,8 @@ function isLinkClose(str) {
module.exports = function linkify(state) { 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, blockTokens = state.tokens,
links; links;
@ -74,6 +75,20 @@ module.exports = function linkify(state) {
fullUrl = state.md.normalizeLink(url); fullUrl = state.md.normalizeLink(url);
if (!state.md.validateLink(fullUrl)) { continue; } 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; pos = links[ln].index;
if (pos > lastPos) { if (pos > lastPos) {
@ -89,7 +104,7 @@ module.exports = function linkify(state) {
nodes.push(token); nodes.push(token);
token = new state.Token('text', '', 0); token = new state.Token('text', '', 0);
token.content = state.md.normalizeLinkText(links[ln].text); token.content = urlText;
token.level = level; token.level = level;
nodes.push(token); nodes.push(token);

18
test/fixtures/markdown-it/normalize.txt

@ -80,3 +80,21 @@ test http://☃.net/ foo
. .
<p>test <a href="http://xn--n3h.net/">http://☃.net/</a> foo</p> <p>test <a href="http://xn--n3h.net/">http://☃.net/</a> foo</p>
. .
.
test //xn--n3h.net/ foo
.
<p>test <a href="//xn--n3h.net/">//☃.net/</a> foo</p>
.
.
test xn--n3h.net foo
.
<p>test <a href="http://xn--n3h.net">☃.net</a> foo</p>
.
.
test xn--n3h@xn--n3h.net foo
.
<p>test <a href="mailto:xn--n3h@xn--n3h.net">xn–n3h@☃.net</a> foo</p>
.

Loading…
Cancel
Save