static-cms/src/formats/formats.js
Mathias Biilmann Christensen c23b2fb531 Make list widget sortable
2016-10-30 16:01:10 -07:00

42 lines
1.1 KiB
JavaScript

import YAML from './yaml';
import JSONFormatter from './json';
import YAMLFrontmatter from './yaml-frontmatter';
const yamlFormatter = new YAML();
const jsonFormatter = new JSONFormatter();
const YamlFrontmatterFormatter = new YAMLFrontmatter();
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,
json: jsonFormatter,
md: YamlFrontmatterFormatter,
markdown: YamlFrontmatterFormatter,
html: YamlFrontmatterFormatter,
}[extension] || YamlFrontmatterFormatter;
}
function formatByName(name) {
return {
yaml: yamlFormatter,
frontmatter: YamlFrontmatterFormatter,
}[name] || YamlFrontmatterFormatter;
}
export function resolveFormat(collectionOrEntity, entry) {
if (typeof collectionOrEntity === 'string') {
return formatByType(collectionOrEntity);
}
const path = entry && entry.path;
if (path) {
return formatByExtension(path.split('.').pop());
}
return formatByName(collectionOrEntity.get('format'));
}