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

Consistently pass the pathPrefix with linked files #4841

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/gatsby-remark-copy-linked-files/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,31 @@ describe(`gatsby-remark-copy-linked-files`, () => {
})
})

it(`copies file to destinationDir when supplied (with pathPrefix)`, async () => {
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
const pathPrefix = `/blog`
const validDestinationDir = `path/to/dir`
const expectedNewPath = path.posix.join(
process.cwd(),
`public`,
validDestinationDir,
`/undefined-undefined.gif`
)
expect.assertions(3)
await plugin(
{ files: getFiles(imagePath), markdownAST, markdownNode, pathPrefix, getNode },
{
destinationDir: validDestinationDir,
}
).then(v => {
expect(v).toBeDefined()
expect(fsExtra.copy).toHaveBeenCalledWith(imagePath, expectedNewPath)
expect(imageURL(markdownAST)).toEqual(
`${pathPrefix}/path/to/dir/undefined-undefined.gif`
)
})
})

it(`copies file to root dir when not supplied'`, async () => {
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
const expectedNewPath = path.posix.join(
Expand Down
17 changes: 10 additions & 7 deletions packages/gatsby-remark-copy-linked-files/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ const newPath = (linkNode, destinationDir) => {
return path.posix.join(process.cwd(), DEPLOY_DIR, newFileName(linkNode))
}

const newLinkURL = (linkNode, destinationDir) => {
if (destinationDir) {
return path.posix.join(`/`, destinationDir, newFileName(linkNode))
}
return path.posix.join(`/`, newFileName(linkNode))
const newLinkURL = (linkNode, destinationDir, pathPrefix) => {
const linkPaths = [`/`, pathPrefix, destinationDir, newFileName(linkNode)]
.filter(function(lpath) {
if (lpath) return true
return false
})

return path.posix.join(...linkPaths)
}

function toArray(buf) {
Expand All @@ -51,7 +54,7 @@ function toArray(buf) {
}

module.exports = (
{ files, markdownNode, markdownAST, getNode },
{ files, markdownNode, markdownAST, pathPrefix, getNode },
pluginOptions = {}
) => {
const defaults = {
Expand Down Expand Up @@ -87,7 +90,7 @@ module.exports = (
// Prevent uneeded copying
if (linkPath === newFilePath) return

const linkURL = newLinkURL(linkNode, options.destinationDir)
const linkURL = newLinkURL(linkNode, options.destinationDir, pathPrefix)
link.url = linkURL
filesToCopy.set(linkPath, newFilePath)
}
Expand Down