Fixes #363
This commit is contained in:
@@ -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");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user