Skip to content

Commit eb73b91

Browse files
authored
Deprecate source, escapeHtml options
Closes remarkjsGH-546. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
1 parent b359173 commit eb73b91

8 files changed

Lines changed: 45 additions & 63 deletions

File tree

β€Žindex.d.tsβ€Ž

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ declare namespace ReactMarkdown {
4848
readonly unwrapDisallowed?: boolean
4949
}
5050

51-
interface SourceProp {
52-
/** @deprecated use children */
53-
readonly source: string
54-
}
55-
5651
interface ChildrenProp {
5752
readonly children: string
5853
}
@@ -69,19 +64,14 @@ declare namespace ReactMarkdown {
6964
readonly allowDangerousHtml?: boolean
7065
}
7166

72-
interface EscapeHtmlProp {
73-
/** @deprecated use allowDangerousHtml */
74-
readonly escapeHtml?: boolean
75-
}
76-
7767
interface SkipHtmlProp {
7868
readonly skipHtml?: boolean
7969
}
8070

8171
type ReactMarkdownProps = ReactMarkdownPropsBase &
82-
MutuallyExclusive<ChildrenProp, SourceProp> &
72+
ChildrenProp &
8373
MutuallyExclusive<AllowedTypesProp, DisallowedTypesProp> &
84-
MutuallyExclusive<EscapeHtmlProp, MutuallyExclusive<SkipHtmlProp, AllowDangerousHtmlProp>>
74+
MutuallyExclusive<SkipHtmlProp, AllowDangerousHtmlProp>
8575

8676
const types: NodeType[]
8777
const renderers: Renderers

β€Žreact-markdown-types-test.tsxβ€Ž

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ import * as ReactDom from 'react-dom'
33
import * as ReactMarkdown from 'react-markdown'
44
import * as ReactMarkdownWitHtml from 'react-markdown/with-html'
55

6-
/* must have children or source, but not both and not neither */
6+
/* must have children */
77
let test = <ReactMarkdown># header</ReactMarkdown>
88
test = <ReactMarkdown children="# header" />
9-
test = <ReactMarkdown source="# header"></ReactMarkdown>
109
// $ExpectError
1110
test = <ReactMarkdown />
12-
// $ExpectError
13-
test = <ReactMarkdown source="# header"># header</ReactMarkdown>
14-
// $ExpectError
15-
test = <ReactMarkdown source="# header" children="# header" />
1611

1712
/* should support allowedTypes or disallowedTypes, but not both */
1813
test = <ReactMarkdown allowedTypes={['heading']}># header</ReactMarkdown>
@@ -34,13 +29,12 @@ test = (
3429
</ReactMarkdown>
3530
)
3631

37-
/* should support skipHtml or escapeHtml, but not both */
38-
test = <ReactMarkdown escapeHtml># header</ReactMarkdown>
32+
/* should support skipHtml or allowDangerousHtml, but not both */
3933
test = <ReactMarkdown skipHtml># header</ReactMarkdown>
4034
test = <ReactMarkdown allowDangerousHtml># header</ReactMarkdown>
4135
test = (
4236
// $ExpectError
43-
<ReactMarkdown escapeHtml skipHtml allowDangerousHtml>
37+
<ReactMarkdown skipHtml allowDangerousHtml>
4438
# header
4539
</ReactMarkdown>
4640
)

β€Žsrc/ast-to-react.jsβ€Ž

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ function getNodeProps(node, key, opts, renderer, parent, index) {
165165
// @todo find a better way than this
166166
props.isBlock = node.position.start.line !== node.position.end.line
167167
props.allowDangerousHtml = opts.allowDangerousHtml
168-
props.escapeHtml = opts.escapeHtml
169168
props.skipHtml = opts.skipHtml
170169
break
171170
case 'parsedHtml': {
@@ -174,7 +173,6 @@ function getNodeProps(node, key, opts, renderer, parent, index) {
174173
parsedChildren = node.children.map((child, i) => astToReact(child, opts, {node, props}, i))
175174
}
176175
props.allowDangerousHtml = opts.allowDangerousHtml
177-
props.escapeHtml = opts.escapeHtml
178176
props.skipHtml = opts.skipHtml
179177
props.element = node.element ? mergeNodeChildren(node, parsedChildren) : null
180178
break

β€Žsrc/plugins/html-parser.jsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function parseHtml(config, tree, props) {
3434
tree,
3535
'html',
3636
(node, index, parent) => {
37-
if (!props.allowDangerousHtml && props.escapeHtml !== false) {
37+
if (!props.allowDangerousHtml) {
3838
parent.children.splice(index, 1, {
3939
type: 'text',
4040
position: node.position,
@@ -179,7 +179,7 @@ function parsedHtml(fromNode, toNode, parent) {
179179
}
180180

181181
module.exports = function getHtmlParserPlugin(config, props) {
182-
if (props && (typeof config.source !== 'undefined' || typeof config.children !== 'undefined')) {
182+
if (props && typeof config.children !== 'undefined') {
183183
throw new Error(
184184
'react-markdown: `html-parser` must be called before use - see https://github.com/remarkjs/react-markdown#parsing-html'
185185
)

β€Žsrc/react-markdown.jsβ€Ž

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,23 @@ const symbols = require('./symbols')
1616

1717
const allTypes = Object.keys(defaultRenderers)
1818

19+
let warningIssuesSource
20+
let warningIssuesEscapeHtml
21+
1922
const ReactMarkdown = function ReactMarkdown(props) {
20-
// To do in next major: remove `source`.
21-
const src = props.source || props.children || ''
23+
if ('source' in props && !warningIssuesSource) {
24+
console.warn('[react-markdown] Warning: please use `children` instead of `source`')
25+
warningIssuesSource = true
26+
}
27+
28+
if ('escapeHtml' in props && !warningIssuesEscapeHtml) {
29+
console.warn(
30+
'[react-markdown] Warning: please use `allowDangerousHtml` instead of `escapeHtml`'
31+
)
32+
warningIssuesEscapeHtml = true
33+
}
34+
35+
const src = props.children || ''
2236

2337
if (props.allowedTypes && props.disallowedTypes) {
2438
throw new Error('Only one of `allowedTypes` and `disallowedTypes` should be defined')
@@ -61,8 +75,7 @@ function determineAstToReactTransforms(props) {
6175
transforms.push(disallowNode.ifNotMatch(props.allowNode, removalMethod))
6276
}
6377

64-
// To do in next major: remove `escapeHtml`.
65-
const renderHtml = (props.allowDangerousHtml || props.escapeHtml === false) && !props.skipHtml
78+
const renderHtml = props.allowDangerousHtml && !props.skipHtml
6679
const hasHtmlParser = (props.astPlugins || []).some(
6780
(transform) => transform.identity === symbols.HtmlParser
6881
)
@@ -87,11 +100,9 @@ ReactMarkdown.defaultProps = {
87100

88101
ReactMarkdown.propTypes = {
89102
className: PropTypes.string,
90-
source: PropTypes.string,
91103
children: PropTypes.string,
92104
sourcePos: PropTypes.bool,
93105
rawSourcePos: PropTypes.bool,
94-
escapeHtml: PropTypes.bool,
95106
allowDangerousHtml: PropTypes.bool,
96107
skipHtml: PropTypes.bool,
97108
allowNode: PropTypes.func,

β€Žsrc/renderers.jsβ€Ž

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ function Html(props) {
104104
return null
105105
}
106106

107-
const dangerous = props.allowDangerousHtml || props.escapeHtml === false
108-
109107
const tag = props.isBlock ? 'div' : 'span'
110108

111-
if (!dangerous) {
109+
if (!props.allowDangerousHtml) {
112110
/* istanbul ignore next - `tag` is a fallback for old React. */
113111
return createElement(React.Fragment || tag, null, props.value)
114112
}

β€Žtest/__snapshots__/react-markdown.test.js.snapβ€Ž

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,22 +1449,6 @@ Array [
14491449
]
14501450
`;
14511451

1452-
exports[`should escape html blocks if escapeHtml prop is set (default) (with HTML parser plugin) 1`] = `
1453-
Array [
1454-
<p>
1455-
This is a regular paragraph.
1456-
</p>,
1457-
"<table>
1458-
<tr>
1459-
<td>Foo</td>
1460-
</tr>
1461-
</table>",
1462-
<p>
1463-
This is another regular paragraph.
1464-
</p>,
1465-
]
1466-
`;
1467-
14681452
exports[`should handle blockquotes 1`] = `
14691453
<blockquote>
14701454
<p>

β€Žtest/react-markdown.test.jsβ€Ž

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ test('can render the most basic of documents (single paragraph)', () => {
2222
expect(component.toJSON()).toMatchSnapshot()
2323
})
2424

25+
test('should warn when passed `source`', () => {
26+
const warn = console.warn
27+
console.warn = jest.fn()
28+
expect(renderHTML(<Markdown source="a">b</Markdown>)).toEqual('<p>b</p>')
29+
expect(console.warn).toHaveBeenCalledWith(
30+
'[react-markdown] Warning: please use `children` instead of `source`'
31+
)
32+
console.warn = warn
33+
})
34+
35+
test('should warn when passed `escapeHtml`', () => {
36+
const warn = console.warn
37+
console.warn = jest.fn()
38+
expect(renderHTML(<Markdown escapeHtml>b</Markdown>)).toEqual('<p>b</p>')
39+
expect(console.warn).toHaveBeenCalledWith(
40+
'[react-markdown] Warning: please use `allowDangerousHtml` instead of `escapeHtml`'
41+
)
42+
console.warn = warn
43+
})
44+
2545
test('uses passed classname for root component', () => {
2646
const component = renderer.create(<Markdown className="md">Test</Markdown>)
2747
expect(component.toJSON()).toMatchSnapshot()
@@ -420,19 +440,6 @@ test('should skip html blocks if skipHtml prop is set (with HTML parser plugin)'
420440
expect(component.toJSON()).toMatchSnapshot()
421441
})
422442

423-
test('should escape html blocks if escapeHtml prop is set (default) (with HTML parser plugin)', () => {
424-
const input = [
425-
'This is a regular paragraph.\n\n<table>\n <tr>\n ',
426-
'<td>Foo</td>\n </tr>\n</table>\n\nThis is another',
427-
' regular paragraph.'
428-
].join('')
429-
430-
const component = renderer.create(
431-
<Markdown children={input} escapeHtml astPlugins={[htmlParser()]} />
432-
)
433-
expect(component.toJSON()).toMatchSnapshot()
434-
})
435-
436443
test('should escape html blocks by default (with HTML parser plugin)', () => {
437444
const input = [
438445
'This is a regular paragraph.\n\n<table>\n <tr>\n ',

0 commit comments

Comments
 (0)