More markdown link heuristics

This commit is contained in:
Shadowfacts 2021-09-01 18:28:18 -04:00
parent 37c2c95881
commit 9599725e04
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 16 additions and 1 deletions

View File

@ -58,8 +58,23 @@ md.renderer.rules.link_open = function(tokens, index, options, env, self) {
if (href != null) { if (href != null) {
try { try {
const parsed = new URL(href); 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; 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) { if (formatted.length > 40) {
formatted = formatted.substr(0, 40) + "..."; formatted = formatted.substr(0, 40) + "...";
} }