From 9599725e04fa0eaf3f25708ef530fe0dd231b889 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 1 Sep 2021 18:28:18 -0400 Subject: [PATCH] More markdown link heuristics --- lib/markdown.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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) + "..."; }