fix(config): strip leading slashes from collection location config (#2131)

This commit is contained in:
Shawn Erquhart 2019-03-01 09:45:23 -05:00 committed by GitHub
parent 686504adee
commit efa650ccf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 6 deletions

View File

@ -8,6 +8,7 @@ describe('config', () => {
foo: 'bar',
media_folder: 'path/to/media',
public_folder: '/path/to/media',
collections: [],
});
expect(applyDefaults(config)).toEqual(config.set('publish_mode', 'simple'));
});
@ -18,6 +19,7 @@ describe('config', () => {
publish_mode: 'complex',
media_folder: 'path/to/media',
public_folder: '/path/to/media',
collections: [],
});
expect(applyDefaults(config)).toEqual(config);
});
@ -28,6 +30,7 @@ describe('config', () => {
fromJS({
foo: 'bar',
media_folder: 'path/to/media',
collections: [],
}),
),
).toEqual(
@ -36,6 +39,7 @@ describe('config', () => {
publish_mode: 'simple',
media_folder: 'path/to/media',
public_folder: '/path/to/media',
collections: [],
}),
);
});
@ -47,6 +51,7 @@ describe('config', () => {
foo: 'bar',
media_folder: 'path/to/media',
public_folder: '/publib/path',
collections: [],
}),
),
).toEqual(
@ -55,6 +60,39 @@ describe('config', () => {
publish_mode: 'simple',
media_folder: 'path/to/media',
public_folder: '/publib/path',
collections: [],
}),
);
});
it('should strip leading slashes from collection folder', () => {
expect(
applyDefaults(
fromJS({
collections: [{ folder: '/foo' }],
}),
),
).toEqual(
fromJS({
publish_mode: 'simple',
public_folder: '/',
collections: [{ folder: 'foo' }],
}),
);
});
it('should strip leading slashes from collection files', () => {
expect(
applyDefaults(
fromJS({
collections: [{ files: [{ file: '/foo' }] }],
}),
),
).toEqual(
fromJS({
publish_mode: 'simple',
public_folder: '/',
collections: [{ files: [{ file: 'foo' }] }],
}),
);
});

View File

@ -30,19 +30,37 @@ export function applyDefaults(config) {
return Map(defaults)
.mergeDeep(config)
.withMutations(map => {
/**
* Use `site_url` as default `display_url`.
*/
// Use `site_url` as default `display_url`.
if (!map.get('display_url') && map.get('site_url')) {
map.set('display_url', map.get('site_url'));
}
/**
* Use media_folder as default public_folder.
*/
// Use media_folder as default public_folder.
const defaultPublicFolder = `/${trimStart(map.get('media_folder'), '/')}`;
if (!map.get('public_folder')) {
map.set('public_folder', defaultPublicFolder);
}
// Strip leading slash from collection folders and files
map.set(
'collections',
map.get('collections').map(collection => {
const folder = collection.get('folder');
if (folder) {
return collection.set('folder', trimStart(folder, '/'));
}
const files = collection.get('files');
if (files) {
return collection.set(
'files',
files.map(file => {
return file.set('file', trimStart(file.get('file'), '/'));
}),
);
}
}),
);
});
}