-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markdown.tsx
50 lines (42 loc) · 1.28 KB
/
Markdown.tsx
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { fromMarkdown } from "mdast-util-from-markdown";
import { gfm } from "micromark-extension-gfm";
import { gfmFromMarkdown } from "mdast-util-gfm";
import { toHast } from "mdast-util-to-hast";
import { type Options, toJsxRuntime } from "hast-util-to-jsx-runtime";
import { raw } from "hast-util-raw";
import { Fragment, jsx, jsxDEV, jsxs } from "@http/jsx-stream/jsx-runtime";
import { fetchContent } from "../lib/content.ts";
import type { Promisable } from "@http/jsx-stream/types";
interface Props {
url?: string;
children?: Promisable<string>;
}
const options = {
Fragment,
jsx,
jsxs,
jsxDEV,
elementAttributeNameCase: "html",
stylePropertyNameCase: "css",
} as Options;
export async function Markdown(props: Props) {
let markdown: string | undefined;
if (props.url) {
const response = await fetchContent(props.url);
if (response.ok) {
markdown = await response.text();
}
}
if (!markdown) {
markdown = await props.children;
}
const mdast = markdown
? fromMarkdown(markdown, {
extensions: [gfm()],
mdastExtensions: [gfmFromMarkdown()],
})
: undefined;
let hast = mdast ? toHast(mdast, { allowDangerousHtml: true }) : undefined;
hast = hast ? raw(hast) : undefined;
return hast ? toJsxRuntime(hast, options) : null;
}