Skip to content

Commit

Permalink
Support CSS via PostCSS (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Apr 24, 2022
1 parent c9fdb4d commit f27cad4
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A remark-lint rule to check language syntax in a code block.
- JavaScript
- JSON
- YAML
- CSS

## Install

Expand Down
39 changes: 31 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,50 @@ import { lintRule } from "unified-lint-rule";
import { visit } from "unist-util-visit";
import { default as swc } from "@swc/core";
import { default as yaml } from "js-yaml";
import { default as postcss } from "postcss";

const remarkLintCodeBlockSyntax = lintRule("remark-lint:code-block-syntax", codeSyntax);
export default remarkLintCodeBlockSyntax;

function codeSyntax(tree, file) {
const supportedLangs = ["js", "json", "yaml"];
const supportedLangs = ["js", "json", "yaml", "css"];
const test = supportedLangs.map((lang) => ({ type: "code", lang: lang }));

visit(tree, test, visitor);

function visitor(node) {
switch (node.lang) {
const report = (reason, language) => {
file.message(`Invalid ${language}: ${reason}`, node);
};

const { lang, value } = node;

switch (lang) {
case "js": {
const reason = checkJs(node.value);
const reason = checkJs(value);
if (reason) {
file.message(`Invalid JavaScript: ${reason}`, node);
report(reason, "JavaScript");
}
break;
}
case "json": {
const reason = checkJson(node.value);
const reason = checkJson(value);
if (reason) {
file.message(`Invalid JSON: ${reason}`, node);
report(reason, "JSON");
}
break;
}
case "yaml": {
const reason = checkYaml(node.value);
const reason = checkYaml(value);
if (reason) {
file.message(`Invalid YAML: ${reason}`, node);
report(reason, "YAML");
}
break;
}
case "css": {
const reason = checkCss(value);
if (reason) {
report(reason, "CSS");
}
break;
}
Expand Down Expand Up @@ -70,4 +84,13 @@ function codeSyntax(tree, file) {
return e.message.split(/\r?\n/)[0];
}
}

function checkCss(code) {
try {
postcss.parse(code);
return null;
} catch (e) {
return e.message;
}
}
}
22 changes: 20 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,26 @@ describe("YAML", () => {
});
});

describe("CSS", () => {
test("valid", () => {
expect(run("css", "a{}")).toEqual([]);
});

test("invalid", () => {
expect(run("css", "a{\n}}")).toEqual([
{
column: 1,
line: 1,
message: "Invalid CSS: <css input>:2:2: Unexpected }",
ruleId: "code-block-syntax",
source: "remark-lint",
},
]);
});
});

describe("Unsupported languages", () => {
test("CSS", () => {
expect(run("css", "a{")).toEqual([]);
test("Ruby", () => {
expect(run("ruby", "a=")).toEqual([]);
});
});
69 changes: 65 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test:watch": "npm run test -- --watch",
"lint": "npm run eslint && npm run prettier:check",
"lint:fix": "npm run eslint:fix && npm run prettier:write",
"eslint": "npx eslint --ignore-path=.gitignore .",
Expand All @@ -35,6 +36,7 @@
"dependencies": {
"@swc/core": "^1.2.171",
"js-yaml": "^4.0.0",
"postcss": "^8.4.12",
"unified-lint-rule": "^2.0.0",
"unist-util-visit": "^4.0.0"
},
Expand Down

0 comments on commit f27cad4

Please sign in to comment.