Skip to content

Commit 0b1c9f2

Browse files
trevorbladesjohno
authored andcommitted
fix(gatsby-remark-copy-linked-files): Support MDX by visiting JSX nodes (#13552)
* Visit both html and jsx nodes to support mdx * Babel transform remark-mdx so that node6 tests pass * Skip the test instead of transforming remark-mdx * Only import remark-mdx on node >=8 * Just move the require inside the test
1 parent 2aa915d commit 0b1c9f2

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

packages/gatsby-remark-copy-linked-files/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
"@babel/cli": "^7.0.0",
2121
"@babel/core": "^7.0.0",
2222
"babel-preset-gatsby-package": "^0.1.4",
23-
"cross-env": "^5.1.4"
23+
"cross-env": "^5.1.4",
24+
"remark": "^10.0.1",
25+
"remark-mdx": "^1.0.14",
26+
"semver": "^6.0.0"
2427
},
2528
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-copy-linked-files#readme",
2629
"keywords": [

packages/gatsby-remark-copy-linked-files/src/__tests__/index.js

+28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ jest.mock(`fs-extra`, () => {
88
const Remark = require(`remark`)
99
const fsExtra = require(`fs-extra`)
1010
const path = require(`path`)
11+
const semver = require(`semver`)
1112

1213
const plugin = require(`../`)
1314

@@ -19,6 +20,15 @@ const remark = new Remark().data(`settings`, {
1920

2021
const imageURL = markdownAST => markdownAST.children[0].children[0].url
2122

23+
const testInNode8OrHigher = (title, ...args) => {
24+
const isNode8OrHigher = semver.satisfies(process.version, `>=8`)
25+
if (isNode8OrHigher) {
26+
it(title, ...args)
27+
} else {
28+
it.skip(`skipped on Node 7 or lower: ${title}`, ...args)
29+
}
30+
}
31+
2232
describe(`gatsby-remark-copy-linked-files`, () => {
2333
afterEach(() => {
2434
fsExtra.copy.mockReset()
@@ -122,6 +132,24 @@ describe(`gatsby-remark-copy-linked-files`, () => {
122132
expect(fsExtra.copy).toHaveBeenCalled()
123133
})
124134

135+
testInNode8OrHigher(`can copy JSX images`, async () => {
136+
const mdx = require(`remark-mdx`)
137+
const path = `images/sample-image.gif`
138+
139+
const markdownAST = remark()
140+
.use(mdx)
141+
.parse(`<img src="${path}" />`)
142+
143+
await plugin({
144+
files: getFiles(path),
145+
markdownAST,
146+
markdownNode,
147+
getNode,
148+
})
149+
150+
expect(fsExtra.copy).toHaveBeenCalled()
151+
})
152+
125153
it(`can copy HTML multiple images`, async () => {
126154
const path1 = `images/sample-image.gif`
127155
const path2 = `images/another-sample-image.gif`

packages/gatsby-remark-copy-linked-files/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ module.exports = (
203203
})
204204

205205
// For each HTML Node
206-
visit(markdownAST, `html`, node => {
206+
visit(markdownAST, [`html`, `jsx`], node => {
207207
const $ = cheerio.load(node.value)
208208

209209
function processUrl({ url }) {

0 commit comments

Comments
 (0)