Fix: Always preserve frontmatter, remove dependency (#4392)

* Fix: Always preserve frontmatter, remove dependency

* Move correct test to correct filename

* Add test for malformed frontmatter, finetune regex

* Use other malformed test case

* Update yarn.lock
master
Elias Meire 2018-04-28 13:53:30 +02:00 committed by Lucas Duailibe
parent 91c701886d
commit 5b7ebb18ba
9 changed files with 159 additions and 45 deletions

View File

@ -34,7 +34,6 @@
"get-stream": "3.0.0",
"globby": "6.1.0",
"graphql": "0.13.2",
"gray-matter": "3.1.1",
"html-tag-names": "1.1.2",
"ignore": "3.3.7",
"jest-docblock": "22.2.2",

View File

@ -1,7 +1,6 @@
"use strict";
const createError = require("../common/parser-create-error");
const grayMatter = require("gray-matter");
// utils
const utils = require("./utils");
@ -438,10 +437,14 @@ function parseNestedCSS(node) {
return node;
}
function parseWithParser(parser, text, frontMatter) {
function parseWithParser(parser, text) {
let result;
const frontMatterMatches = text.match(/^---(\n[\s\S]*)?\n---/);
const frontMatter = frontMatterMatches && frontMatterMatches[0];
const normalizedText = frontMatter ? text.substr(frontMatter.length) : text;
try {
result = parser.parse(text);
result = parser.parse(normalizedText);
} catch (e) {
if (typeof e.line !== "number") {
throw e;
@ -449,10 +452,12 @@ function parseWithParser(parser, text, frontMatter) {
throw createError("(postcss) " + e.name + " " + e.reason, { start: e });
}
if (Object.keys(frontMatter.data).length > 0) {
if (frontMatterMatches) {
const frontMatterContent = (frontMatterMatches[1] || "").trim();
const rightPad = frontMatterContent.length > 0 ? "\n" : "";
result.nodes.unshift({
type: "comment-yaml",
value: grayMatter.stringify("", frontMatter.data).replace(/\s$/, "")
value: `---\n${frontMatterContent}${rightPad}---\n`
});
}
@ -484,22 +489,15 @@ function parse(text, parsers, opts) {
opts.parser === "less" || opts.parser === "scss";
const isSCSS = utils.isSCSS(opts.parser, text);
const frontMatter = grayMatter(text);
const normalizedText = frontMatter.content;
try {
return parseWithParser(requireParser(isSCSS), normalizedText, frontMatter);
return parseWithParser(requireParser(isSCSS), text);
} catch (originalError) {
if (hasExplicitParserChoice) {
throw originalError;
}
try {
return parseWithParser(
requireParser(!isSCSS),
normalizedText,
frontMatter
);
return parseWithParser(requireParser(!isSCSS), text);
} catch (_secondError) {
throw originalError;
}

View File

@ -1,5 +1,103 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`empty.css 1`] = `
---
---
a {
color: red;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
---
a {
color: red;
}
`;
exports[`empty_newlines.css 1`] = `
---
---
a {
color: red;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
---
a {
color: red;
}
`;
exports[`malformed.css 1`] = `
---
aaa
b---
a {
color: red;
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- aaa b--- a {
color: red;
}
`;
exports[`only_comments.css 1`] = `
---
# comment 1
# comment 2
# comment 3
---
a {
color: red;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
# comment 1
# comment 2
# comment 3
---
a {
color: red;
}
`;
exports[`with_comments.css 1`] = `
---
title: Title
description: Description
# This is a comment
---
a {
color: red;
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
title: Title
description: Description
# This is a comment
---
a {
color: red;
}
`;
exports[`yaml.css 1`] = `
---
title: Title

6
tests/css_yaml/empty.css Normal file
View File

@ -0,0 +1,6 @@
---
---
a {
color: red;
}

View File

@ -0,0 +1,11 @@
---
---
a {
color: red;
}

View File

@ -0,0 +1,7 @@
---
aaa
b---
a {
color: red;
}

View File

@ -0,0 +1,9 @@
---
# comment 1
# comment 2
# comment 3
---
a {
color: red;
}

View File

@ -0,0 +1,9 @@
---
title: Title
description: Description
# This is a comment
---
a {
color: red;
}

View File

@ -1839,12 +1839,6 @@ 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"
@ -2161,15 +2155,6 @@ graphql@0.13.2:
dependencies:
iterall "^1.2.1"
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"
@ -2459,7 +2444,7 @@ is-equal-shallow@^0.1.3:
dependencies:
is-primitive "^2.0.0"
is-extendable@^0.1.0, is-extendable@^0.1.1:
is-extendable@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"
@ -2957,16 +2942,16 @@ 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, js-yaml@^3.9.1:
version "3.10.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc"
js-yaml@^3.7.0, js-yaml@^3.9.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.9.1.tgz#08775cebdfdd359209f0d2acd383c8f86a6904a0"
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"
js-yaml@^3.7.0, js-yaml@^3.9.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.9.1.tgz#08775cebdfdd359209f0d2acd383c8f86a6904a0"
js-yaml@^3.9.1:
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"
@ -3080,10 +3065,6 @@ 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"
@ -4549,10 +4530,6 @@ 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"