static-cms/src/formats/formats.js

38 lines
1.0 KiB
JavaScript
Raw Normal View History

2016-09-11 23:08:18 +02:00
import YAML from './yaml';
import YAMLFrontmatter from './yaml-frontmatter';
2016-09-11 23:08:18 +02:00
const yamlFormatter = new YAML();
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 {
'yml': yamlFormatter,
'md': YamlFrontmatterFormatter,
'markdown': YamlFrontmatterFormatter,
'html': YamlFrontmatterFormatter
}[extension] || YamlFrontmatterFormatter;
}
function formatByName(name) {
return {
'yaml': yamlFormatter,
'frontmatter': YamlFrontmatterFormatter
}[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);
}
if (entry && entry.path) {
return formatByExtension(entry.path.split('.').pop());
2016-09-11 23:08:18 +02:00
}
2016-09-13 14:53:50 +02:00
return formatByName(collectionOrEntity.get('format'));
}