Skip to content

Commit 35cb508

Browse files
committed
safe markdown renderer (#609, #403)
1 parent faf5e87 commit 35cb508

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

src/components/MarkdownRender/markdown-without-katex.jsx

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,73 @@ import remarkGfm from 'remark-gfm'
55
import remarkBreaks from 'remark-breaks'
66
import { Pre } from './Pre'
77
import { Hyperlink } from './Hyperlink'
8+
import { memo } from 'react'
89

910
export function MarkdownRender(props) {
1011
return (
1112
<div dir="auto">
1213
<ReactMarkdown
14+
allowedElements={[
15+
'div',
16+
'p',
17+
'span',
18+
19+
'video',
20+
'img',
21+
22+
'abbr',
23+
'acronym',
24+
'b',
25+
'blockquote',
26+
'code',
27+
'em',
28+
'i',
29+
'li',
30+
'ol',
31+
'ul',
32+
'strong',
33+
'table',
34+
'tr',
35+
'td',
36+
'th',
37+
38+
'details',
39+
'summary',
40+
'kbd',
41+
'samp',
42+
'sub',
43+
'sup',
44+
'ins',
45+
'del',
46+
'var',
47+
'q',
48+
'dl',
49+
'dt',
50+
'dd',
51+
'ruby',
52+
'rt',
53+
'rp',
54+
55+
'br',
56+
'hr',
57+
58+
'h1',
59+
'h2',
60+
'h3',
61+
'h4',
62+
'h5',
63+
'h6',
64+
65+
'thead',
66+
'tbody',
67+
'tfoot',
68+
'u',
69+
's',
70+
'a',
71+
'pre',
72+
'cite',
73+
]}
74+
unwrapDisallowed={true}
1375
remarkPlugins={[remarkGfm, remarkBreaks]}
1476
rehypePlugins={[
1577
rehypeRaw,
@@ -37,4 +99,4 @@ MarkdownRender.propTypes = {
3799
...ReactMarkdown.propTypes,
38100
}
39101

40-
export default MarkdownRender
102+
export default memo(MarkdownRender)

src/components/MarkdownRender/markdown.jsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,67 @@ export function MarkdownRender(props) {
1414
return (
1515
<div dir="auto">
1616
<ReactMarkdown
17+
allowedElements={[
18+
'div',
19+
'p',
20+
'span',
21+
22+
'video',
23+
'img',
24+
25+
'abbr',
26+
'acronym',
27+
'b',
28+
'blockquote',
29+
'code',
30+
'em',
31+
'i',
32+
'li',
33+
'ol',
34+
'ul',
35+
'strong',
36+
'table',
37+
'tr',
38+
'td',
39+
'th',
40+
41+
'details',
42+
'summary',
43+
'kbd',
44+
'samp',
45+
'sub',
46+
'sup',
47+
'ins',
48+
'del',
49+
'var',
50+
'q',
51+
'dl',
52+
'dt',
53+
'dd',
54+
'ruby',
55+
'rt',
56+
'rp',
57+
58+
'br',
59+
'hr',
60+
61+
'h1',
62+
'h2',
63+
'h3',
64+
'h4',
65+
'h5',
66+
'h6',
67+
68+
'thead',
69+
'tbody',
70+
'tfoot',
71+
'u',
72+
's',
73+
'a',
74+
'pre',
75+
'cite',
76+
]}
77+
unwrapDisallowed={true}
1778
remarkPlugins={[remarkMath, remarkGfm, remarkBreaks]}
1879
rehypePlugins={[
1980
rehypeKatex,

0 commit comments

Comments
 (0)