feat: support per collection public_folder (#3069)

* feat: support per collection public_folder

* test: improve tests readability
This commit is contained in:
Erez Rokah 2020-01-14 20:02:53 +02:00 committed by Shawn Erquhart
parent 476f45096e
commit 0a50efda8e
5 changed files with 161 additions and 91 deletions

View File

@ -3,6 +3,7 @@ import { applyDefaults } from '../config';
describe('config', () => { describe('config', () => {
describe('applyDefaults', () => { describe('applyDefaults', () => {
describe('publish_mode', () => {
it('should set publish_mode if not set', () => { it('should set publish_mode if not set', () => {
const config = fromJS({ const config = fromJS({
foo: 'bar', foo: 'bar',
@ -23,7 +24,9 @@ describe('config', () => {
}); });
expect(applyDefaults(config).get('publish_mode')).toEqual('complex'); expect(applyDefaults(config).get('publish_mode')).toEqual('complex');
}); });
});
describe('public_folder', () => {
it('should set public_folder based on media_folder if not set', () => { it('should set public_folder based on media_folder if not set', () => {
expect( expect(
applyDefaults( applyDefaults(
@ -48,7 +51,9 @@ describe('config', () => {
).get('public_folder'), ).get('public_folder'),
).toEqual('/publib/path'); ).toEqual('/publib/path');
}); });
});
describe('collections', () => {
it('should strip leading slashes from collection folder', () => { it('should strip leading slashes from collection folder', () => {
expect( expect(
applyDefaults( applyDefaults(
@ -69,13 +74,14 @@ describe('config', () => {
).toEqual(fromJS([{ files: [{ file: 'foo' }] }])); ).toEqual(fromJS([{ files: [{ file: 'foo' }] }]));
}); });
it('should set default slug config', () => { describe('slug', () => {
it('should set default slug config if not set', () => {
expect(applyDefaults(fromJS({ collections: [] })).get('slug')).toEqual( expect(applyDefaults(fromJS({ collections: [] })).get('slug')).toEqual(
fromJS({ encoding: 'unicode', clean_accents: false, sanitize_replacement: '-' }), fromJS({ encoding: 'unicode', clean_accents: false, sanitize_replacement: '-' }),
); );
}); });
it('should not override slug encoding', () => { it('should not overwrite slug encoding if set', () => {
expect( expect(
applyDefaults(fromJS({ collections: [], slug: { encoding: 'ascii' } })).getIn([ applyDefaults(fromJS({ collections: [], slug: { encoding: 'ascii' } })).getIn([
'slug', 'slug',
@ -84,7 +90,7 @@ describe('config', () => {
).toEqual('ascii'); ).toEqual('ascii');
}); });
it('should not override slug clean_accents', () => { it('should not overwrite slug clean_accents if set', () => {
expect( expect(
applyDefaults(fromJS({ collections: [], slug: { clean_accents: true } })).getIn([ applyDefaults(fromJS({ collections: [], slug: { clean_accents: true } })).getIn([
'slug', 'slug',
@ -93,7 +99,7 @@ describe('config', () => {
).toEqual(true); ).toEqual(true);
}); });
it('should not override slug sanitize_replacement', () => { it('should not overwrite slug sanitize_replacement if set', () => {
expect( expect(
applyDefaults(fromJS({ collections: [], slug: { sanitize_replacement: '_' } })).getIn([ applyDefaults(fromJS({ collections: [], slug: { sanitize_replacement: '_' } })).getIn([
'slug', 'slug',
@ -102,4 +108,64 @@ describe('config', () => {
).toEqual('_'); ).toEqual('_');
}); });
}); });
describe('public_folder and media_folder', () => {
it('should set collection public_folder collection based on media_folder if not set', () => {
expect(
applyDefaults(
fromJS({
collections: [{ folder: 'foo', media_folder: 'static/images/docs' }],
}),
).get('collections'),
).toEqual(
fromJS([
{
folder: 'foo',
media_folder: 'static/images/docs',
public_folder: 'static/images/docs',
},
]),
);
});
it('should not overwrite collection public_folder if set', () => {
expect(
applyDefaults(
fromJS({
collections: [
{
folder: 'foo',
media_folder: 'static/images/docs',
public_folder: 'images/docs',
},
],
}),
).get('collections'),
).toEqual(
fromJS([
{
folder: 'foo',
media_folder: 'static/images/docs',
public_folder: 'images/docs',
},
]),
);
});
it("should set collection media_folder and public_folder to an empty string when collection path exists, but collection media_folder doesn't", () => {
expect(
applyDefaults(
fromJS({
collections: [{ folder: 'foo', path: '{{slug}}/index' }],
}),
).get('collections'),
).toEqual(
fromJS([
{ folder: 'foo', path: '{{slug}}/index', media_folder: '', public_folder: '' },
]),
);
});
});
});
});
}); });

View File

@ -64,6 +64,9 @@ export function applyDefaults(config) {
// default value for media folder when using the path config // default value for media folder when using the path config
collection = collection.set('media_folder', ''); collection = collection.set('media_folder', '');
} }
if (collection.has('media_folder') && !collection.has('public_folder')) {
collection = collection.set('public_folder', collection.get('media_folder'));
}
return collection.set('folder', trimStart(folder, '/')); return collection.set('folder', trimStart(folder, '/'));
} }

View File

@ -182,7 +182,7 @@ describe('entries', () => {
).toBe('/media/image.png'); ).toBe('/media/image.png');
}); });
it('should resolve path from public folder when in editorial workflow for collection with no media folder', () => { it('should resolve path from public folder when in editorial workflow for collection with no public folder', () => {
expect( expect(
selectMediaFilePublicPath( selectMediaFilePublicPath(
Map({ public_folder: '/media', publish_mode: EDITORIAL_WORKFLOW }), Map({ public_folder: '/media', publish_mode: EDITORIAL_WORKFLOW }),
@ -192,21 +192,21 @@ describe('entries', () => {
).toBe('/media/image.png'); ).toBe('/media/image.png');
}); });
it('should resolve path from collection media folder when in editorial workflow for collection with media folder', () => { it('should resolve path from collection media folder when in editorial workflow for collection with public folder', () => {
expect( expect(
selectMediaFilePublicPath( selectMediaFilePublicPath(
Map({ public_folder: '/media', publish_mode: EDITORIAL_WORKFLOW }), Map({ public_folder: '/media', publish_mode: EDITORIAL_WORKFLOW }),
Map({ name: 'posts', folder: 'posts', media_folder: '' }), Map({ name: 'posts', folder: 'posts', public_folder: '' }),
'image.png', 'image.png',
), ),
).toBe('image.png'); ).toBe('image.png');
}); });
it('should handle relative media_folder', () => { it('should handle relative public_folder', () => {
expect( expect(
selectMediaFilePublicPath( selectMediaFilePublicPath(
Map({ public_folder: '/media', publish_mode: EDITORIAL_WORKFLOW }), Map({ public_folder: '/media', publish_mode: EDITORIAL_WORKFLOW }),
Map({ name: 'posts', folder: 'posts', media_folder: '../../static/media/' }), Map({ name: 'posts', folder: 'posts', public_folder: '../../static/media/' }),
'image.png', 'image.png',
), ),
).toBe('../../static/media/image.png'); ).toBe('../../static/media/image.png');

View File

@ -190,8 +190,8 @@ export const selectMediaFilePublicPath = (
let publicFolder = config.get('public_folder'); let publicFolder = config.get('public_folder');
const useWorkflow = config.get('publish_mode') === EDITORIAL_WORKFLOW; const useWorkflow = config.get('publish_mode') === EDITORIAL_WORKFLOW;
if (useWorkflow && collection && collection.has('media_folder')) { if (useWorkflow && collection && collection.has('public_folder')) {
publicFolder = collection.get('media_folder') as string; publicFolder = collection.get('public_folder') as string;
} }
return join(publicFolder, basename(mediaPath)); return join(publicFolder, basename(mediaPath));

View File

@ -100,6 +100,7 @@ type CollectionObject = {
fields: EntryFields; fields: EntryFields;
isFetching: boolean; isFetching: boolean;
media_folder?: string; media_folder?: string;
public_folder?: string;
preview_path?: string; preview_path?: string;
preview_path_date_field?: string; preview_path_date_field?: string;
summary?: string; summary?: string;