Re-add ability to parse frontmatter like ---json.

This commit is contained in:
Caleb 2017-09-14 09:43:12 -06:00
parent 335dd15e6d
commit 9f40edaf52

View File

@ -2,21 +2,42 @@ import matter from 'gray-matter';
import tomlEng from 'toml';
import YAML from './yaml';
const parsers = {
toml: tomlEng.parse.bind(tomlEng),
json: (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 matter.engines.json.parse(JSONinput);
},
}
function inferFrontmatterFormat(str) {
const firstLine = str.substr(0, str.indexOf('\n')).trim();
if ((firstLine.length > 3) && (firstLine.substr(0, 3) === "---")) {
// No need to infer, `gray-matter` will handle things like `---toml` for us.
return;
}
switch (firstLine) {
case "---":
return { language: "yaml", delimiters: "---" };
case "+++":
return { language: "toml", delimiters: "+++", engines: { toml: tomlEng.parse.bind(tomlEng) } };
return { language: "toml", delimiters: "+++" };
case "{":
return { language: "json", delimiters: ["{", "}"], engines: { json: ((input) => matter.engines.json.parse('{' + input + '}')) } };
return { language: "json", delimiters: ["{", "}"] };
default:
throw "Unrecgonized front-matter format.";
}
}
export default class Frontmatter {
fromFile(content) {
const result = matter(content, inferFrontmatterFormat(content));
const result = matter(content, { engines: parsers, ...inferFrontmatterFormat(content) });
const data = result.data;
data.body = result.content;
return data;