2017-10-30 13:48:19 -06:00
|
|
|
import yamlFormatter from './yaml';
|
|
|
|
import tomlFormatter from './toml';
|
|
|
|
import jsonFormatter from './json';
|
|
|
|
import FrontmatterFormatter from './frontmatter';
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2017-11-17 21:05:42 -07:00
|
|
|
export const supportedFormats = [
|
2017-12-01 16:56:29 -07:00
|
|
|
'yml',
|
2017-11-17 21:05:42 -07:00
|
|
|
'yaml',
|
|
|
|
'toml',
|
|
|
|
'json',
|
2017-12-01 16:56:29 -07:00
|
|
|
'frontmatter',
|
2017-11-17 21:05:42 -07:00
|
|
|
];
|
|
|
|
|
2017-10-30 13:48:19 -06:00
|
|
|
export const formatToExtension = format => ({
|
2017-12-01 16:56:29 -07:00
|
|
|
yml: 'yml',
|
2017-10-30 13:48:19 -06:00
|
|
|
yaml: 'yml',
|
|
|
|
toml: 'toml',
|
|
|
|
json: 'json',
|
2017-12-01 16:56:29 -07:00
|
|
|
frontmatter: 'md',
|
2017-10-30 13:48:19 -06:00
|
|
|
}[format]);
|
2016-09-11 23:08:18 +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-11-18 08:17:49 -07:00
|
|
|
json: jsonFormatter,
|
2017-04-09 19:32:33 +01:00
|
|
|
frontmatter: FrontmatterFormatter,
|
2017-11-17 21:05:42 -07:00
|
|
|
}[name];
|
2016-09-13 14:53:50 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:04:17 -03:00
|
|
|
export function resolveFormat(collectionOrEntity, entry) {
|
2017-11-05 08:48:57 -07:00
|
|
|
// If the format is specified in the collection, use that format.
|
|
|
|
const format = collectionOrEntity.get('format');
|
|
|
|
if (format) {
|
|
|
|
return formatByName(format);
|
2016-09-11 23:08:18 +02:00
|
|
|
}
|
2017-11-05 08:48:57 -07:00
|
|
|
|
|
|
|
// If a file already exists, infer the format from its file extension.
|
|
|
|
const filePath = entry && entry.path;
|
|
|
|
if (filePath) {
|
|
|
|
const fileExtension = filePath.split('.').pop();
|
|
|
|
return formatByExtension(fileExtension);
|
|
|
|
}
|
|
|
|
|
2017-11-09 20:26:53 -07:00
|
|
|
// If creating a new file, and an `extension` is specified in the
|
|
|
|
// collection config, infer the format from that extension.
|
|
|
|
const extension = collectionOrEntity.get('extension');
|
|
|
|
if (extension) {
|
|
|
|
return formatByExtension(extension);
|
|
|
|
}
|
|
|
|
|
2017-11-05 08:48:57 -07:00
|
|
|
// If no format is specified and it cannot be inferred, return the default.
|
2017-11-17 21:05:42 -07:00
|
|
|
return formatByName('frontmatter');
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|