diff --git a/package.json b/package.json index 58743230..26e0eb91 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/formats/formats.js b/src/formats/formats.js index fc7740fe..d529323a 100644 --- a/src/formats/formats.js +++ b/src/formats/formats.js @@ -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; } diff --git a/src/formats/frontmatter.js b/src/formats/frontmatter.js index 3f5b7df2..43aadf46 100644 --- a/src/formats/frontmatter.js +++ b/src/formats/frontmatter.js @@ -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. diff --git a/src/formats/helpers.js b/src/formats/helpers.js new file mode 100644 index 00000000..896cc4c2 --- /dev/null +++ b/src/formats/helpers.js @@ -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; +}; diff --git a/src/formats/toml.js b/src/formats/toml.js new file mode 100644 index 00000000..4255c685 --- /dev/null +++ b/src/formats/toml.js @@ -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) }); + } +} diff --git a/src/formats/yaml.js b/src/formats/yaml.js index 64b9f51c..fec213a7 100644 --- a/src/formats/yaml.js +++ b/src/formats/yaml.js @@ -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); diff --git a/yarn.lock b/yarn.lock index a0b7fd38..6966738e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"