This commit is contained in:
Zef Hemel
2023-02-23 15:33:51 +01:00
parent 495012f796
commit c1528eff08
6 changed files with 88 additions and 11 deletions
+28 -1
View File
@@ -13,6 +13,8 @@ type MarkdownRenderOptions = {
annotationPositions?: true;
renderFrontMatter?: true;
attachmentUrlPrefix?: string;
// When defined, use to inline images as data: urls
inlineAttachments?: (url: string) => Promise<string>;
};
function cleanTags(values: (Tag | null)[]): Tag[] {
@@ -390,11 +392,36 @@ function render(
}
}
export function renderMarkdownToHtml(
async function traverseTag(
t: Tag,
fn: (t: Tag) => Promise<void>,
): Promise<void> {
await fn(t);
if (typeof t === "string") {
return;
}
if (t.body) {
for (const child of t.body) {
await traverseTag(child, fn);
}
}
}
export async function renderMarkdownToHtml(
t: ParseTree,
options: MarkdownRenderOptions = {},
) {
preprocess(t, options);
const htmlTree = posPreservingRender(t, options);
if (htmlTree && options.inlineAttachments) {
await traverseTag(htmlTree, async (t) => {
if (typeof t === "string") {
return;
}
if (t.name === "img") {
t.attrs!.src = await options.inlineAttachments!(t.attrs!.src!);
}
});
}
return renderHtml(htmlTree);
}