@@ -35,13 +35,19 @@ describe(`gatsby-remark-copy-linked-files`, () => {
35
35
} ,
36
36
}
37
37
}
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
+ }
45
51
46
52
describe ( `images` , ( ) => {
47
53
; [ `svg` , `gif` ] . forEach ( extension => {
@@ -265,8 +271,93 @@ describe(`gatsby-remark-copy-linked-files`, () => {
265
271
expect ( fsExtra . copy ) . not . toHaveBeenCalled ( )
266
272
} )
267
273
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 ( `` )
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 ( `` )
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 ( `` )
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
+
268
358
describe ( `options.destinationDir` , ( ) => {
269
- const imagePath = `images/sample-image.gif`
359
+ const imageName = `sample-image`
360
+ const imagePath = `images/${ imageName } .gif`
270
361
271
362
it ( `throws an error if the destination supplied by destinationDir points outside of the root dir` , async ( ) => {
272
363
const markdownAST = remark . parse ( `` )
@@ -303,11 +394,12 @@ describe(`gatsby-remark-copy-linked-files`, () => {
303
394
it ( `copies file to the destination supplied by destinationDir` , async ( ) => {
304
395
const markdownAST = remark . parse ( `` )
305
396
const validDestinationDir = `path/to/dir`
397
+ const fileLocationPart = `some-hash/${ imageName } .gif`
306
398
const expectedNewPath = path . posix . join (
307
399
process . cwd ( ) ,
308
400
`public` ,
309
401
validDestinationDir ,
310
- `/undefined/undefined.gif`
402
+ fileLocationPart
311
403
)
312
404
expect . assertions ( 3 )
313
405
await plugin (
@@ -319,15 +411,15 @@ describe(`gatsby-remark-copy-linked-files`, () => {
319
411
expect ( v ) . toBeDefined ( )
320
412
expect ( fsExtra . copy ) . toHaveBeenCalledWith ( imagePath , expectedNewPath )
321
413
expect ( imageURL ( markdownAST ) ) . toEqual (
322
- `/path/to/dir/undefined/undefined.gif `
414
+ `/path/to/dir/${ fileLocationPart } `
323
415
)
324
416
} )
325
417
} )
326
418
327
419
it ( `copies file to the destination supplied by the destinationDir function` , async ( ) => {
328
420
const markdownAST = remark . parse ( `` )
329
421
const customDestinationDir = f => `foo/${ f . hash } --bar`
330
- const expectedDestination = `foo/undefined --bar.gif`
422
+ const expectedDestination = `foo/some-hash --bar.gif`
331
423
expect . assertions ( 3 )
332
424
await plugin (
333
425
{ files : getFiles ( imagePath ) , markdownAST, markdownNode, getNode } ,
@@ -346,11 +438,13 @@ describe(`gatsby-remark-copy-linked-files`, () => {
346
438
const markdownAST = remark . parse ( `` )
347
439
const pathPrefix = `/blog`
348
440
const validDestinationDir = `path/to/dir`
441
+
442
+ const fileLocationPart = `some-hash/${ imageName } .gif`
349
443
const expectedNewPath = path . posix . join (
350
444
process . cwd ( ) ,
351
445
`public` ,
352
446
validDestinationDir ,
353
- `/undefined/undefined.gif`
447
+ fileLocationPart
354
448
)
355
449
expect . assertions ( 3 )
356
450
await plugin (
@@ -368,7 +462,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
368
462
expect ( v ) . toBeDefined ( )
369
463
expect ( fsExtra . copy ) . toHaveBeenCalledWith ( imagePath , expectedNewPath )
370
464
expect ( imageURL ( markdownAST ) ) . toEqual (
371
- `${ pathPrefix } /path/to/dir/undefined/undefined.gif `
465
+ `${ pathPrefix } /path/to/dir/${ fileLocationPart } `
372
466
)
373
467
} )
374
468
} )
@@ -377,7 +471,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
377
471
const markdownAST = remark . parse ( `` )
378
472
const pathPrefix = `/blog`
379
473
const customDestinationDir = f => `hello${ f . name } 123`
380
- const expectedDestination = `helloundefined123 .gif`
474
+ const expectedDestination = `hello ${ imageName } 123 .gif`
381
475
expect . assertions ( 3 )
382
476
await plugin (
383
477
{
@@ -400,12 +494,12 @@ describe(`gatsby-remark-copy-linked-files`, () => {
400
494
} )
401
495
} )
402
496
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 ( ) => {
404
498
const markdownAST = remark . parse ( `` )
405
499
const expectedNewPath = path . posix . join (
406
500
process . cwd ( ) ,
407
501
`public` ,
408
- `/undefined/undefined .gif`
502
+ `/some-hash/ ${ imageName } .gif`
409
503
)
410
504
expect . assertions ( 3 )
411
505
await plugin ( {
@@ -416,7 +510,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
416
510
} ) . then ( v => {
417
511
expect ( v ) . toBeDefined ( )
418
512
expect ( fsExtra . copy ) . toHaveBeenCalledWith ( imagePath , expectedNewPath )
419
- expect ( imageURL ( markdownAST ) ) . toEqual ( `/undefined/undefined .gif` )
513
+ expect ( imageURL ( markdownAST ) ) . toEqual ( `/some-hash/ ${ imageName } .gif` )
420
514
} )
421
515
} )
422
516
} )
0 commit comments