-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathmarkdown.tsx
54 lines (47 loc) · 1.2 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
51
52
53
54
import DOMPurify from "dompurify";
import MarkdownIt from "markdown-it";
import * as React from "react";
import { cn } from "@/lib/utils";
const md = new MarkdownIt({
html: true,
breaks: true,
linkify: true,
typographer: true,
quotes: "“”‘’",
});
export interface MarkdownProps extends React.HTMLAttributes<HTMLDivElement> {
content: string;
/**
* Whether to wrap the content in article tags with prose styling
* @default true
*/
prose?: boolean;
}
const Markdown = React.forwardRef<HTMLDivElement, MarkdownProps>(
({ className, content, prose = true, ...props }, ref) => {
const html = React.useMemo(() => {
const renderedHtml = md.render(content);
return DOMPurify.sanitize(renderedHtml);
}, [content]);
if (prose) {
return (
<article
ref={ref}
className={cn("prose max-w-none dark:prose-invert", className)}
dangerouslySetInnerHTML={{ __html: html }}
{...props}
/>
);
}
return (
<div
ref={ref}
className={className}
dangerouslySetInnerHTML={{ __html: html }}
{...props}
/>
);
},
);
Markdown.displayName = "Markdown";
export { Markdown };