From 510f657542830747492addf840337fbd7fe81b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Thu, 30 Jun 2022 13:57:52 +0100 Subject: [PATCH] Fix absolute URL detection when traversing markdown links (#1255) --- assets/js/lib/markdown.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/assets/js/lib/markdown.js b/assets/js/lib/markdown.js index 70b0311bf..ab0ac865d 100644 --- a/assets/js/lib/markdown.js +++ b/assets/js/lib/markdown.js @@ -134,13 +134,18 @@ function remarkExpandUrls(options) { return (ast) => { if (options.baseUrl) { visit(ast, "link", (node) => { - if (node.url && !isAbsoluteUrl(node.url) && !isPageAnchor(node.url)) { + if ( + node.url && + !isAbsoluteUrl(node.url) && + !isInternalUrl(node.url) && + !isPageAnchor(node.url) + ) { node.url = urlAppend(options.baseUrl, node.url); } }); visit(ast, "image", (node) => { - if (node.url && !isAbsoluteUrl(node.url)) { + if (node.url && !isAbsoluteUrl(node.url) && !isInternalUrl(node.url)) { node.url = urlAppend(options.baseUrl, node.url); } }); @@ -220,7 +225,7 @@ function rehypeExternalLinks(options) { } function isAbsoluteUrl(url) { - return url.startsWith("http") || url.startsWith("/"); + return /^(?:[a-z]+:)?\/\//i.test(url); } function isPageAnchor(url) {