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",
|
"slate-soft-break": "^0.4.0",
|
||||||
"slug": "^0.9.1",
|
"slug": "^0.9.1",
|
||||||
"toml-j0.4": "^1.1.1",
|
"toml-j0.4": "^1.1.1",
|
||||||
|
"tomlify-j0.4": "^3.0.0-alpha.0",
|
||||||
"unified": "^6.1.4",
|
"unified": "^6.1.4",
|
||||||
"unist-builder": "^1.0.2",
|
"unist-builder": "^1.0.2",
|
||||||
"unist-util-visit-parents": "^1.1.1",
|
"unist-util-visit-parents": "^1.1.1",
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import YAML from './yaml';
|
import YAML from './yaml';
|
||||||
|
import TOML from './toml';
|
||||||
import JSONFormatter from './json';
|
import JSONFormatter from './json';
|
||||||
import Frontmatter from './frontmatter';
|
import Frontmatter from './frontmatter';
|
||||||
|
|
||||||
const yamlFormatter = new YAML();
|
const yamlFormatter = new YAML();
|
||||||
|
const tomlFormatter = new TOML();
|
||||||
const jsonFormatter = new JSONFormatter();
|
const jsonFormatter = new JSONFormatter();
|
||||||
const FrontmatterFormatter = new Frontmatter();
|
const FrontmatterFormatter = new Frontmatter();
|
||||||
|
|
||||||
@ -16,6 +18,7 @@ export function formatByExtension(extension) {
|
|||||||
return {
|
return {
|
||||||
yml: yamlFormatter,
|
yml: yamlFormatter,
|
||||||
yaml: yamlFormatter,
|
yaml: yamlFormatter,
|
||||||
|
toml: tomlFormatter,
|
||||||
json: jsonFormatter,
|
json: jsonFormatter,
|
||||||
md: FrontmatterFormatter,
|
md: FrontmatterFormatter,
|
||||||
markdown: FrontmatterFormatter,
|
markdown: FrontmatterFormatter,
|
||||||
@ -27,6 +30,7 @@ function formatByName(name) {
|
|||||||
return {
|
return {
|
||||||
yml: yamlFormatter,
|
yml: yamlFormatter,
|
||||||
yaml: yamlFormatter,
|
yaml: yamlFormatter,
|
||||||
|
toml: tomlFormatter,
|
||||||
frontmatter: FrontmatterFormatter,
|
frontmatter: FrontmatterFormatter,
|
||||||
}[name] || FrontmatterFormatter;
|
}[name] || FrontmatterFormatter;
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import matter from 'gray-matter';
|
import matter from 'gray-matter';
|
||||||
import tomlEng from 'toml-j0.4';
|
import TOML from './toml';
|
||||||
import YAML from './yaml';
|
import YAML from './yaml';
|
||||||
|
|
||||||
|
const tomlFormatter = new TOML();
|
||||||
|
|
||||||
const parsers = {
|
const parsers = {
|
||||||
toml: tomlEng.parse.bind(tomlEng),
|
toml: tomlFormatter.fromFile.bind(tomlFormatter),
|
||||||
json: (input) => {
|
json: (input) => {
|
||||||
let JSONinput = input.trim();
|
let JSONinput = input.trim();
|
||||||
// Fix JSON if leading and trailing brackets were trimmed.
|
// 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 yaml from 'js-yaml';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import AssetProxy from '../valueObjects/AssetProxy';
|
import AssetProxy from '../valueObjects/AssetProxy';
|
||||||
|
import { sortKeys } from './helpers';
|
||||||
|
|
||||||
const MomentType = new yaml.Type('date', {
|
const MomentType = new yaml.Type('date', {
|
||||||
kind: 'scalar',
|
kind: 'scalar',
|
||||||
@ -35,20 +36,6 @@ const OutputSchema = new yaml.Schema({
|
|||||||
explicit: yaml.DEFAULT_SAFE_SCHEMA.explicit,
|
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 {
|
export default class YAML {
|
||||||
fromFile(content) {
|
fromFile(content) {
|
||||||
return yaml.safeLoad(content);
|
return yaml.safeLoad(content);
|
||||||
|
@ -9139,6 +9139,10 @@ toml-j0.4@^1.1.1:
|
|||||||
version "1.1.1"
|
version "1.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/toml-j0.4/-/toml-j0.4-1.1.1.tgz#eb0c70348609a0263bb1d6e4a3dd191dcca60866"
|
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:
|
topbar@^0.1.3:
|
||||||
version "0.1.3"
|
version "0.1.3"
|
||||||
resolved "https://registry.yarnpkg.com/topbar/-/topbar-0.1.3.tgz#c9ef8776dc4469f7840e6416f4136ddeccf4b7c6"
|
resolved "https://registry.yarnpkg.com/topbar/-/topbar-0.1.3.tgz#c9ef8776dc4469f7840e6416f4136ddeccf4b7c6"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user