Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-remark-copy-linked-files): replace checking parent node type to 'dir' #31780

Merged
merged 3 commits into from Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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