Skip to content

Commit e854dcf

Browse files
authored
fix(gatsby-remark-copy-linked-files): replace checking parent node type to 'dir' (#31780)
* remove checking parent node type * fix the test cases * add a new test case
1 parent ce8e39d commit e854dcf

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

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

+48-14
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@ describe(`gatsby-remark-copy-linked-files`, () => {
2424
fsExtra.copy.mockReset()
2525
})
2626

27+
const parentDir = `/`
2728
const markdownNode = {
2829
parent: {},
2930
}
3031
const getNode = () => {
3132
return {
32-
dir: ``,
33+
dir: parentDir,
3334
internal: {
3435
type: `File`,
3536
},
3637
}
3738
}
3839
const getFiles = filePath => {
39-
const absolutePath = path.posix.normalize(filePath)
40+
const absolutePath = path.posix.normalize(parentDir + filePath)
4041
return [
4142
{
4243
absolutePath,
@@ -271,14 +272,32 @@ describe(`gatsby-remark-copy-linked-files`, () => {
271272
expect(fsExtra.copy).not.toHaveBeenCalled()
272273
})
273274

275+
it(`do nothing if dir is not found`, async () => {
276+
const getNode = () => {
277+
return {
278+
internal: {
279+
type: `Node`,
280+
},
281+
}
282+
}
283+
const path = `images/sample-image.gif`
284+
285+
const markdownAST = remark.parse(`![sample][1]\n\n[1]: ${path}`)
286+
287+
await plugin({ files: getFiles(path), markdownAST, markdownNode, getNode })
288+
289+
expect(fsExtra.copy).not.toHaveBeenCalled()
290+
})
291+
274292
describe(`respects pathPrefix`, () => {
275293
const imageName = `sample-image`
276-
const imagePath = `images/${imageName}.svg`
294+
const imageRelativePath = `images/${imageName}.svg`
295+
const imagePath = parentDir + imageRelativePath
277296

278297
// pathPrefix passed to plugins already combine pathPrefix and assetPrefix
279298
it(`relative pathPrefix (no assetPrefix)`, async () => {
280299
const pathPrefix = `/path-prefix`
281-
const markdownAST = remark.parse(`![some image](${imagePath})`)
300+
const markdownAST = remark.parse(`![some image](${imageRelativePath})`)
282301

283302
const expectedNewPath = path.posix.join(
284303
process.cwd(),
@@ -304,7 +323,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
304323

305324
it(`absolute pathPrefix (with assetPrefix, empty base path prefix)`, async () => {
306325
const pathPrefix = `https://cdn.mysiteassets.com`
307-
const markdownAST = remark.parse(`![some image](${imagePath})`)
326+
const markdownAST = remark.parse(`![some image](${imageRelativePath})`)
308327

309328
const expectedNewPath = path.posix.join(
310329
process.cwd(),
@@ -330,7 +349,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
330349

331350
it(`absolute pathPrefix (with assetPrefix, and non-empty base path prefix)`, async () => {
332351
const pathPrefix = `https://cdn.mysiteassets.com/path-prefix`
333-
const markdownAST = remark.parse(`![some image](${imagePath})`)
352+
const markdownAST = remark.parse(`![some image](${imageRelativePath})`)
334353

335354
const expectedNewPath = path.posix.join(
336355
process.cwd(),
@@ -357,10 +376,13 @@ describe(`gatsby-remark-copy-linked-files`, () => {
357376

358377
describe(`options.destinationDir`, () => {
359378
const imageName = `sample-image`
360-
const imagePath = `images/${imageName}.gif`
379+
const imageRelativePath = `images/${imageName}.gif`
380+
const imagePath = parentDir + imageRelativePath
361381

362382
it(`throws an error if the destination supplied by destinationDir points outside of the root dir`, async () => {
363-
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
383+
const markdownAST = remark.parse(
384+
`![some absolute image](${imageRelativePath})`
385+
)
364386
const invalidDestinationDir = `../destination`
365387
expect.assertions(2)
366388
return plugin(
@@ -375,7 +397,9 @@ describe(`gatsby-remark-copy-linked-files`, () => {
375397
})
376398

377399
it(`throws an error if the destination supplied by the destinationDir function points outside of the root dir`, async () => {
378-
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
400+
const markdownAST = remark.parse(
401+
`![some absolute image](${imageRelativePath})`
402+
)
379403
const invalidDestinationDir = `../destination`
380404
const customDestinationDir = f =>
381405
`../destination/${f.hash}/${f.name}/${f.notexist}`
@@ -392,7 +416,9 @@ describe(`gatsby-remark-copy-linked-files`, () => {
392416
})
393417

394418
it(`copies file to the destination supplied by destinationDir`, async () => {
395-
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
419+
const markdownAST = remark.parse(
420+
`![some absolute image](${imageRelativePath})`
421+
)
396422
const validDestinationDir = `path/to/dir`
397423
const fileLocationPart = `some-hash/${imageName}.gif`
398424
const expectedNewPath = path.posix.join(
@@ -417,7 +443,9 @@ describe(`gatsby-remark-copy-linked-files`, () => {
417443
})
418444

419445
it(`copies file to the destination supplied by the destinationDir function`, async () => {
420-
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
446+
const markdownAST = remark.parse(
447+
`![some absolute image](${imageRelativePath})`
448+
)
421449
const customDestinationDir = f => `foo/${f.hash}--bar`
422450
const expectedDestination = `foo/some-hash--bar.gif`
423451
expect.assertions(3)
@@ -435,7 +463,9 @@ describe(`gatsby-remark-copy-linked-files`, () => {
435463
})
436464

437465
it(`copies file to the destination supplied by destinationDir (with pathPrefix)`, async () => {
438-
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
466+
const markdownAST = remark.parse(
467+
`![some absolute image](${imageRelativePath})`
468+
)
439469
const pathPrefix = `/blog`
440470
const validDestinationDir = `path/to/dir`
441471

@@ -468,7 +498,9 @@ describe(`gatsby-remark-copy-linked-files`, () => {
468498
})
469499

470500
it(`copies file to the destination supplied by the destinationDir function (with pathPrefix)`, async () => {
471-
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
501+
const markdownAST = remark.parse(
502+
`![some absolute image](${imageRelativePath})`
503+
)
472504
const pathPrefix = `/blog`
473505
const customDestinationDir = f => `hello${f.name}123`
474506
const expectedDestination = `hello${imageName}123.gif`
@@ -495,7 +527,9 @@ describe(`gatsby-remark-copy-linked-files`, () => {
495527
})
496528

497529
it(`copies file to the root dir when destinationDir is not supplied`, async () => {
498-
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
530+
const markdownAST = remark.parse(
531+
`![some absolute image](${imageRelativePath})`
532+
)
499533
const expectedNewPath = path.posix.join(
500534
process.cwd(),
501535
`public`,

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ module.exports = (
8989
// Copy linked files to the destination directory and modify the AST to point
9090
// to new location of the files.
9191
const visitor = link => {
92-
if (
93-
isRelativeUrl(link.url) &&
94-
getNode(markdownNode.parent).internal.type === `File`
95-
) {
92+
if (isRelativeUrl(link.url) && getNode(markdownNode.parent).dir) {
9693
const linkPath = path.posix.join(
9794
getNode(markdownNode.parent).dir,
9895
link.url
@@ -193,11 +190,8 @@ module.exports = (
193190
return
194191
}
195192

196-
// since dir will be undefined on non-files
197-
if (
198-
markdownNode.parent &&
199-
getNode(markdownNode.parent).internal.type !== `File`
200-
) {
193+
// Just make sure the parent node has dir
194+
if (markdownNode.parent && !getNode(markdownNode.parent).dir) {
201195
return
202196
}
203197

0 commit comments

Comments
 (0)