2016-09-11 23:08:18 +02:00
|
|
|
import YAML from './yaml';
|
2016-10-27 15:33:15 +02:00
|
|
|
import JSONFormatter from './json';
|
2016-02-25 20:40:35 -08:00
|
|
|
import YAMLFrontmatter from './yaml-frontmatter';
|
|
|
|
|
2016-09-11 23:08:18 +02:00
|
|
|
const yamlFormatter = new YAML();
|
2016-10-27 15:33:15 +02:00
|
|
|
const jsonFormatter = new JSONFormatter();
|
2016-09-11 23:08:18 +02:00
|
|
|
const YamlFrontmatterFormatter = new YAMLFrontmatter();
|
|
|
|
|
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
|
|
|
|
return YamlFrontmatterFormatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatByExtension(extension) {
|
|
|
|
return {
|
2016-10-27 15:33:15 +02:00
|
|
|
yml: yamlFormatter,
|
|
|
|
json: jsonFormatter,
|
|
|
|
md: YamlFrontmatterFormatter,
|
|
|
|
markdown: YamlFrontmatterFormatter,
|
|
|
|
html: YamlFrontmatterFormatter,
|
2016-09-13 14:53:50 +02:00
|
|
|
}[extension] || YamlFrontmatterFormatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatByName(name) {
|
|
|
|
return {
|
2016-10-27 15:33:15 +02:00
|
|
|
yaml: yamlFormatter,
|
|
|
|
frontmatter: YamlFrontmatterFormatter,
|
2016-09-13 14:53:50 +02:00
|
|
|
}[name] || YamlFrontmatterFormatter;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|