This commit is contained in:
Zef Hemel
2023-12-19 17:20:47 +01:00
parent e8acd1dbfb
commit a5c7405a35
6 changed files with 34 additions and 11 deletions
+3 -3
View File
@@ -16,7 +16,7 @@ export type MarkdownRenderOptions = {
attachmentUrlPrefix?: string;
preserveAttributes?: true;
// When defined, use to inline images as data: urls
translateUrls?: (url: string) => string;
translateUrls?: (url: string, type: "link" | "image") => string;
};
function cleanTags(values: (Tag | null)[], cleanWhitespace = false): Tag[] {
@@ -466,11 +466,11 @@ export function renderMarkdownToHtml(
return;
}
if (t.name === "img") {
t.attrs!.src = options.translateUrls!(t.attrs!.src!);
t.attrs!.src = options.translateUrls!(t.attrs!.src!, "image");
}
if (t.name === "a" && t.attrs!.href) {
t.attrs!.href = options.translateUrls!(t.attrs!.href);
t.attrs!.href = options.translateUrls!(t.attrs!.href, "link");
}
});
}
+8 -2
View File
@@ -2,6 +2,7 @@ import { asset, clientStore, editor, markdown, system } from "$sb/syscalls.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts";
import { resolvePath } from "$sb/lib/resolve.ts";
import { expandCodeWidgets } from "./api.ts";
import { folderName, resolve } from "../../common/path.ts";
export async function updateMarkdownPreview() {
if (!(await clientStore.get("enableMarkdownPreview"))) {
@@ -18,9 +19,14 @@ export async function updateMarkdownPreview() {
const html = renderMarkdownToHtml(mdTree, {
smartHardBreak: true,
annotationPositions: true,
translateUrls: (url) => {
translateUrls: (url, type) => {
if (!url.includes("://")) {
url = resolvePath(currentPage, decodeURI(url), true);
if (type === "image" && !url.startsWith("/")) {
// Make relative to current folder
url = resolve(folderName(currentPage), decodeURI(url));
} else if (type === "link") { // link
url = resolvePath(currentPage, decodeURI(url), true);
}
}
return url;
},