Skip to content

Commit

Permalink
fix(gatsby-remark-copy-linked-files): replace checking parent node ty…
Browse files Browse the repository at this point in the history
…pe to 'dir' (#31780) (#31828)

* remove checking parent node type

* fix the test cases

* add a new test case

(cherry picked from commit e854dcf)

Co-authored-by: Lee Yee Run <leerun1409@gmail.com>
  • Loading branch information
GatsbyJS Bot and lee1409 committed Jun 8, 2021
1 parent 86f6628 commit 34176c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
62 changes: 48 additions & 14 deletions packages/gatsby-remark-copy-linked-files/src/__tests__/index.js
Expand Up @@ -24,19 +24,20 @@ describe(`gatsby-remark-copy-linked-files`, () => {
fsExtra.copy.mockReset()
})

const parentDir = `/`
const markdownNode = {
parent: {},
}
const getNode = () => {
return {
dir: ``,
dir: parentDir,
internal: {
type: `File`,
},
}
}
const getFiles = filePath => {
const absolutePath = path.posix.normalize(filePath)
const absolutePath = path.posix.normalize(parentDir + filePath)
return [
{
absolutePath,
Expand Down Expand Up @@ -271,14 +272,32 @@ describe(`gatsby-remark-copy-linked-files`, () => {
expect(fsExtra.copy).not.toHaveBeenCalled()
})

it(`do nothing if dir is not found`, async () => {
const getNode = () => {
return {
internal: {
type: `Node`,
},
}
}
const path = `images/sample-image.gif`

const markdownAST = remark.parse(`![sample][1]\n\n[1]: ${path}`)

await plugin({ files: getFiles(path), markdownAST, markdownNode, getNode })

expect(fsExtra.copy).not.toHaveBeenCalled()
})

describe(`respects pathPrefix`, () => {
const imageName = `sample-image`
const imagePath = `images/${imageName}.svg`
const imageRelativePath = `images/${imageName}.svg`
const imagePath = parentDir + imageRelativePath

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

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

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

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

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

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

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

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

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

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

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

it(`copies file to the destination supplied by destinationDir (with pathPrefix)`, async () => {
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
const markdownAST = remark.parse(
`![some absolute image](${imageRelativePath})`
)
const pathPrefix = `/blog`
const validDestinationDir = `path/to/dir`

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

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

it(`copies file to the root dir when destinationDir is not supplied`, async () => {
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
const markdownAST = remark.parse(
`![some absolute image](${imageRelativePath})`
)
const expectedNewPath = path.posix.join(
process.cwd(),
`public`,
Expand Down
12 changes: 3 additions & 9 deletions packages/gatsby-remark-copy-linked-files/src/index.js
Expand Up @@ -89,10 +89,7 @@ module.exports = (
// Copy linked files to the destination directory and modify the AST to point
// to new location of the files.
const visitor = link => {
if (
isRelativeUrl(link.url) &&
getNode(markdownNode.parent).internal.type === `File`
) {
if (isRelativeUrl(link.url) && getNode(markdownNode.parent).dir) {
const linkPath = path.posix.join(
getNode(markdownNode.parent).dir,
link.url
Expand Down Expand Up @@ -193,11 +190,8 @@ module.exports = (
return
}

// since dir will be undefined on non-files
if (
markdownNode.parent &&
getNode(markdownNode.parent).internal.type !== `File`
) {
// Just make sure the parent node has dir
if (markdownNode.parent && !getNode(markdownNode.parent).dir) {
return
}

Expand Down

0 comments on commit 34176c6

Please sign in to comment.