markdown-exit icon indicating copy to clipboard operation
markdown-exit copied to clipboard

Encode IDN hostnames with `URL` instead of punycode.js?

Open onmax opened this issue 4 weeks ago • 1 comments

Summary

Encode IDN hostnames in normalizeLink with URL instead of punycode.js.

Motivation

Node, browsers, Deno, Bun and Workers all have URL, which does the same job as punycode.toASCII. Using it for non-ASCII hosts shrinks markdown-exit/core:

before after change
minified 98.2 KiB 97.3 KiB -0.9 KiB
gzip 33.92 KiB 33.56 KiB -0.36 KiB

toUnicode still needs punycode.js, because browsers have nothing like it. Dropping punycode.js completely saves 0.99 KiB, but link text would show xn--n3h.net instead of ☃.net.

Proposed Solution

function toASCII(host: string) {
  return /[^\0-\x7F]/.test(host) ? new URL(`http://${host}`).hostname : host
}

One fixture changes, <http://xn--γ.com/> in normalize.txt, which is an invalid host either way. URL also maps hosts the way browsers do, so Ä.com becomes xn--4ca.com instead of xn--7ba.com.

It's a small win. Would you take a PR?

Would you like to work on this new feature?

  • [x] I'm interested in working on this new feature

onmax avatar Nov 05 '26 19:11 onmax

Tried this: the 0.36 KiB only shows up with import { toUnicode } from 'punycode.js', which breaks in plain Node because punycode.js is CommonJS. With the default import nothing gets tree-shaken, so this saves nothing. Closing, sorry for the noise!

onmax avatar Nov 09 '26 18:11 onmax