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