2018-02-08 19:18:33 -07:00
|
|
|
import { List } from 'immutable';
|
2017-10-30 13:48:19 -06:00
|
|
|
import yamlFormatter from './yaml';
|
|
|
|
import tomlFormatter from './toml';
|
|
|
|
import jsonFormatter from './json';
|
2018-02-08 20:04:42 -05:00
|
|
|
import { FrontmatterInfer, frontmatterJSON, frontmatterTOML, frontmatterYAML } from './frontmatter';
|
|
|
|
|
|
|
|
export const frontmatterFormats = ['yaml-frontmatter','toml-frontmatter','json-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',
|
2018-01-29 15:35:36 -07:00
|
|
|
'json-frontmatter',
|
|
|
|
'toml-frontmatter',
|
|
|
|
'yaml-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',
|
2018-01-29 15:35:36 -07:00
|
|
|
'json-frontmatter': 'md',
|
|
|
|
'toml-frontmatter': 'md',
|
|
|
|
'yaml-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,
|
2018-01-29 15:35:36 -07:00
|
|
|
md: FrontmatterInfer,
|
|
|
|
markdown: FrontmatterInfer,
|
|
|
|
html: FrontmatterInfer,
|
2017-12-02 09:22:44 -07:00
|
|
|
}[extension];
|
2016-09-13 14:53:50 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 20:04:42 -05:00
|
|
|
function formatByName(name, customDelimiter) {
|
2016-09-13 14:53:50 +02:00
|
|
|
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,
|
2018-01-29 15:35:36 -07:00
|
|
|
frontmatter: FrontmatterInfer,
|
2018-02-08 20:04:42 -05:00
|
|
|
'json-frontmatter': frontmatterJSON(customDelimiter),
|
|
|
|
'toml-frontmatter': frontmatterTOML(customDelimiter),
|
|
|
|
'yaml-frontmatter': frontmatterYAML(customDelimiter),
|
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) {
|
2018-02-08 20:04:42 -05:00
|
|
|
// Check for custom delimiter
|
2018-02-08 19:18:33 -07:00
|
|
|
const frontmatter_delimiter = collectionOrEntity.get('frontmatter_delimiter');
|
|
|
|
const customDelimiter = List.isList(frontmatter_delimiter) ? frontmatter_delimiter.toArray() : frontmatter_delimiter;
|
|
|
|
|
2017-11-05 08:48:57 -07:00
|
|
|
// If the format is specified in the collection, use that format.
|
2018-02-08 20:04:42 -05:00
|
|
|
const formatSpecification = collectionOrEntity.get('format');
|
|
|
|
if (formatSpecification) {
|
|
|
|
return formatByName(formatSpecification, customDelimiter);
|
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.
|
2018-02-08 20:04:42 -05:00
|
|
|
return formatByName('frontmatter', customDelimiter);
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|