Fix absolute URL detection when traversing markdown links (#1255)

This commit is contained in:
Jonatan Kłosko 2022-06-30 13:57:52 +01:00 committed by GitHub
parent bf6823240b
commit 510f657542
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -134,13 +134,18 @@ function remarkExpandUrls(options) {
return (ast) => { return (ast) => {
if (options.baseUrl) { if (options.baseUrl) {
visit(ast, "link", (node) => { 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); node.url = urlAppend(options.baseUrl, node.url);
} }
}); });
visit(ast, "image", (node) => { 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); node.url = urlAppend(options.baseUrl, node.url);
} }
}); });
@ -220,7 +225,7 @@ function rehypeExternalLinks(options) {
} }
function isAbsoluteUrl(url) { function isAbsoluteUrl(url) {
return url.startsWith("http") || url.startsWith("/"); return /^(?:[a-z]+:)?\/\//i.test(url);
} }
function isPageAnchor(url) { function isPageAnchor(url) {