Handle absolute markdown links with leading slash

This commit is contained in:
Sebastian Martinez
2024-03-29 11:54:54 +01:00
committed by Rūdolfs Ošiņš
parent d6f3d17805
commit 2efd0ad58e
9 changed files with 68 additions and 56 deletions
+12 -8
View File
@@ -115,19 +115,23 @@ export function formatPublicExplorer(
.replace("$path", fullPath.replace(`/nodes/${host}/${rid}`, ""));
}
// Takes a path, eg. "../images/image.png", and a base from where to start resolving, e.g. "static/images/index.html".
// Returns the resolved path.
// Takes a path, eg. "../images/image.png", and a base from where to start
// resolving, e.g. "static/images/index.html". Returns the resolved path.
export function canonicalize(
path: string,
base: string,
origin = document.location.origin,
): string {
path = path.replace(/^\//, ""); // Remove leading slash
const finalPath = base
.split("/")
.slice(0, -1) // Remove file name.
.concat([path]) // Add image file path.
.join("/");
let finalPath: string | undefined;
if (path.startsWith("/")) {
finalPath = new URL(path, origin).pathname;
} else {
finalPath = base
.split("/")
.slice(0, -1) // Remove file name.
.concat([path]) // Add image file path.
.join("/");
}
// URL is used to resolve relative paths, eg. `../../assets/image.png`.
const url = new URL(finalPath, origin);