2017-09-13 08:55:10 -06:00
|
|
|
import matter from 'gray-matter';
|
2017-10-30 13:48:19 -06:00
|
|
|
import tomlFormatter from './toml';
|
|
|
|
import yamlFormatter from './yaml';
|
|
|
|
import jsonFormatter from './json';
|
2017-10-26 12:43:28 -06:00
|
|
|
|
2017-09-14 09:43:12 -06:00
|
|
|
const parsers = {
|
2018-01-29 15:35:36 -07:00
|
|
|
toml: {
|
|
|
|
parse: input => tomlFormatter.fromFile(input),
|
|
|
|
stringify: (metadata, { sortedKeys }) => tomlFormatter.toFile(metadata, sortedKeys),
|
|
|
|
},
|
|
|
|
json: {
|
|
|
|
parse: input => {
|
|
|
|
let JSONinput = input.trim();
|
|
|
|
// Fix JSON if leading and trailing brackets were trimmed.
|
|
|
|
if (JSONinput.substr(0, 1) !== '{') {
|
|
|
|
JSONinput = '{' + JSONinput;
|
|
|
|
}
|
|
|
|
if (JSONinput.substr(-1) !== '}') {
|
|
|
|
JSONinput = JSONinput + '}';
|
|
|
|
}
|
|
|
|
return jsonFormatter.fromFile(JSONinput);
|
|
|
|
},
|
|
|
|
stringify: (metadata, { sortedKeys }) => {
|
|
|
|
let JSONoutput = jsonFormatter.toFile(metadata, sortedKeys).trim();
|
|
|
|
// Trim leading and trailing brackets.
|
|
|
|
if (JSONoutput.substr(0, 1) === '{' && JSONoutput.substr(-1) === '}') {
|
|
|
|
JSONoutput = JSONoutput.substring(1, JSONoutput.length - 1);
|
|
|
|
}
|
|
|
|
return JSONoutput;
|
|
|
|
},
|
2017-10-30 13:48:19 -06:00
|
|
|
},
|
|
|
|
yaml: {
|
|
|
|
parse: input => yamlFormatter.fromFile(input),
|
|
|
|
stringify: (metadata, { sortedKeys }) => yamlFormatter.toFile(metadata, sortedKeys),
|
2017-09-14 09:43:12 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-12 20:51:40 -06:00
|
|
|
function inferFrontmatterFormat(str) {
|
|
|
|
const firstLine = str.substr(0, str.indexOf('\n')).trim();
|
2017-09-14 09:43:12 -06:00
|
|
|
if ((firstLine.length > 3) && (firstLine.substr(0, 3) === "---")) {
|
|
|
|
// No need to infer, `gray-matter` will handle things like `---toml` for us.
|
|
|
|
return;
|
|
|
|
}
|
2017-09-12 20:51:40 -06:00
|
|
|
switch (firstLine) {
|
|
|
|
case "---":
|
2018-01-29 15:35:36 -07:00
|
|
|
return getFormatOpts('yaml');
|
2017-09-12 20:51:40 -06:00
|
|
|
case "+++":
|
2018-01-29 15:35:36 -07:00
|
|
|
return getFormatOpts('toml');
|
2017-09-12 20:51:40 -06:00
|
|
|
case "{":
|
2018-01-29 15:35:36 -07:00
|
|
|
return getFormatOpts('json');
|
2017-09-14 09:43:12 -06:00
|
|
|
default:
|
2017-10-26 13:16:44 -06:00
|
|
|
throw "Unrecognized front-matter format.";
|
2017-09-12 20:51:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 15:35:36 -07:00
|
|
|
export const getFormatOpts = format => ({
|
|
|
|
yaml: { language: "yaml", delimiters: "---" },
|
|
|
|
toml: { language: "toml", delimiters: "+++" },
|
|
|
|
json: { language: "json", delimiters: ["{", "}"] },
|
|
|
|
}[format]);
|
|
|
|
|
|
|
|
class FrontmatterFormatter {
|
|
|
|
constructor(format) {
|
|
|
|
this.format = getFormatOpts(format);
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
fromFile(content) {
|
2018-01-29 15:35:36 -07:00
|
|
|
const format = this.format || inferFrontmatterFormat(content);
|
|
|
|
const result = matter(content, { engines: parsers, ...format });
|
2017-10-30 13:48:19 -06:00
|
|
|
return {
|
|
|
|
...result.data,
|
|
|
|
body: result.content,
|
|
|
|
};
|
2018-01-29 15:35:36 -07:00
|
|
|
}
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2017-01-19 12:09:18 -02:00
|
|
|
toFile(data, sortedKeys) {
|
2017-11-11 15:52:02 -05:00
|
|
|
const { body = '', ...meta } = data;
|
2017-04-20 15:37:52 -04:00
|
|
|
|
2018-01-29 15:35:36 -07:00
|
|
|
// Stringify to YAML if the format was not set
|
|
|
|
const format = this.format || getFormatOpts('yaml');
|
|
|
|
|
2017-10-30 13:48:19 -06:00
|
|
|
// `sortedKeys` is not recognized by gray-matter, so it gets passed through to the parser
|
2018-01-29 15:35:36 -07:00
|
|
|
return matter.stringify(body, meta, { engines: parsers, sortedKeys, ...format });
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
}
|
2018-01-29 15:35:36 -07:00
|
|
|
|
|
|
|
export const FrontmatterInfer = new FrontmatterFormatter();
|
|
|
|
export const FrontmatterYAML = new FrontmatterFormatter('yaml');
|
|
|
|
export const FrontmatterTOML = new FrontmatterFormatter('toml');
|
|
|
|
export const FrontmatterJSON = new FrontmatterFormatter('json');
|