Browse Source

Punycode domain names only in known schemas

In urls like `skype:XXX` it prevents XXX to be parsed as domain
name and encoded with punycode.
pull/82/head
Alex Kocharin 10 years ago
parent
commit
b05bfd6e8b
  1. 18
      lib/index.js
  2. 16
      test/fixtures/markdown-it/normalize.txt

18
lib/index.js

@ -34,14 +34,24 @@ function validateLink(url) {
return true;
}
var RECODE_HOSTNAME_FOR = [ 'http:', 'https:', 'mailto:' ];
function normalizeLink(url) {
var parsed = mdurl.parse(url, true);
if (parsed.hostname) {
// Encode hostnames in urls like:
// `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
//
// We don't encode unknown schemas, because it's likely that we encode
// something we shouldn't (e.g. `skype:name` treated as `skype:host`)
//
if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
try {
parsed.hostname = punycode.toASCII(parsed.hostname);
} catch(er) {}
}
}
return mdurl.encode(mdurl.format(parsed));
}
@ -50,10 +60,18 @@ function normalizeLinkText(url) {
var parsed = mdurl.parse(url, true);
if (parsed.hostname) {
// Encode hostnames in urls like:
// `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
//
// We don't encode unknown schemas, because it's likely that we encode
// something we shouldn't (e.g. `skype:name` treated as `skype:host`)
//
if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
try {
parsed.hostname = punycode.toUnicode(parsed.hostname);
} catch(er) {}
}
}
return mdurl.decode(mdurl.format(parsed));
}

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

@ -43,6 +43,22 @@ Invalid punycode (non-ascii):
<p><a href="http://xn--xn---emd.com/">http://xn–γ.com/</a></p>
.
Two slashes should start a domain:
.
[](//☃.net/)
.
<p><a href="//xn--n3h.net/"></a></p>
.
Don't encode domains in unknown schemas:
.
[](skype:γγγ)
.
<p><a href="skype:%CE%B3%CE%B3%CE%B3"></a></p>
.
Should auto-add protocol to autolinks:
.

Loading…
Cancel
Save