From 4a9a33c5f4752cac6d29f74f0e8f600ff67ff2f1 Mon Sep 17 00:00:00 2001 From: Caleb Date: Fri, 17 Nov 2017 20:11:23 -0700 Subject: [PATCH 1/4] Split collection config validator. Also clarify collection validation messages. --- src/reducers/collections.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/reducers/collections.js b/src/reducers/collections.js index 118d9489c5d2..01257c4e8e39 100644 --- a/src/reducers/collections.js +++ b/src/reducers/collections.js @@ -12,12 +12,11 @@ const collections = (state = null, action) => { case CONFIG_SUCCESS: return OrderedMap().withMutations((map) => { (configCollections || []).forEach((configCollection) => { + validateCollection(configCollection); if (has(configCollection, 'folder')) { configCollection.type = FOLDER; // eslint-disable-line no-param-reassign } else if (has(configCollection, 'files')) { configCollection.type = FILES; // eslint-disable-line no-param-reassign - } else { - throw new Error('Unknown collection type. Collections can be either Folder based or File based. Please verify your site configuration'); } map.set(configCollection.name, fromJS(configCollection)); }); @@ -27,6 +26,13 @@ const collections = (state = null, action) => { } }; +function validateCollection(configCollection) { + const collectionName = get(configCollection, 'name'); + if (!has(configCollection, 'folder') && !has(configCollection, 'files')) { + throw new Error(`Unknown collection type for collection "${ collectionName }". Collections can be either Folder based or File based.`); + } +} + const selectors = { [FOLDER]: { entryExtension(collection) { From 8ea31f61d63cbff5275218ea8a98cfb70201fe8b Mon Sep 17 00:00:00 2001 From: Caleb Date: Fri, 17 Nov 2017 21:05:42 -0700 Subject: [PATCH 2/4] Throw config error if unsupported format. --- src/formats/formats.js | 15 +++++++++++++-- src/reducers/collections.js | 7 +++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/formats/formats.js b/src/formats/formats.js index be48fa28af82..81566148f9cc 100644 --- a/src/formats/formats.js +++ b/src/formats/formats.js @@ -3,6 +3,14 @@ import tomlFormatter from './toml'; import jsonFormatter from './json'; import FrontmatterFormatter from './frontmatter'; +export const supportedFormats = [ + 'markdown', + 'yaml', + 'toml', + 'json', + 'html', +]; + export const formatToExtension = format => ({ markdown: 'md', yaml: 'yml', @@ -29,8 +37,11 @@ function formatByName(name) { yaml: yamlFormatter, toml: tomlFormatter, json: jsonFormatter, + md: FrontmatterFormatter, + markdown: FrontmatterFormatter, + html: FrontmatterFormatter, frontmatter: FrontmatterFormatter, - }[name] || FrontmatterFormatter; + }[name]; } export function resolveFormat(collectionOrEntity, entry) { @@ -55,5 +66,5 @@ export function resolveFormat(collectionOrEntity, entry) { } // If no format is specified and it cannot be inferred, return the default. - return formatByName(); + return formatByName('frontmatter'); } diff --git a/src/reducers/collections.js b/src/reducers/collections.js index 01257c4e8e39..e983312df9f0 100644 --- a/src/reducers/collections.js +++ b/src/reducers/collections.js @@ -1,10 +1,10 @@ import { OrderedMap, fromJS } from 'immutable'; -import { has } from 'lodash'; +import { has, get } from 'lodash'; import consoleError from '../lib/consoleError'; import { CONFIG_SUCCESS } from '../actions/config'; import { FILES, FOLDER } from '../constants/collectionTypes'; import { INFERABLE_FIELDS } from '../constants/fieldInference'; -import { formatToExtension } from '../formats/formats'; +import { formatToExtension, supportedFormats } from '../formats/formats'; const collections = (state = null, action) => { const configCollections = action.payload && action.payload.collections; @@ -31,6 +31,9 @@ function validateCollection(configCollection) { if (!has(configCollection, 'folder') && !has(configCollection, 'files')) { throw new Error(`Unknown collection type for collection "${ collectionName }". Collections can be either Folder based or File based.`); } + if (has(configCollection, 'format') && !supportedFormats.includes(get(configCollection, 'format'))) { + throw new Error(`Unknown collection format for collection "${ collectionName }". Supported formats are ${ supportedFormats.join(',') }`); + } } const selectors = { From d62d8506e4304c119accd2aa79afd3076cb08508 Mon Sep 17 00:00:00 2001 From: Caleb Date: Fri, 1 Dec 2017 16:56:29 -0700 Subject: [PATCH 3/4] Remove support for markdown and html as explicit formats. The `frontmatter` format is the correct one to use here. --- src/formats/formats.js | 11 ++++------- src/reducers/collections.js | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/formats/formats.js b/src/formats/formats.js index 81566148f9cc..2fd5e66a5c7f 100644 --- a/src/formats/formats.js +++ b/src/formats/formats.js @@ -4,19 +4,19 @@ import jsonFormatter from './json'; import FrontmatterFormatter from './frontmatter'; export const supportedFormats = [ - 'markdown', + 'yml', 'yaml', 'toml', 'json', - 'html', + 'frontmatter', ]; export const formatToExtension = format => ({ - markdown: 'md', + yml: 'yml', yaml: 'yml', toml: 'toml', json: 'json', - html: 'html', + frontmatter: 'md', }[format]); export function formatByExtension(extension) { @@ -37,9 +37,6 @@ function formatByName(name) { yaml: yamlFormatter, toml: tomlFormatter, json: jsonFormatter, - md: FrontmatterFormatter, - markdown: FrontmatterFormatter, - html: FrontmatterFormatter, frontmatter: FrontmatterFormatter, }[name]; } diff --git a/src/reducers/collections.js b/src/reducers/collections.js index e983312df9f0..4cd50cd14323 100644 --- a/src/reducers/collections.js +++ b/src/reducers/collections.js @@ -39,7 +39,7 @@ function validateCollection(configCollection) { const selectors = { [FOLDER]: { entryExtension(collection) { - return collection.get('extension') || formatToExtension(collection.get('format') || 'markdown'); + return collection.get('extension') || formatToExtension(collection.get('format') || 'frontmatter'); }, fields(collection) { return collection.get('fields'); From dede8e063068820e9d58b46195d6c37f78992123 Mon Sep 17 00:00:00 2001 From: Caleb Date: Sat, 2 Dec 2017 09:22:44 -0700 Subject: [PATCH 4/4] Throw if cannot infer format from file extension. --- src/formats/formats.js | 2 +- src/reducers/collections.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/formats/formats.js b/src/formats/formats.js index 2fd5e66a5c7f..6d85b045c500 100644 --- a/src/formats/formats.js +++ b/src/formats/formats.js @@ -28,7 +28,7 @@ export function formatByExtension(extension) { md: FrontmatterFormatter, markdown: FrontmatterFormatter, html: FrontmatterFormatter, - }[extension] || FrontmatterFormatter; + }[extension]; } function formatByName(name) { diff --git a/src/reducers/collections.js b/src/reducers/collections.js index 4cd50cd14323..9e0f967f86e8 100644 --- a/src/reducers/collections.js +++ b/src/reducers/collections.js @@ -4,7 +4,7 @@ import consoleError from '../lib/consoleError'; import { CONFIG_SUCCESS } from '../actions/config'; import { FILES, FOLDER } from '../constants/collectionTypes'; import { INFERABLE_FIELDS } from '../constants/fieldInference'; -import { formatToExtension, supportedFormats } from '../formats/formats'; +import { formatByExtension, formatToExtension, supportedFormats } from '../formats/formats'; const collections = (state = null, action) => { const configCollections = action.payload && action.payload.collections; @@ -32,7 +32,11 @@ function validateCollection(configCollection) { throw new Error(`Unknown collection type for collection "${ collectionName }". Collections can be either Folder based or File based.`); } if (has(configCollection, 'format') && !supportedFormats.includes(get(configCollection, 'format'))) { - throw new Error(`Unknown collection format for collection "${ collectionName }". Supported formats are ${ supportedFormats.join(',') }`); + throw new Error(`Unknown collection format for collection "${ collectionName }". Supported formats are ${ supportedFormats.join(',') }`); + } + if (!has(configCollection, 'format') && has(configCollection, 'extension') && !formatByExtension(get(configCollection, 'extension'))) { + // Cannot infer format from extension. + throw new Error(`Please set a format for collection "${ collectionName }". Supported formats are ${ supportedFormats.join(',') }`); } }