static-cms/src/formats/formats.js

43 lines
1.0 KiB
JavaScript
Raw Normal View History

import yamlFormatter from './yaml';
import tomlFormatter from './toml';
import jsonFormatter from './json';
import FrontmatterFormatter from './frontmatter';
export const formatToExtension = format => ({
markdown: 'md',
yaml: 'yml',
toml: 'toml',
json: 'json',
html: 'html',
}[format]);
2016-09-11 23:08:18 +02:00
export function formatByExtension(extension) {
2016-09-13 14:53:50 +02:00
return {
2016-10-27 15:33:15 +02:00
yml: yamlFormatter,
yaml: yamlFormatter,
toml: tomlFormatter,
2016-10-27 15:33:15 +02:00
json: jsonFormatter,
md: FrontmatterFormatter,
markdown: FrontmatterFormatter,
html: FrontmatterFormatter,
}[extension] || FrontmatterFormatter;
2016-09-13 14:53:50 +02:00
}
function formatByName(name) {
return {
yml: yamlFormatter,
2016-10-27 15:33:15 +02:00
yaml: yamlFormatter,
toml: tomlFormatter,
2017-11-18 08:17:49 -07:00
json: jsonFormatter,
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-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'));
}