Skip to content

Commit 6355e45

Browse files
wooormAslak Hellesøy
and
Aslak Hellesøy
committed
Fix to pass vfile to plugins
Closes GH-588. Co-authored-by: Aslak Hellesøy <[email protected]>
1 parent 5cf6e1b commit 6355e45

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
"space-separated-tokens": "^1.1.0",
8787
"style-to-object": "^0.3.0",
8888
"unified": "^9.0.0",
89-
"unist-util-visit": "^2.0.0"
89+
"unist-util-visit": "^2.0.0",
90+
"vfile": "^4.0.0"
9091
},
9192
"peerDependencies": {
9293
"@types/react": ">=16",

src/react-markdown.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const React = require('react')
4+
const vfile = require('vfile')
45
const unified = require('unified')
56
const parse = require('remark-parse')
67
const remarkRehype = require('remark-rehype')
@@ -98,21 +99,24 @@ function ReactMarkdown(options) {
9899
.use(options.rehypePlugins || [])
99100
.use(filter, options)
100101

101-
let children = options.children
102+
/** @type {vfile} */
103+
let file
102104

103-
if (typeof children !== 'string') {
104-
if (children !== undefined && children !== null) {
105+
if (typeof options.children === 'string') {
106+
file = vfile(options.children)
107+
} else {
108+
if (options.children !== undefined && options.children !== null) {
105109
console.warn(
106-
`[react-markdown] Warning: please pass a string as \`children\` (not: \`${children}\`)`
110+
`[react-markdown] Warning: please pass a string as \`children\` (not: \`${options.children}\`)`
107111
)
108112
}
109113

110-
children = ''
114+
file = vfile()
111115
}
112116

113117
/** @type {Root} */
114118
// @ts-ignore we’ll throw if it isn’t a root next.
115-
const hastNode = processor.runSync(processor.parse(children))
119+
const hastNode = processor.runSync(processor.parse(file), file)
116120

117121
if (hastNode.type !== 'root') {
118122
throw new TypeError('Expected a `root` node')

test/__snapshots__/react-markdown.test.js.snap

+15
Original file line numberDiff line numberDiff line change
@@ -2327,6 +2327,21 @@ Array [
23272327
]
23282328
`;
23292329

2330+
exports[`should pass on raw source position to non-tag components if rawSourcePos option is enabled and \`rehype-raw\` is used 1`] = `
2331+
Object {
2332+
"end": Object {
2333+
"column": 6,
2334+
"line": 1,
2335+
"offset": 5,
2336+
},
2337+
"start": Object {
2338+
"column": 1,
2339+
"line": 1,
2340+
"offset": 0,
2341+
},
2342+
}
2343+
`;
2344+
23302345
exports[`should render empty link references 1`] = `"<p>Stuff were changed in [][]. Check out the changelog for reference.</p>"`;
23312346

23322347
exports[`should render image references 1`] = `"<p>Checkout out this ninja: <img src=\\"/assets/ninja.png\\" alt=\\"The Waffle Ninja\\"/>. Pretty neat, eh?</p>"`;

test/react-markdown.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,27 @@ test('should pass on raw source position to non-tag components if rawSourcePos o
581581
expect(component.toJSON()).toMatchSnapshot()
582582
})
583583

584+
test('should pass on raw source position to non-tag components if rawSourcePos option is enabled and `rehype-raw` is used', () => {
585+
const input = '*Foo*'
586+
/**
587+
* @param {Object} props
588+
* @param {Position} [props.sourcePosition]
589+
*/
590+
const em = ({sourcePosition}) => {
591+
expect(sourcePosition).toMatchSnapshot()
592+
return ''
593+
}
594+
595+
renderer.create(
596+
<Markdown
597+
children={input}
598+
rawSourcePos
599+
rehypePlugins={[raw]}
600+
components={{em}}
601+
/>
602+
)
603+
})
604+
584605
test('should skip nodes that are not defined as allowed', () => {
585606
const input =
586607
'# Header\n\nParagraph\n## New header\n1. List item\n2. List item 2'

0 commit comments

Comments
 (0)