From ab8226ce63fd707dcfb0b21d199cb936718435d2 Mon Sep 17 00:00:00 2001 From: Caleb Date: Tue, 12 Sep 2017 20:51:40 -0600 Subject: [PATCH 1/5] Infer front-matter type on our own. --- src/formats/frontmatter.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/formats/frontmatter.js b/src/formats/frontmatter.js index 7aaaf035..6006bc72 100644 --- a/src/formats/frontmatter.js +++ b/src/formats/frontmatter.js @@ -8,9 +8,21 @@ preliminaries(true); yamlParser(true); tomlParser(true); +function inferFrontmatterFormat(str) { + const firstLine = str.substr(0, str.indexOf('\n')).trim(); + switch (firstLine) { + case "---": + return { lang: "yaml", delims: "---" }; + case "+++": + return { lang: "toml", delims: "+++" }; + case "{": + return { lang: "json", delims: ["{", "}"] }; + } +} + export default class Frontmatter { fromFile(content) { - const result = preliminaries.parse(content); + const result = preliminaries.parse(content, inferFrontmatterFormat(content)); const data = result.data; data.body = result.content; return data; From ebf91a980035451d5d642924571e8d709d059103 Mon Sep 17 00:00:00 2001 From: Caleb Date: Wed, 13 Sep 2017 08:55:10 -0600 Subject: [PATCH 2/5] Switch back to `gray-matter` for front-matter parsing. --- package.json | 5 ++--- src/formats/frontmatter.js | 20 +++++++------------ yarn.lock | 39 +++++++++++++++++--------------------- 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index fa2d558d..04c4d2c7 100644 --- a/package.json +++ b/package.json @@ -106,6 +106,7 @@ "dateformat": "^1.0.12", "deep-equal": "^1.0.1", "fuzzy": "^0.1.1", + "gray-matter": "^3.0.3", "history": "^2.1.2", "immutability-helper": "^2.0.0", "immutable": "^3.7.6", @@ -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", @@ -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", diff --git a/src/formats/frontmatter.js b/src/formats/frontmatter.js index 6006bc72..0043cdd0 100644 --- a/src/formats/frontmatter.js +++ b/src/formats/frontmatter.js @@ -1,28 +1,22 @@ -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); - function inferFrontmatterFormat(str) { const firstLine = str.substr(0, str.indexOf('\n')).trim(); switch (firstLine) { case "---": - return { lang: "yaml", delims: "---" }; + return { language: "yaml", delimiters: "---" }; case "+++": - return { lang: "toml", delims: "+++" }; + return { language: "toml", delimiters: "+++", engines: { toml: tomlEng.parse.bind(tomlEng) } }; case "{": - return { lang: "json", delims: ["{", "}"] }; + return { language: "json", delimiters: ["{", "}"] }; } } export default class Frontmatter { fromFile(content) { - const result = preliminaries.parse(content, inferFrontmatterFormat(content)); + const result = matter(content, inferFrontmatterFormat(content)); const data = result.data; data.body = result.content; return data; @@ -45,6 +39,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 } }); } } diff --git a/yarn.lock b/yarn.lock index fe2aa47e..82daf1bf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-3.0.3.tgz#0451a7eacdcb4f13fcbe12b14725422bc86b1030" + 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" @@ -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" @@ -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" @@ -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" @@ -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" From 335dd15e6ddb02e1b0649e1aefc5b2d7a02c3a82 Mon Sep 17 00:00:00 2001 From: Caleb Date: Wed, 13 Sep 2017 19:46:45 -0600 Subject: [PATCH 3/5] Fix JSON parser for gray-matter. --- src/formats/frontmatter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/formats/frontmatter.js b/src/formats/frontmatter.js index 0043cdd0..b84528ff 100644 --- a/src/formats/frontmatter.js +++ b/src/formats/frontmatter.js @@ -10,7 +10,7 @@ function inferFrontmatterFormat(str) { case "+++": return { language: "toml", delimiters: "+++", engines: { toml: tomlEng.parse.bind(tomlEng) } }; case "{": - return { language: "json", delimiters: ["{", "}"] }; + return { language: "json", delimiters: ["{", "}"], engines: { json: ((input) => matter.engines.json.parse('{' + input + '}')) } }; } } From 9f40edaf52dddea5ee685c4f6023ef67d733a243 Mon Sep 17 00:00:00 2001 From: Caleb Date: Thu, 14 Sep 2017 09:43:12 -0600 Subject: [PATCH 4/5] Re-add ability to parse frontmatter like `---json`. --- src/formats/frontmatter.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/formats/frontmatter.js b/src/formats/frontmatter.js index b84528ff..8740fbd2 100644 --- a/src/formats/frontmatter.js +++ b/src/formats/frontmatter.js @@ -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; From 86c4987d84b159ccc21266f698e9437a2849ab0d Mon Sep 17 00:00:00 2001 From: Caleb Date: Fri, 15 Sep 2017 07:29:28 -0600 Subject: [PATCH 5/5] Upgrade `gray-matter` to v3.0.6. This upgrade allow `gray-matter` to work in browsers as well. --- package.json | 2 +- yarn.lock | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 04c4d2c7..56d1dbd8 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,7 @@ "dateformat": "^1.0.12", "deep-equal": "^1.0.1", "fuzzy": "^0.1.1", - "gray-matter": "^3.0.3", + "gray-matter": "^3.0.6", "history": "^2.1.2", "immutability-helper": "^2.0.0", "immutable": "^3.7.6", diff --git a/yarn.lock b/yarn.lock index 82daf1bf..52dd7200 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3513,9 +3513,9 @@ 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.3: - version "3.0.3" - resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-3.0.3.tgz#0451a7eacdcb4f13fcbe12b14725422bc86b1030" +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"