fix: handle YAML front matter block (#3802)

master
Evilebot Tnawi 2018-01-24 19:44:09 +03:00 committed by GitHub
parent 5008836dc2
commit a3bf378259
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 147 additions and 5 deletions

View File

@ -34,6 +34,7 @@
"get-stream": "3.0.0",
"globby": "6.1.0",
"graphql": "0.12.3",
"gray-matter": "3.1.1",
"ignore": "3.3.7",
"jest-docblock": "21.3.0-beta.11",
"leven": "2.1.0",

View File

@ -1,6 +1,7 @@
"use strict";
const createError = require("../common/parser-create-error");
const grayMatter = require("gray-matter");
function parseSelector(selector) {
// If there's a comment inside of a selector, the parser tries to parse
@ -189,7 +190,11 @@ function parseNestedCSS(node) {
};
}
}
if (node.type && typeof node.value === "string") {
if (
node.type &&
node.type !== "css-comment-yaml" &&
typeof node.value === "string"
) {
try {
if (node.value.endsWith(DEFAULT_SCSS_DIRECTIVE)) {
node.default = true;
@ -226,7 +231,7 @@ function parseNestedCSS(node) {
return node;
}
function parseWithParser(parser, text) {
function parseWithParser(parser, text, frontMatter) {
let result;
try {
result = parser.parse(text);
@ -236,6 +241,14 @@ function parseWithParser(parser, text) {
}
throw createError("(postcss) " + e.name + " " + e.reason, { start: e });
}
if (Object.keys(frontMatter.data).length > 0) {
result.nodes.unshift({
type: "comment-yaml",
value: grayMatter.stringify("", frontMatter.data).replace(/\s$/, "")
});
}
const prefixedResult = addTypePrefix(result, "css-");
const parsedResult = parseNestedCSS(prefixedResult);
return parsedResult;
@ -269,15 +282,22 @@ function parse(text, parsers, opts) {
? opts.parser === "scss"
: IS_POSSIBLY_SCSS.test(text);
const frontMatter = grayMatter(text);
const normalizedText = frontMatter.content;
try {
return parseWithParser(requireParser(isSCSS), text);
return parseWithParser(requireParser(isSCSS), normalizedText, frontMatter);
} catch (originalError) {
if (hasExplicitParserChoice) {
throw originalError;
}
try {
return parseWithParser(requireParser(!isSCSS), text);
return parseWithParser(
requireParser(!isSCSS),
normalizedText,
frontMatter
);
} catch (_secondError) {
throw originalError;
}

View File

@ -29,6 +29,8 @@ function genericPrint(path, options, print) {
}
switch (n.type) {
case "css-comment-yaml":
return n.value;
case "css-root": {
const nodes = printNodeSequence(path, options, print);

View File

@ -0,0 +1,64 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`yaml.css 1`] = `
---
title: Title
description: Description
---
a {
color: red;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
title: Title
description: Description
---
a {
color: red;
}
`;
exports[`yaml.less 1`] = `
---
title: Title
description: Description
---
a {
color: red;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
title: Title
description: Description
---
a {
color: red;
}
`;
exports[`yaml.scss 1`] = `
---
title: Title
description: Description
---
a {
color: red;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
title: Title
description: Description
---
a {
color: red;
}
`;

View File

@ -0,0 +1 @@
run_spec(__dirname, ["css"]);

8
tests/css_yaml/yaml.css Normal file
View File

@ -0,0 +1,8 @@
---
title: Title
description: Description
---
a {
color: red;
}

8
tests/css_yaml/yaml.less Normal file
View File

@ -0,0 +1,8 @@
---
title: Title
description: Description
---
a {
color: red;
}

8
tests/css_yaml/yaml.scss Normal file
View File

@ -0,0 +1,8 @@
---
title: Title
description: Description
---
a {
color: red;
}

View File

@ -1731,6 +1731,12 @@ expect@^21.2.1:
jest-message-util "^21.2.1"
jest-regex-util "^21.2.0"
extend-shallow@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f"
dependencies:
is-extendable "^0.1.0"
extend@^3.0.0, extend@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444"
@ -2011,6 +2017,15 @@ graphql@0.12.3:
dependencies:
iterall "1.1.3"
gray-matter@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-3.1.1.tgz#101f80d9e69eeca6765cdce437705b18f40876ac"
dependencies:
extend-shallow "^2.0.1"
js-yaml "^3.10.0"
kind-of "^5.0.2"
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"
@ -2280,7 +2295,7 @@ is-equal-shallow@^0.1.3:
dependencies:
is-primitive "^2.0.0"
is-extendable@^0.1.1:
is-extendable@^0.1.0, is-extendable@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"
@ -2765,6 +2780,13 @@ js-tokens@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
js-yaml@^3.10.0:
version "3.10.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc"
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"
js-yaml@^3.7.0, js-yaml@^3.8.4, js-yaml@^3.9.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.9.1.tgz#08775cebdfdd359209f0d2acd383c8f86a6904a0"
@ -2871,6 +2893,10 @@ kind-of@^4.0.0:
dependencies:
is-buffer "^1.1.5"
kind-of@^5.0.2:
version "5.1.0"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-5.1.0.tgz#729c91e2d857b7a419a1f9aa65685c4c33f5845d"
lazy-cache@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e"
@ -4325,6 +4351,10 @@ strip-ansi@^3.0.0, strip-ansi@^3.0.1:
dependencies:
ansi-regex "^2.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"