From ba193be13bd612e0ffa13af2783ac9e897c2904e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 17 Oct 2022 13:00:25 +0200 Subject: [PATCH] Expand relative URLs in HTML elements in Markdown (#1491) --- assets/js/lib/markdown.js | 42 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/assets/js/lib/markdown.js b/assets/js/lib/markdown.js index e54cf80c5..976d706d9 100644 --- a/assets/js/lib/markdown.js +++ b/assets/js/lib/markdown.js @@ -58,10 +58,10 @@ class Markdown { .use(remarkMath) .use(remarkPrepareMermaid) .use(remarkSyntaxHiglight, { highlight }) - .use(remarkExpandUrls, { baseUrl: this.baseUrl }) // We keep the HTML nodes, parse with rehype-raw and then sanitize .use(remarkRehype, { allowDangerousHtml: true }) .use(rehypeRaw) + .use(rehypeExpandUrls, { baseUrl: this.baseUrl }) .use(rehypeSanitize, sanitizeSchema()) .use(rehypeKatex) .use(rehypeMermaid) @@ -139,32 +139,38 @@ function remarkSyntaxHiglight(options) { // Expands relative URLs against the given base url // and deals with ".." in URLs -function remarkExpandUrls(options) { +function rehypeExpandUrls(options) { return (ast) => { if (options.baseUrl) { - visit(ast, "link", (node) => { - if ( - node.url && - !isAbsoluteUrl(node.url) && - !isInternalUrl(node.url) && - !isPageAnchor(node.url) - ) { - node.url = urlAppend(options.baseUrl, node.url); - } - }); + visit(ast, "element", (node) => { + if (node.tagName === "a" && node.properties) { + const url = node.properties.href; - visit(ast, "image", (node) => { - if (node.url && !isAbsoluteUrl(node.url) && !isInternalUrl(node.url)) { - node.url = urlAppend(options.baseUrl, node.url); + if ( + url && + !isAbsoluteUrl(url) && + !isInternalUrl(url) && + !isPageAnchor(url) + ) { + node.properties.href = urlAppend(options.baseUrl, url); + } + } + + if (node.tagName === "img" && node.properties) { + const url = node.properties.src; + + if (url && !isAbsoluteUrl(url) && !isInternalUrl(url)) { + node.properties.src = urlAppend(options.baseUrl, url); + } } }); } // Browser normalizes URLs with ".." so we use a "__parent__" // modifier instead and handle it on the server - visit(ast, "link", (node) => { - if (node.url) { - node.url = node.url + visit(ast, "element", (node) => { + if (node.tagName === "a" && node.properties && node.properties.href) { + node.properties.href = node.properties.href .split("/") .map((part) => (part === ".." ? "__parent__" : part)) .join("/");