yap-blog/src/lib/markdown.ts
2026-07-02 21:32:33 -04:00

30 lines
1,019 B
TypeScript

import rehypeRaw from "rehype-raw";
import rehypeSanitize from "rehype-sanitize";
import rehypeStringify from "rehype-stringify";
import remarkGfm from "remark-gfm";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
import { contentSchema } from "./sanitize-schema";
/**
* GitHub-flavored Markdown -> sanitized HTML.
*
* Bodies are stored as HTML (see src/lib/html.ts), so this pipeline now
* serves two jobs: converting markdown pasted into the editor and
* converting the markdown-authored seed content at seed time. It shares
* the same sanitize schema as the storage pipeline, so both routes admit
* exactly the same HTML.
*/
const processor = unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeSanitize, contentSchema)
.use(rehypeStringify);
export function renderMarkdown(markdown: string): string {
return processor.processSync(markdown).toString();
}