Skip to content

Commit 6270c3d

Browse files
piehgatsbybot
and
gatsbybot
authored
fix(gatsby-remark-copy-linked-files): respect assetPrefix (#26976)
* test(gatsby-remarkc-copy-linked-files): get rid of "undefined" in assertions - it doesn't inspire confidence * fix(gatsby-remark-copy-linked-files): honor assetPrefix correctly Co-authored-by: gatsbybot <[email protected]>
1 parent 5c5c045 commit 6270c3d

File tree

2 files changed

+112
-21
lines changed

2 files changed

+112
-21
lines changed

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

Lines changed: 111 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ describe(`gatsby-remark-copy-linked-files`, () => {
3535
},
3636
}
3737
}
38-
const getFiles = filePath => [
39-
{
40-
absolutePath: path.posix.normalize(filePath),
41-
internal: {},
42-
extension: filePath.split(`.`).pop().trim(),
43-
},
44-
]
38+
const getFiles = filePath => {
39+
const absolutePath = path.posix.normalize(filePath)
40+
return [
41+
{
42+
absolutePath,
43+
name: path.basename(absolutePath, path.extname(absolutePath)),
44+
internal: {
45+
contentDigest: `some-hash`,
46+
},
47+
extension: filePath.split(`.`).pop().trim(),
48+
},
49+
]
50+
}
4551

4652
describe(`images`, () => {
4753
;[`svg`, `gif`].forEach(extension => {
@@ -265,8 +271,93 @@ describe(`gatsby-remark-copy-linked-files`, () => {
265271
expect(fsExtra.copy).not.toHaveBeenCalled()
266272
})
267273

274+
describe(`respects pathPrefix`, () => {
275+
const imageName = `sample-image`
276+
const imagePath = `images/${imageName}.svg`
277+
278+
// pathPrefix passed to plugins already combine pathPrefix and assetPrefix
279+
it(`relative pathPrefix (no assetPrefix)`, async () => {
280+
const pathPrefix = `/path-prefix`
281+
const markdownAST = remark.parse(`![some image](${imagePath})`)
282+
283+
const expectedNewPath = path.posix.join(
284+
process.cwd(),
285+
`public`,
286+
`some-hash`,
287+
`sample-image.svg`
288+
)
289+
290+
await plugin({
291+
files: getFiles(imagePath),
292+
markdownAST,
293+
markdownNode,
294+
getNode,
295+
pathPrefix,
296+
})
297+
298+
expect(imageURL(markdownAST)).toEqual(
299+
`/path-prefix/some-hash/sample-image.svg`
300+
)
301+
302+
expect(fsExtra.copy).toHaveBeenCalledWith(imagePath, expectedNewPath)
303+
})
304+
305+
it(`absolute pathPrefix (with assetPrefix, empty base path prefix)`, async () => {
306+
const pathPrefix = `https://cdn.mysiteassets.com`
307+
const markdownAST = remark.parse(`![some image](${imagePath})`)
308+
309+
const expectedNewPath = path.posix.join(
310+
process.cwd(),
311+
`public`,
312+
`some-hash`,
313+
`sample-image.svg`
314+
)
315+
316+
await plugin({
317+
files: getFiles(imagePath),
318+
markdownAST,
319+
markdownNode,
320+
getNode,
321+
pathPrefix,
322+
})
323+
324+
expect(imageURL(markdownAST)).toEqual(
325+
`https://cdn.mysiteassets.com/some-hash/sample-image.svg`
326+
)
327+
328+
expect(fsExtra.copy).toHaveBeenCalledWith(imagePath, expectedNewPath)
329+
})
330+
331+
it(`absolute pathPrefix (with assetPrefix, and non-empty base path prefix)`, async () => {
332+
const pathPrefix = `https://cdn.mysiteassets.com/path-prefix`
333+
const markdownAST = remark.parse(`![some image](${imagePath})`)
334+
335+
const expectedNewPath = path.posix.join(
336+
process.cwd(),
337+
`public`,
338+
`some-hash`,
339+
`sample-image.svg`
340+
)
341+
342+
await plugin({
343+
files: getFiles(imagePath),
344+
markdownAST,
345+
markdownNode,
346+
getNode,
347+
pathPrefix,
348+
})
349+
350+
expect(imageURL(markdownAST)).toEqual(
351+
`https://cdn.mysiteassets.com/path-prefix/some-hash/sample-image.svg`
352+
)
353+
354+
expect(fsExtra.copy).toHaveBeenCalledWith(imagePath, expectedNewPath)
355+
})
356+
})
357+
268358
describe(`options.destinationDir`, () => {
269-
const imagePath = `images/sample-image.gif`
359+
const imageName = `sample-image`
360+
const imagePath = `images/${imageName}.gif`
270361

271362
it(`throws an error if the destination supplied by destinationDir points outside of the root dir`, async () => {
272363
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
@@ -303,11 +394,12 @@ describe(`gatsby-remark-copy-linked-files`, () => {
303394
it(`copies file to the destination supplied by destinationDir`, async () => {
304395
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
305396
const validDestinationDir = `path/to/dir`
397+
const fileLocationPart = `some-hash/${imageName}.gif`
306398
const expectedNewPath = path.posix.join(
307399
process.cwd(),
308400
`public`,
309401
validDestinationDir,
310-
`/undefined/undefined.gif`
402+
fileLocationPart
311403
)
312404
expect.assertions(3)
313405
await plugin(
@@ -319,15 +411,15 @@ describe(`gatsby-remark-copy-linked-files`, () => {
319411
expect(v).toBeDefined()
320412
expect(fsExtra.copy).toHaveBeenCalledWith(imagePath, expectedNewPath)
321413
expect(imageURL(markdownAST)).toEqual(
322-
`/path/to/dir/undefined/undefined.gif`
414+
`/path/to/dir/${fileLocationPart}`
323415
)
324416
})
325417
})
326418

327419
it(`copies file to the destination supplied by the destinationDir function`, async () => {
328420
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
329421
const customDestinationDir = f => `foo/${f.hash}--bar`
330-
const expectedDestination = `foo/undefined--bar.gif`
422+
const expectedDestination = `foo/some-hash--bar.gif`
331423
expect.assertions(3)
332424
await plugin(
333425
{ files: getFiles(imagePath), markdownAST, markdownNode, getNode },
@@ -346,11 +438,13 @@ describe(`gatsby-remark-copy-linked-files`, () => {
346438
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
347439
const pathPrefix = `/blog`
348440
const validDestinationDir = `path/to/dir`
441+
442+
const fileLocationPart = `some-hash/${imageName}.gif`
349443
const expectedNewPath = path.posix.join(
350444
process.cwd(),
351445
`public`,
352446
validDestinationDir,
353-
`/undefined/undefined.gif`
447+
fileLocationPart
354448
)
355449
expect.assertions(3)
356450
await plugin(
@@ -368,7 +462,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
368462
expect(v).toBeDefined()
369463
expect(fsExtra.copy).toHaveBeenCalledWith(imagePath, expectedNewPath)
370464
expect(imageURL(markdownAST)).toEqual(
371-
`${pathPrefix}/path/to/dir/undefined/undefined.gif`
465+
`${pathPrefix}/path/to/dir/${fileLocationPart}`
372466
)
373467
})
374468
})
@@ -377,7 +471,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
377471
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
378472
const pathPrefix = `/blog`
379473
const customDestinationDir = f => `hello${f.name}123`
380-
const expectedDestination = `helloundefined123.gif`
474+
const expectedDestination = `hello${imageName}123.gif`
381475
expect.assertions(3)
382476
await plugin(
383477
{
@@ -400,12 +494,12 @@ describe(`gatsby-remark-copy-linked-files`, () => {
400494
})
401495
})
402496

403-
it(`copies file to the root dir when destinationDir is not supplied'`, async () => {
497+
it(`copies file to the root dir when destinationDir is not supplied`, async () => {
404498
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
405499
const expectedNewPath = path.posix.join(
406500
process.cwd(),
407501
`public`,
408-
`/undefined/undefined.gif`
502+
`/some-hash/${imageName}.gif`
409503
)
410504
expect.assertions(3)
411505
await plugin({
@@ -416,7 +510,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
416510
}).then(v => {
417511
expect(v).toBeDefined()
418512
expect(fsExtra.copy).toHaveBeenCalledWith(imagePath, expectedNewPath)
419-
expect(imageURL(markdownAST)).toEqual(`/undefined/undefined.gif`)
513+
expect(imageURL(markdownAST)).toEqual(`/some-hash/${imageName}.gif`)
420514
})
421515
})
422516
})

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ const newPath = (linkNode, options) => {
5858
const newLinkURL = (linkNode, options, pathPrefix) => {
5959
const { destinationDir } = options
6060
const destination = getDestination(linkNode, destinationDir)
61-
const linkPaths = [`/`, pathPrefix, destination].filter(lpath =>
62-
lpath ? true : false
63-
)
64-
return path.posix.join(...linkPaths)
61+
return `${pathPrefix ? pathPrefix : ``}/${destination}`
6562
}
6663

6764
function toArray(buf) {

0 commit comments

Comments
 (0)