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
+19 -11
View File
@@ -1,4 +1,5 @@
import type { Tokens } from "marked";
import type { Route } from "@app/lib/router";
import dompurify from "dompurify";
import katexMarkedExtension from "marked-katex-extension";
@@ -6,6 +7,8 @@ import markedLinkifyIt from "marked-linkify-it";
import { Marked, Renderer as BaseRenderer } from "marked";
import emojis from "@app/lib/emojis";
import { routeToPath } from "@app/lib/router";
import { canonicalize, isUrl } from "@app/lib/utils";
dompurify.setConfig({
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -101,16 +104,19 @@ const anchorMarkedExtension = {
};
export class Renderer extends BaseRenderer {
#baseUrl: string | undefined;
#route: Route;
#stripEmphasizedStyling: boolean | undefined;
/**
* If `baseUrl` is provided, all hrefs attributes in anchor tags, except those
* starting with `#`, are resolved with respect to `baseUrl`
*/
constructor(baseUrl: string | undefined, stripEmphasizedStyling: boolean) {
constructor(
activeUnloadedRoute: Route,
{ stripEmphasizedStyling }: { stripEmphasizedStyling: boolean },
) {
super();
this.#baseUrl = baseUrl;
this.#route = activeUnloadedRoute;
this.#stripEmphasizedStyling = stripEmphasizedStyling;
}
// Overwrites the rendering of heading tokens.
@@ -139,15 +145,17 @@ export class Renderer extends BaseRenderer {
if (href.startsWith("#")) {
// By lowercasing we avoid casing mismatches, between headings and links.
return `<a ${title ? `title="${title}"` : ""} href="${href.toLowerCase()}">${text}</a>`;
} else {
try {
href = new URL(href, this.#baseUrl).href;
} catch {
// Use original href value
}
return `<a ${title ? `title="${title}"` : ""} href="${href}">${text}</a>`;
}
if ("path" in this.#route && this.#route.path && !isUrl(href)) {
href = routeToPath({
...this.#route,
path: canonicalize(href, this.#route.path),
route: undefined,
});
}
return `<a ${title ? `title="${title}"` : ""} href="${href}">${text}</a>`;
}
}
+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);