diff --git a/lib/markdown.ts b/lib/markdown.ts index 9599e09..992a0f1 100644 --- a/lib/markdown.ts +++ b/lib/markdown.ts @@ -58,8 +58,23 @@ md.renderer.rules.link_open = function(tokens, index, options, env, self) { if (href != null) { try { const parsed = new URL(href); + let hostname = parsed.hostname; + + // hide wwww from beginning of domain + if (hostname.startsWith("www.")) { + hostname = hostname.substr(4); + } + + // if the path is /, omit it const formattedPathname = parsed.pathname === "/" ? "" : parsed.pathname; - let formatted = `${parsed.hostname}${formattedPathname}`; + + let formatted = `${hostname}${formattedPathname}`; + + if (hostname.endsWith("youtube.com") && formattedPathname == "/watch") { + // otherwise yt video links end up as `youtube.com/watch` which looks weird + formatted += parsed.search; + } + if (formatted.length > 40) { formatted = formatted.substr(0, 40) + "..."; }