cg / hackerproducts
63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
root / src / lib / markdown.ts
src/lib/markdown.ts · 28 lines · 728 B← back to folder
| 1 | import { unified } from "unified"; |
| 2 | import remarkParse from "remark-parse"; |
| 3 | import remarkGfm from "remark-gfm"; |
| 4 | import remarkRehype from "remark-rehype"; |
| 5 | import rehypeSanitize, { defaultSchema } from "rehype-sanitize"; |
| 6 | import rehypeStringify from "rehype-stringify"; |
| 7 | |
| 8 | const sanitizeSchema = { |
| 9 | ...defaultSchema, |
| 10 | protocols: { |
| 11 | ...defaultSchema.protocols, |
| 12 | href: ["http", "https", "mailto"], |
| 13 | src: ["http", "https"], |
| 14 | }, |
| 15 | }; |
| 16 | |
| 17 | export async function renderMarkdown(markdown: string): Promise<string> { |
| 18 | const file = await unified() |
| 19 | .use(remarkParse) |
| 20 | .use(remarkGfm) |
| 21 | .use(remarkRehype) |
| 22 | .use(rehypeSanitize, sanitizeSchema) |
| 23 | .use(rehypeStringify) |
| 24 | .process(markdown || ""); |
| 25 | |
| 26 | return String(file); |
| 27 | } |
| 28 |