Add support for TOML files. (#740)
* Move `sortKeys` into a helper function. * Add support for TOML files.
This commit is contained in:
parent
3d65cc380e
commit
2ef6556d4a
@ -180,6 +180,7 @@
|
||||
"slate-soft-break": "^0.4.0",
|
||||
"slug": "^0.9.1",
|
||||
"toml-j0.4": "^1.1.1",
|
||||
"tomlify-j0.4": "^3.0.0-alpha.0",
|
||||
"unified": "^6.1.4",
|
||||
"unist-builder": "^1.0.2",
|
||||
"unist-util-visit-parents": "^1.1.1",
|
||||
|
@ -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);
|
||||
|
@ -9139,6 +9139,10 @@ toml-j0.4@^1.1.1:
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/toml-j0.4/-/toml-j0.4-1.1.1.tgz#eb0c70348609a0263bb1d6e4a3dd191dcca60866"
|
||||
|
||||
tomlify-j0.4@^3.0.0-alpha.0:
|
||||
version "3.0.0-alpha.0"
|
||||
resolved "https://registry.yarnpkg.com/tomlify-j0.4/-/tomlify-j0.4-3.0.0-alpha.0.tgz#f5ed30adfde71e60084dea80aa39c1be2046a7fc"
|
||||
|
||||
topbar@^0.1.3:
|
||||
version "0.1.3"
|
||||
resolved "https://registry.yarnpkg.com/topbar/-/topbar-0.1.3.tgz#c9ef8776dc4469f7840e6416f4136ddeccf4b7c6"
|
||||
|
Loading…
x
Reference in New Issue
Block a user