2016-09-11 23:08:18 +02:00
|
|
|
import YAML from './yaml';
|
2017-10-26 12:43:28 -06:00
|
|
|
import TOML from './toml';
|
2016-10-27 15:33:15 +02:00
|
|
|
import JSONFormatter from './json';
|
2017-04-09 19:32:33 +01:00
|
|
|
import Frontmatter from './frontmatter';
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2016-09-11 23:08:18 +02:00
|
|
|
const yamlFormatter = new YAML();
|
2017-10-26 12:43:28 -06:00
|
|
|
const tomlFormatter = new TOML();
|
2016-10-27 15:33:15 +02:00
|
|
|
const jsonFormatter = new JSONFormatter();
|
2017-04-09 19:32:33 +01:00
|
|
|
const FrontmatterFormatter = new Frontmatter();
|
2016-09-11 23:08:18 +02:00
|
|
|
|
2016-09-13 14:53:50 +02:00
|
|
|
function formatByType(type) {
|
|
|
|
// Right now the only type is "editorialWorkflow" and
|
|
|
|
// we always returns the same format
|
2017-04-09 19:32:33 +01:00
|
|
|
return FrontmatterFormatter;
|
2016-09-13 14:53:50 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 17:44:50 -08:00
|
|
|
export function formatByExtension(extension) {
|
2016-09-13 14:53:50 +02:00
|
|
|
return {
|
2016-10-27 15:33:15 +02:00
|
|
|
yml: yamlFormatter,
|
2017-08-15 14:42:55 -07:00
|
|
|
yaml: yamlFormatter,
|
2017-10-26 12:43:28 -06:00
|
|
|
toml: tomlFormatter,
|
2016-10-27 15:33:15 +02:00
|
|
|
json: jsonFormatter,
|
2017-04-09 19:32:33 +01:00
|
|
|
md: FrontmatterFormatter,
|
|
|
|
markdown: FrontmatterFormatter,
|
|
|
|
html: FrontmatterFormatter,
|
|
|
|
}[extension] || FrontmatterFormatter;
|
2016-09-13 14:53:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatByName(name) {
|
|
|
|
return {
|
2017-08-15 14:42:55 -07:00
|
|
|
yml: yamlFormatter,
|
2016-10-27 15:33:15 +02:00
|
|
|
yaml: yamlFormatter,
|
2017-10-26 12:43:28 -06:00
|
|
|
toml: tomlFormatter,
|
2017-04-09 19:32:33 +01:00
|
|
|
frontmatter: FrontmatterFormatter,
|
|
|
|
}[name] || FrontmatterFormatter;
|
2016-09-13 14:53:50 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:04:17 -03:00
|
|
|
export function resolveFormat(collectionOrEntity, entry) {
|
2016-09-13 14:53:50 +02:00
|
|
|
if (typeof collectionOrEntity === 'string') {
|
|
|
|
return formatByType(collectionOrEntity);
|
|
|
|
}
|
2016-10-27 15:33:15 +02:00
|
|
|
const path = entry && entry.path;
|
|
|
|
if (path) {
|
|
|
|
return formatByExtension(path.split('.').pop());
|
2016-09-11 23:08:18 +02:00
|
|
|
}
|
2016-09-13 14:53:50 +02:00
|
|
|
return formatByName(collectionOrEntity.get('format'));
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|