From a215cfbe3aa875fb932878039a5efbde6d97b538 Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Tue, 28 Jan 2020 10:33:21 -0800 Subject: [PATCH] fix: allow absolute paths for collection media folder (#3160) --- .../src/reducers/__tests__/entries.spec.js | 59 +++++++++++++++---- .../netlify-cms-core/src/reducers/entries.ts | 12 ++-- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/packages/netlify-cms-core/src/reducers/__tests__/entries.spec.js b/packages/netlify-cms-core/src/reducers/__tests__/entries.spec.js index 225fcf46..946ab677 100644 --- a/packages/netlify-cms-core/src/reducers/__tests__/entries.spec.js +++ b/packages/netlify-cms-core/src/reducers/__tests__/entries.spec.js @@ -98,7 +98,7 @@ describe('entries', () => { ).toEqual('posts/title'); }); - it('should resolve relative media folder', () => { + it('should resolve collection relative media folder', () => { expect( selectMediaFolder( Map({ media_folder: 'static/media' }), @@ -108,7 +108,21 @@ describe('entries', () => { ).toEqual('posts/'); }); - it('should resolve media folder template', () => { + it('should return collection absolute media folder as is', () => { + expect( + selectMediaFolder( + Map({ media_folder: '/static/Images' }), + Map({ + name: 'getting-started', + folder: 'src/docs/getting-started', + media_folder: '/static/images/docs/getting-started', + }), + Map({ path: 'src/docs/getting-started/with-github.md' }), + ), + ).toEqual('/static/images/docs/getting-started'); + }); + + it('should compile relative media folder template', () => { const slugConfig = { encoding: 'unicode', clean_accents: false, @@ -134,6 +148,33 @@ describe('entries', () => { ), ).toEqual('static/media/hosting-and-deployment/deployment-with-nanobox'); }); + + it('should compile absolute media folder template', () => { + const slugConfig = { + encoding: 'unicode', + clean_accents: false, + sanitize_replacement: '-', + }; + + const entry = fromJS({ + path: 'src/docs/extending/overview.md', + data: { title: 'Overview' }, + }); + const collection = fromJS({ + name: 'extending', + folder: 'src/docs/extending', + media_folder: '{{media_folder}}/docs/extending', + fields: [{ name: 'title', widget: 'string' }], + }); + + expect( + selectMediaFolder( + fromJS({ media_folder: '/static/images', slug: slugConfig }), + collection, + entry, + ), + ).toEqual('/static/images/docs/extending'); + }); }); describe('selectMediaFilePath', () => { @@ -143,13 +184,7 @@ describe('entries', () => { ); }); - it('should resolve path from global media folder when absolute path', () => { - expect( - selectMediaFilePath(Map({ media_folder: 'static/media' }), null, null, '/media/image.png'), - ).toBe('static/media/image.png'); - }); - - it('should resolve path from global media folder when relative path for collection with no media folder', () => { + it('should resolve path from global media folder for collection with no media folder', () => { expect( selectMediaFilePath( Map({ media_folder: 'static/media' }), @@ -160,7 +195,7 @@ describe('entries', () => { ).toBe('static/media/image.png'); }); - it('should resolve path from collection media folder when relative path for collection with media folder', () => { + it('should resolve path from collection media folder for collection with media folder', () => { expect( selectMediaFilePath( Map({ media_folder: 'static/media' }), @@ -196,7 +231,7 @@ describe('entries', () => { ).toBe('/media/image.png'); }); - it('should resolve path from collection media folder for collection with public folder', () => { + it('should resolve path from collection public folder for collection with public folder', () => { expect( selectMediaFilePublicPath( Map({ public_folder: '/media' }), @@ -216,7 +251,7 @@ describe('entries', () => { ).toBe('../../static/media/image.png'); }); - it('should resolve public folder template', () => { + it('should compile public folder template', () => { const slugConfig = { encoding: 'unicode', clean_accents: false, diff --git a/packages/netlify-cms-core/src/reducers/entries.ts b/packages/netlify-cms-core/src/reducers/entries.ts index 468bb956..988f95d4 100644 --- a/packages/netlify-cms-core/src/reducers/entries.ts +++ b/packages/netlify-cms-core/src/reducers/entries.ts @@ -157,6 +157,10 @@ export const selectMediaFolder = ( 'media_folder', config.get('slug'), ); + // return absolute paths as is + if (folder.startsWith('/')) { + return folder; + } mediaFolder = join(entryDir, folder as string); } else { mediaFolder = join(collection.get('folder') as string, DRAFT_MEDIA_FILES); @@ -176,13 +180,7 @@ export const selectMediaFilePath = ( return mediaPath; } - let mediaFolder; - if (mediaPath.startsWith('/')) { - // absolute media paths are not bound to a collection - mediaFolder = selectMediaFolder(config, null, entryMap); - } else { - mediaFolder = selectMediaFolder(config, collection, entryMap); - } + const mediaFolder = selectMediaFolder(config, collection, entryMap); return join(mediaFolder, basename(mediaPath)); };