Add option to compile markdown

This commit is contained in:
Endeavorance 2025-03-21 14:58:26 -04:00
parent e7218143ec
commit c1f3c6cade
7 changed files with 62 additions and 237 deletions

View file

@ -6,9 +6,9 @@ import type { BoundPlaybill } from "./binding";
import remarkWikiLink from "remark-wiki-link";
import { toSlug } from "./slug";
import rehypeRaw from "rehype-raw";
import rehypeSanitize from "rehype-sanitize";
import remarkGfm from "remark-gfm";
import rehypeShiftHeading from "rehype-shift-heading";
import type { TaggedComponent } from "@proscenium/playbill";
export type MarkdownParserFunction = (input: string) => Promise<string>;
@ -37,11 +37,43 @@ export function createMarkdownRenderer(
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeShiftHeading, { shift: 1 })
.use(rehypeSanitize)
.use(rehypeStringify);
// TODO: Add sanitization
return async (input: string): Promise<string> => {
const parsed = await parser.process(input);
return String(parsed);
};
}
export async function compileMarkdownInPlaybill(
boundPlaybill: BoundPlaybill,
): Promise<BoundPlaybill> {
const renderMarkdown = createMarkdownRenderer(boundPlaybill);
const playbill = boundPlaybill.playbill;
// Define a processor function to iterate over all components and process their markdown
const processMarkdownInComponent = async (entry: TaggedComponent) => {
entry.component.description = await renderMarkdown(
entry.component.description,
);
if (entry.type === "resource" && entry.component.type === "table") {
const newData: string[][] = [];
for (const row of entry.component.data) {
const newRow: string[] = [];
for (const cell of row) {
newRow.push(await renderMarkdown(cell));
}
newData.push(newRow);
}
entry.component.data = newData;
}
};
// Process all components
await playbill.processComponents(processMarkdownInComponent);
return boundPlaybill;
}