Skip to content

Commit 5f54455

Browse files
committed
fix(type): Fix upgrade react-markdown v6 error.
1 parent ac753b1 commit 5f54455

File tree

3 files changed

+50
-51
lines changed

3 files changed

+50
-51
lines changed

README.md

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,59 +51,63 @@ type MarkdownPreviewProps = {
5151
} & ReactMarkdownProps;
5252
```
5353

54-
This [`ReactMarkdownProps`](https://github.com/remarkjs/react-markdown/blob/22bb78747d768181cb9ea8711b5e13c3768921d8/index.d.ts#L32-L84) details.
55-
5654
- `source` (`string`, default: `''`)\
5755
Markdown to parse
5856
- `className` (`string?`)\
5957
Wrap the markdown in a `div` with this class name
60-
- `allowDangerousHtml` (`boolean`, default: `false`)\
61-
This project is safe by default and escapes HTML.
62-
Use `allowDangerousHtml: true` to allow dangerous html instead.
63-
See [security](https://github.com/remarkjs/react-markdown/tree/22bb78747d768181cb9ea8711b5e13c3768921d8#security)
58+
59+
This [`ReactMarkdownProps`](https://github.com/remarkjs/react-markdown/tree/15b4757082cf3f32a25eba0b8ea30baf751a8b40#props) details.
60+
61+
- `children` (`string`, default: `''`)\
62+
Markdown to parse
63+
- `className` (`string?`)\
64+
Wrap the markdown in a `div` with this class name
6465
- `skipHtml` (`boolean`, default: `false`)\
65-
Ignore HTML in Markdown
66+
Ignore HTML in Markdown completely
6667
- `sourcePos` (`boolean`, default: `false`)\
67-
Pass a prop to all renderers with a serialized position
68+
Pass a prop to all components with a serialized position
6869
(`data-sourcepos="3:1-3:13"`)
6970
- `rawSourcePos` (`boolean`, default: `false`)\
70-
Pass a prop to all renderers with their [position](https://github.com/syntax-tree/unist#position)
71+
Pass a prop to all components with their [position][]
7172
(`sourcePosition: {start: {line: 3, column: 1}, end:…}`)
72-
- `includeNodeIndex` (`boolean`, default: `false`)\
73-
Pass [`index`](https://github.com/syntax-tree/unist#index) and `parentChildCount` in props to all renderers
74-
- `allowedTypes` (`Array.<string>`, default: list of all types)\
75-
Node types to allow (can’t combine w/ `disallowedTypes`).
76-
All types are available at `ReactMarkdown.types`
77-
- `disallowedTypes` (`Array.<string>`, default: `[]`)\
78-
Node types to disallow (can’t combine w/ `allowedTypes`)
79-
- `allowNode` (`(node, index, parent) => boolean?`, optional)\
80-
Function called to check if a node is allowed (when truthy) or not.
81-
`allowedTypes` / `disallowedTypes` is used first!
73+
- `includeElementIndex` (`boolean`, default: `false`)\
74+
Pass the `index` (number of elements before it) and `siblingCount` (number
75+
of elements in parent) as props to all components
76+
- `allowedElements` (`Array.<string>`, default: `undefined`)\
77+
Tag names to allow (can’t combine w/ `disallowedElements`).
78+
By default all elements are allowed
79+
- `disallowedElements` (`Array.<string>`, default: `undefined`)\
80+
Tag names to disallow (can’t combine w/ `allowedElements`).
81+
By default no elements are disallowed
82+
- `allowElement` (`(element, index, parent) => boolean?`, optional)\
83+
Function called to check if an element is allowed (when truthy) or not.
84+
`allowedElements` / `disallowedElements` is used first!
8285
- `unwrapDisallowed` (`boolean`, default: `false`)\
83-
Extract (unwrap) the children of not allowed nodes.
84-
By default, when `strong` is not allowed, it and it’s content is dropped,
85-
but with `unwrapDisallowed` the node itself is dropped but the content used
86-
- `linkTarget` (`string` or `(url, text, title) => string`, optional)\
86+
Extract (unwrap) the children of not allowed elements.
87+
By default, when `strong` is not allowed, it and it’s children is dropped,
88+
but with `unwrapDisallowed` the element itself is dropped but the children
89+
used
90+
- `linkTarget` (`string` or `(href, children, title) => string`, optional)\
8791
Target to use on links (such as `_blank` for `<a target="_blank"…`)
88-
- `transformLinkUri` (`(uri) => string`, default:
92+
- `transformLinkUri` (`(href, children, title) => string`, default:
8993
[`./uri-transformer.js`][uri], optional)\
9094
URL to use for links.
9195
The default allows only `http`, `https`, `mailto`, and `tel`, and is
9296
available at `ReactMarkdown.uriTransformer`.
9397
Pass `null` to allow all URLs.
94-
See [security](https://github.com/remarkjs/react-markdown/tree/22bb78747d768181cb9ea8711b5e13c3768921d8#security)
95-
- `transformImageUri` (`(uri) => string`, default:
98+
See [security][]
99+
- `transformImageUri` (`(src, alt, title) => string`, default:
96100
[`./uri-transformer.js`][uri], optional)\
97101
Same as `transformLinkUri` but for images
98-
- `renderers` (`Object.<Component>`, default: `{}`)\
99-
Object mapping node types to React components.
100-
Merged with the default renderers (available at `ReactMarkdown.renderers`).
101-
Which props are passed varies based on the node
102-
- `plugins` (`Array.<Plugin>`, default: `[]`)\
103-
List of [remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md#list-of-plugins) to use.
102+
- `components` (`Object.<string, Component>`, default: `{}`)\
103+
Object mapping tag names to React components
104+
- `remarkPlugins` (`Array.<Plugin>`, default: `[]`)\
105+
List of [remark plugins][remark-plugins] to use.
106+
See the next section for examples on how to pass options
107+
- `rehypePlugins` (`Array.<Plugin>`, default: `[]`)\
108+
List of [rehype plugins][rehype-plugins] to use.
104109
See the next section for examples on how to pass options
105110

106-
See [Options Props](https://github.com/remarkjs/react-markdown/tree/22bb78747d768181cb9ea8711b5e13c3768921d8#props) for more details.
107111

108112
## Development
109113

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"@babel/runtime": "7.13.10",
4848
"@types/prismjs": "1.16.4",
4949
"prismjs": "1.23.0",
50+
"rehype-sanitize": "4.0.0",
51+
"rehype-raw": "5.1.0",
5052
"remark-gfm": "1.0.0",
5153
"react-markdown": "6.0.1"
5254
},

src/index.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React, { useEffect, useImperativeHandle } from 'react';
2-
import ReactMarkdown, { ReactMarkdownProps } from 'react-markdown';
2+
import ReactMarkdown from 'react-markdown';
33
import gfm from 'remark-gfm';
4+
import rehypeRaw from 'rehype-raw';
5+
import rehypeSanitize from 'rehype-sanitize';
46
import Prism from 'prismjs';
57
import 'prismjs/components/prism-markup';
68
import { loadLang } from './langs';
@@ -13,7 +15,7 @@ export type MarkdownPreviewProps = {
1315
style?: React.CSSProperties;
1416
onScroll?: (e: React.UIEvent<HTMLDivElement>) => void;
1517
onMouseOver?: (e: React.MouseEvent<HTMLDivElement>) => void;
16-
} & ReactMarkdownProps;
18+
} & Omit<ReactMarkdown.ReactMarkdownOptions, 'children'>;
1719

1820
export type MarkdownPreviewRef = {
1921
mdp: React.RefObject<HTMLDivElement>;
@@ -50,24 +52,15 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
5052
useImperativeHandle(ref, () => ({ ...props, lang: loadedLang.current, mdp }), [mdp, props]);
5153

5254
const cls = `wmde-markdown wmde-markdown-color ${className || ''}`;
53-
const reactMarkdownProps = {
54-
allowDangerousHtml: true,
55-
allowNode: (node, index, parent) => {
56-
if (node.type === 'html' && reactMarkdownProps.allowDangerousHtml) {
57-
// filter style
58-
node.value = (node.value as string).replace(/<((style|script|link|input|form)|\/(style|script|link|input|form))(\s?[^>]*>)/gi, (a: string) => {
59-
return a.replace(/[<>]/g, (e: string) => (({ '<': '&lt;', '>': '&gt;' } as { [key: string]: string })[e]))
60-
});
61-
}
62-
return true;
63-
},
64-
...other,
65-
plugins: [gfm, ...(other.plugins || [])],
66-
source: source || '',
67-
} as ReactMarkdownProps;
6855
return (
6956
<div ref={mdp} onScroll={onScroll} onMouseOver={onMouseOver} className={cls} style={style}>
70-
<ReactMarkdown {...reactMarkdownProps} />
57+
<ReactMarkdown
58+
{...other}
59+
plugins={[gfm, ...(other.plugins || [])]}
60+
rehypePlugins={[rehypeRaw, rehypeSanitize, ...(other.rehypePlugins || [])]}
61+
>
62+
{source || ''}
63+
</ReactMarkdown>
7164
</div>
7265
);
7366
});

0 commit comments

Comments
 (0)