Skip to content

Commit 01f4b5d

Browse files
docs: show mixed HTML and HTML sanitizing
1 parent ed4b5ab commit 01f4b5d

File tree

4 files changed

+101
-5
lines changed

4 files changed

+101
-5
lines changed

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
"react-is": "^16.13.1",
5757
"react-test-renderer": "^16.13.1",
5858
"rehype-katex": "^3.0.0",
59+
"rehype-raw": "^4.0.2",
60+
"rehype-sanitize": "^3.0.1",
5961
"remark-math": "^2.0.1",
6062
"ts-loader": "^7.0.4",
6163
"tsdx": "^0.13.2",

stories/remark-component.stories.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from 'react';
22
import { text } from '@storybook/addon-knobs';
33
import remarkMath from 'remark-math';
44
import rehypeKatex from 'rehype-katex';
5+
import rehypeRaw from 'rehype-raw';
6+
import rehypeSanitize from 'rehype-sanitize';
57
import 'katex/dist/katex.min.css';
68

79
import { Remark } from '../src';
@@ -11,7 +13,7 @@ export default {
1113
component: Remark,
1214
};
1315

14-
export const Default = () => (
16+
export const PlainMarkdown = () => (
1517
<Remark>
1618
{text(
1719
'content',
@@ -26,7 +28,7 @@ export const Default = () => (
2628
</Remark>
2729
);
2830

29-
export const Math = () => (
31+
export const MarkdownWithMath = () => (
3032
<Remark remarkPlugins={[remarkMath]} rehypePlugins={[rehypeKatex]}>
3133
{text(
3234
'content',
@@ -38,3 +40,28 @@ $$`
3840
)}
3941
</Remark>
4042
);
43+
44+
export const MixedHTMLSanitized = () => (
45+
<Remark
46+
remarkToRehypeOptions={{ allowDangerousHTML: true }}
47+
rehypePlugins={[rehypeRaw, rehypeSanitize]}
48+
>
49+
{text(
50+
'content',
51+
`# header
52+
53+
<strong>mixed</strong>
54+
<em>with</em>
55+
<kbd>html</kbd>
56+
`
57+
)}
58+
</Remark>
59+
);
60+
61+
MixedHTMLSanitized.story = {
62+
parameters: {
63+
knobs: {
64+
escapeHTML: false,
65+
},
66+
},
67+
};

stories/remark-hook.stories.tsx

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useEffect } from 'react';
22
import { text } from '@storybook/addon-knobs';
33
import remarkMath from 'remark-math';
44
import rehypeKatex from 'rehype-katex';
5+
import rehypeRaw from 'rehype-raw';
6+
import rehypeSanitize from 'rehype-sanitize';
57
import 'katex/dist/katex.min.css';
68

79
import { useRemark } from '../src';
@@ -11,9 +13,18 @@ export default {
1113
component: useRemark,
1214
};
1315

14-
export const Default = () => {
16+
export const PlainMarkdown = () => {
1517
const [reactContent, setMarkdownSource] = useRemark();
16-
const markdownSource = text('markdown', '# header\n* list');
18+
const markdownSource = text(
19+
'markdown',
20+
`# header
21+
22+
1. ordered
23+
2. list
24+
25+
* unordered
26+
* list`
27+
);
1728

1829
useEffect(() => {
1930
setMarkdownSource(markdownSource);
@@ -22,7 +33,7 @@ export const Default = () => {
2233
return reactContent;
2334
};
2435

25-
export const Math = () => {
36+
export const MarkdownWithMath = () => {
2637
const [reactContent, setMarkdownSource] = useRemark({
2738
remarkPlugins: [remarkMath],
2839
rehypePlugins: [rehypeKatex],
@@ -42,3 +53,32 @@ export const Math = () => {
4253

4354
return reactContent;
4455
};
56+
57+
export const MixedHTMLSanitized = () => {
58+
const [reactContent, setMarkdownSource] = useRemark({
59+
remarkToRehypeOptions: { allowDangerousHTML: true },
60+
rehypePlugins: [rehypeRaw, rehypeSanitize],
61+
});
62+
const markdownSource = text(
63+
'markdown',
64+
`# header
65+
66+
<strong>mixed</strong>
67+
<em>with</em>
68+
<kbd>html</kbd>`
69+
);
70+
71+
useEffect(() => {
72+
setMarkdownSource(markdownSource);
73+
}, [markdownSource]);
74+
75+
return reactContent;
76+
};
77+
78+
MixedHTMLSanitized.story = {
79+
parameters: {
80+
knobs: {
81+
escapeHTML: false,
82+
},
83+
},
84+
};

0 commit comments

Comments
 (0)