Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: multi content authoring #2988

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/netlify-cms-core/src/actions/__tests__/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,33 @@ describe('config', () => {
]),
).toEqual('_');
});

it('should add langauge fields', () => {
const inputFields = [
{ label: 'Title', name: 'title', widget: 'string', translate: true },
{ label: 'Slug', name: 'slug', widget: 'string' },
];
const outputFields = [
{
label: 'Title',
name: 'title',
widget: 'object',
fields: [
{ label: 'EN', name: 'en', widget: 'string' },
{ label: 'FR', name: 'fr', widget: 'string' },
],
},
{ label: 'Slug', name: 'slug', widget: 'string' },
];

expect(
applyDefaults(
fromJS({
languages: ['en', 'fr'],
collections: [{ folder: '_posts', fields: inputFields }],
}),
).get('collections'),
).toEqual(fromJS([{ folder: '_posts', fields: outputFields }]));
});
});
});
32 changes: 31 additions & 1 deletion packages/netlify-cms-core/src/actions/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import yaml from 'js-yaml';
import { Map, fromJS } from 'immutable';
import { trimStart, get } from 'lodash';
import { trimStart, get, cloneDeep, omit } from 'lodash';
import { authenticateUser } from 'Actions/auth';
import * as publishModes from 'Constants/publishModes';
import { validateConfig } from 'Constants/configSchema';
import { selectIdentifier } from 'Reducers/collections';

export const CONFIG_REQUEST = 'CONFIG_REQUEST';
export const CONFIG_SUCCESS = 'CONFIG_SUCCESS';
Expand Down Expand Up @@ -54,6 +55,7 @@ export function applyDefaults(config) {
map.setIn(['slug', 'sanitize_replacement'], '-');
}

const langs = map.get('languages');
// Strip leading slash from collection folders and files
map.set(
'collections',
Expand All @@ -64,6 +66,18 @@ export function applyDefaults(config) {
// default value for media folder when using the path config
collection = collection.set('media_folder', '');
}

const fields = collection.get('fields');
if (langs && fields) {
// add languague fields
collection = collection.set(
'fields',
fromJS(
addLanguageFields(fields.toJS(), langs.toJS(), selectIdentifier(collection)),
),
);
}

return collection.set('folder', trimStart(folder, '/'));
}

Expand All @@ -81,6 +95,22 @@ export function applyDefaults(config) {
});
}

export function addLanguageFields(fields, langs, identifier) {
return fields.reduce((acc, item) => {
const name = item.name;
// exclude identifier and body fields
if (item.translate && name !== identifier && name !== 'body') {
const langFields = langs.map(lang => {
return { ...omit(cloneDeep(item), 'translate'), label: lang.toUpperCase(), name: lang };
});
const newField = { ...omit(item, 'translate'), widget: 'object', fields: langFields };
return [...acc, newField];
}

return [...acc, item];
}, []);
}

function mergePreloadedConfig(preloadedConfig, loadedConfig) {
const map = fromJS(loadedConfig) || Map();
return preloadedConfig ? preloadedConfig.mergeDeep(map) : map;
Expand Down