fix: move linkout from marked to helper so that linkout is only shown if needed

This commit is contained in:
MiniDigger | Martin 2023-03-19 11:19:49 +01:00
parent 1f5d4ba0cb
commit 4f3980873c
2 changed files with 29 additions and 33 deletions

View File

@ -1,35 +1,7 @@
import { marked } from "marked";
import markedLinkifyIt from "marked-linkify-it";
import markedExtendedTables from "marked-extended-tables";
import { useBackendData } from "~/store/backendData";
const isSafeHost = (host: string) => {
for (const safeHost of useBackendData.security.safeDownloadHosts) {
// Make sure it's the full host or a subdomain
if (host === safeHost || host.endsWith("." + safeHost)) {
return true;
}
}
return false;
};
const isSafe = (urlString: string) => {
try {
const url = new URL(urlString);
const host = url.hostname;
if (url.protocol?.startsWith("mailto")) {
return true;
} else if (!host || isSafeHost(host)) {
return true;
}
} catch {}
return false;
};
const makeSafe = (urlString: string) => (isSafe(urlString) ? urlString : "/linkout?remoteUrl=" + encodeURIComponent(urlString));
const proxyImage = (urlString: string) => (isSafe(urlString) ? urlString : useBackendData.security.imageProxyUrl.replace("%s", urlString));
import { linkout, proxyImage } from "~/composables/useUrlHelper";
const renderer = {
heading(text: string, level: number) {
@ -47,7 +19,7 @@ const renderer = {
return `<img src="${proxyImage(href)}" alt="${alt}"` + (title ? ` title="${title}">` : ">");
},
link(href: string, title: string, text: string) {
return `<a href="${makeSafe(href)}"` + (title ? ` title="${title}">` : ">") + text + "</a>";
return `<a href="${linkout(href)}"` + (title ? ` title="${title}">` : ">") + text + "</a>";
},
};
marked.use({ renderer });

View File

@ -1,4 +1,5 @@
import { useConfig } from "~/lib/composables/useConfig";
import { useBackendData } from "~/store/backendData";
export function authUrl(user: string) {
return useConfig().authHost + "/" + user;
@ -13,6 +14,29 @@ export function forumUserUrl(name: number | string) {
return `https://forums.papermc.io/members/` + name;
}
export function linkout(url: string) {
return "/linkout?remoteUrl=" + encodeURIComponent(url);
}
const isSafeHost = (host: string) => {
for (const safeHost of useBackendData.security.safeDownloadHosts) {
// Make sure it's the full host or a subdomain
if (host === safeHost || host.endsWith("." + safeHost)) {
return true;
}
}
return false;
};
const isSafe = (urlString: string) => {
try {
const url = new URL(urlString);
const host = url.hostname;
if (url.protocol?.startsWith("mailto")) {
return true;
} else if (!host || isSafeHost(host)) {
return true;
}
} catch {}
return false;
};
export const linkout = (urlString: string) => (isSafe(urlString) ? urlString : "/linkout?remoteUrl=" + encodeURIComponent(urlString));
export const proxyImage = (urlString: string) => (isSafe(urlString) ? urlString : useBackendData.security.imageProxyUrl.replace("%s", urlString));