More markdown link heuristics

This commit is contained in:
Shadowfacts 2021-09-01 18:28:18 -04:00
parent 37c2c95881
commit 9599725e04
Signed by: 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) {
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) + "...";
}