forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.ts
More file actions
28 lines (26 loc) · 987 Bytes
/
Copy pathmarkdown.ts
File metadata and controls
28 lines (26 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
import remarkRehype from "remark-rehype";
import rehypeSanitize from "rehype-sanitize";
import rehypeStringify from "rehype-stringify";
/**
* Renders problem-statement Markdown to sanitized HTML on the server — no
* client-side Markdown library. `rehype-sanitize` runs last, so the result is
* safe to inject with `dangerouslySetInnerHTML` even though statements are
* authored content. GFM (tables, strikethrough, task lists) is supported. The
* pipeline is pure JS, so it runs on the Cloudflare Workers runtime.
*/
const processor = unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeSanitize)
.use(rehypeStringify)
.freeze();
/** Markdown → sanitized HTML string. */
export async function renderMarkdown(md: string): Promise<string> {
if (!md) return "";
const file = await processor.process(md);
return String(file);
}