Add support for TOML files. (#740)
* Move `sortKeys` into a helper function. * Add support for TOML files.
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
import YAML from './yaml';
|
||||
import TOML from './toml';
|
||||
import JSONFormatter from './json';
|
||||
import Frontmatter from './frontmatter';
|
||||
|
||||
const yamlFormatter = new YAML();
|
||||
const tomlFormatter = new TOML();
|
||||
const jsonFormatter = new JSONFormatter();
|
||||
const FrontmatterFormatter = new Frontmatter();
|
||||
|
||||
@ -16,6 +18,7 @@ export function formatByExtension(extension) {
|
||||
return {
|
||||
yml: yamlFormatter,
|
||||
yaml: yamlFormatter,
|
||||
toml: tomlFormatter,
|
||||
json: jsonFormatter,
|
||||
md: FrontmatterFormatter,
|
||||
markdown: FrontmatterFormatter,
|
||||
@ -27,6 +30,7 @@ function formatByName(name) {
|
||||
return {
|
||||
yml: yamlFormatter,
|
||||
yaml: yamlFormatter,
|
||||
toml: tomlFormatter,
|
||||
frontmatter: FrontmatterFormatter,
|
||||
}[name] || FrontmatterFormatter;
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
import matter from 'gray-matter';
|
||||
import tomlEng from 'toml-j0.4';
|
||||
import TOML from './toml';
|
||||
import YAML from './yaml';
|
||||
|
||||
const tomlFormatter = new TOML();
|
||||
|
||||
const parsers = {
|
||||
toml: tomlEng.parse.bind(tomlEng),
|
||||
toml: tomlFormatter.fromFile.bind(tomlFormatter),
|
||||
json: (input) => {
|
||||
let JSONinput = input.trim();
|
||||
// Fix JSON if leading and trailing brackets were trimmed.
|
||||
|
13
src/formats/helpers.js
Normal file
13
src/formats/helpers.js
Normal file
@ -0,0 +1,13 @@
|
||||
export const sortKeys = (sortedKeys = []) => (a, b) => {
|
||||
const idxA = sortedKeys.indexOf(a);
|
||||
const idxB = sortedKeys.indexOf(b);
|
||||
if (idxA === -1 || idxB === -1) {
|
||||
if (a > b) return 1;
|
||||
if (a < b) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (idxA > idxB) return 1;
|
||||
if (idxA < idxB) return -1;
|
||||
return 0;
|
||||
};
|
26
src/formats/toml.js
Normal file
26
src/formats/toml.js
Normal file
@ -0,0 +1,26 @@
|
||||
import toml from 'toml-j0.4';
|
||||
import tomlify from 'tomlify-j0.4';
|
||||
import moment from 'moment';
|
||||
import AssetProxy from '../valueObjects/AssetProxy';
|
||||
import { sortKeys } from './helpers';
|
||||
|
||||
const outputReplacer = (key, value) => {
|
||||
if (moment.isMoment(value)) {
|
||||
return value.format(value._f);
|
||||
}
|
||||
if (value instanceof AssetProxy) {
|
||||
return `${ value.path }`;
|
||||
}
|
||||
// Return `false` to use default (`undefined` would delete key).
|
||||
return false;
|
||||
};
|
||||
|
||||
export default class TOML {
|
||||
fromFile(content) {
|
||||
return toml.parse(content);
|
||||
}
|
||||
|
||||
toFile(data, sortedKeys = []) {
|
||||
return tomlify.toToml(data, { replace: outputReplacer, sort: sortKeys(sortedKeys) });
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import yaml from 'js-yaml';
|
||||
import moment from 'moment';
|
||||
import AssetProxy from '../valueObjects/AssetProxy';
|
||||
import { sortKeys } from './helpers';
|
||||
|
||||
const MomentType = new yaml.Type('date', {
|
||||
kind: 'scalar',
|
||||
@ -35,20 +36,6 @@ const OutputSchema = new yaml.Schema({
|
||||
explicit: yaml.DEFAULT_SAFE_SCHEMA.explicit,
|
||||
});
|
||||
|
||||
const sortKeys = (sortedKeys = []) => (a, b) => {
|
||||
const idxA = sortedKeys.indexOf(a);
|
||||
const idxB = sortedKeys.indexOf(b);
|
||||
if (idxA === -1 || idxB === -1) {
|
||||
if (a > b) return 1;
|
||||
if (a < b) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (idxA > idxB) return 1;
|
||||
if (idxA < idxB) return -1;
|
||||
return 0;
|
||||
};
|
||||
|
||||
export default class YAML {
|
||||
fromFile(content) {
|
||||
return yaml.safeLoad(content);
|
||||
|
Reference in New Issue
Block a user