Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch back to gray-matter for front-matter parsing. #601

Merged
merged 5 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"dateformat": "^1.0.12",
"deep-equal": "^1.0.1",
"fuzzy": "^0.1.1",
"gray-matter": "^3.0.6",
"history": "^2.1.2",
"immutability-helper": "^2.0.0",
"immutable": "^3.7.6",
Expand All @@ -121,9 +122,6 @@
"moment": "^2.11.2",
"node-sass": "^3.10.0",
"normalize.css": "^4.2.0",
"preliminaries": "^1.3.0",
"preliminaries-parser-toml": "^1.3.0",
"preliminaries-parser-yaml": "^1.3.0",
"prismjs": "^1.5.1",
"prop-types": "^15.5.10",
"react": "^15.1.0",
Expand Down Expand Up @@ -164,6 +162,7 @@
"slate-edit-table": "^0.10.1",
"slate-soft-break": "^0.3.0",
"slug": "^0.9.1",
"toml": "^2.3.3",
"unified": "^6.1.4",
"unist-builder": "^1.0.2",
"unist-util-visit-parents": "^1.1.1",
Expand Down
45 changes: 36 additions & 9 deletions src/formats/frontmatter.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import preliminaries from 'preliminaries';
import yamlParser from 'preliminaries-parser-yaml';
import tomlParser from 'preliminaries-parser-toml';
import matter from 'gray-matter';
import tomlEng from 'toml';
import YAML from './yaml';

// Automatically register parsers
preliminaries(true);
yamlParser(true);
tomlParser(true);
const parsers = {
toml: tomlEng.parse.bind(tomlEng),
json: (input) => {
let JSONinput = input.trim();
// Fix JSON if leading and trailing brackets were trimmed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would brackets be trimmed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delimiters always get trimmed. For stuff like YAML where the delims are --- and not actually part of the code that is helpful. But with JSON, the delimiters { and } get trimmed, but they actually need to be in there for the parser. With preliminaries they did this too, here: preliminaries/index.js#L376-L379

We can't just add them all the time, though, we have to if-check first, because they could have wrapped the whole thing in a ---json delimiter, in which case the brackets wouldn't get trimmed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: "+++" };
case "{":
return { language: "json", delimiters: ["{", "}"] };
default:
throw "Unrecgonized front-matter format.";
}
}

export default class Frontmatter {
fromFile(content) {
const result = preliminaries.parse(content);
const result = matter(content, { engines: parsers, ...inferFrontmatterFormat(content) });
const data = result.data;
data.body = result.content;
return data;
Expand All @@ -33,6 +60,6 @@ export default class Frontmatter {
return new YAML().toFile(metadata, sortedKeys);
},
};
return preliminaries.stringify(body, meta, { lang: "yaml", delims: "---", parser });
return matter.stringify(body, meta, { language: "yaml", delimiters: "---", engines: { yaml: parser } });
}
}
39 changes: 17 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3513,6 +3513,14 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"

gray-matter@^3.0.6:
version "3.0.6"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-3.0.6.tgz#43480310bca9965009cbd844fa04de6756dd1ccf"
dependencies:
js-yaml "^3.8.1"
kind-of "^5.0.0"
strip-bom-string "^1.0.0"

growly@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
Expand Down Expand Up @@ -4809,6 +4817,10 @@ kind-of@^4.0.0:
dependencies:
is-buffer "^1.1.5"

kind-of@^5.0.0:
version "5.0.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-5.0.2.tgz#f57bec933d9a2209ffa96c5c08343607b7035fda"

known-css-properties@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/known-css-properties/-/known-css-properties-0.2.0.tgz#899c94be368e55b42d7db8d5be7d73a4a4a41454"
Expand Down Expand Up @@ -6754,23 +6766,6 @@ postcss@^6.0.1, postcss@^6.0.2:
source-map "^0.5.6"
supports-color "^4.2.1"

preliminaries-parser-toml@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/preliminaries-parser-toml/-/preliminaries-parser-toml-1.3.0.tgz#e62203c45d1c5bf5908b02fb8830336b59a0ebe6"
dependencies:
toml "^2.3.2"
toml-js "0.0.8"

preliminaries-parser-yaml@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/preliminaries-parser-yaml/-/preliminaries-parser-yaml-1.3.0.tgz#72b5c69c930e322b07ed78a6696d4bf61de73585"
dependencies:
js-yaml "^3.8.1"

preliminaries@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/preliminaries/-/preliminaries-1.3.0.tgz#da1602e704c36da5dde59b7f4aea2a5380ee6357"

prelude-ls@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
Expand Down Expand Up @@ -8257,6 +8252,10 @@ strip-ansi@^4.0.0:
dependencies:
ansi-regex "^3.0.0"

strip-bom-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/strip-bom-string/-/strip-bom-string-1.0.0.tgz#e5211e9224369fbb81d633a2f00044dc8cedad92"

strip-bom@3.0.0, strip-bom@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
Expand Down Expand Up @@ -8651,11 +8650,7 @@ to-fast-properties@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47"

toml-js@0.0.8:
version "0.0.8"
resolved "https://registry.yarnpkg.com/toml-js/-/toml-js-0.0.8.tgz#648ea6f1a4d63b19c0bb30b8ed03e40d09473b0a"

toml@^2.3.2:
toml@^2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/toml/-/toml-2.3.3.tgz#8d683d729577cb286231dfc7a8affe58d31728fb"

Expand Down