remark-lint-code-block-syntax
Advanced tools
Comparing version 0.4.0 to 0.5.0
45
index.js
@@ -5,2 +5,3 @@ import { lintRule } from "unified-lint-rule"; | ||
import { default as yaml } from "js-yaml"; | ||
import { default as postcss } from "postcss"; | ||
@@ -11,3 +12,3 @@ const remarkLintCodeBlockSyntax = lintRule("remark-lint:code-block-syntax", codeSyntax); | ||
function codeSyntax(tree, file) { | ||
const supportedLangs = ["js", "json", "yaml"]; | ||
const supportedLangs = ["js", "javascript", "json", "yaml", "yml", "css"]; | ||
const test = supportedLangs.map((lang) => ({ type: "code", lang: lang })); | ||
@@ -18,7 +19,14 @@ | ||
function visitor(node) { | ||
switch (node.lang) { | ||
case "js": { | ||
const reason = checkJs(node.value); | ||
const report = (reason, language) => { | ||
file.message(`Invalid ${language}: ${reason}`, node); | ||
}; | ||
const { lang, value } = node; | ||
switch (lang) { | ||
case "js": | ||
case "javascript": { | ||
const reason = checkJs(value); | ||
if (reason) { | ||
file.message(`Invalid JavaScript: ${reason}`, node); | ||
report(reason, "JavaScript"); | ||
} | ||
@@ -28,15 +36,23 @@ 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); | ||
case "yaml": | ||
case "yml": { | ||
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; | ||
} | ||
default: | ||
@@ -77,2 +93,11 @@ // ignore | ||
} | ||
function checkCss(code) { | ||
try { | ||
postcss.parse(code); | ||
return null; | ||
} catch (e) { | ||
return e.message; | ||
} | ||
} | ||
} |
{ | ||
"name": "remark-lint-code-block-syntax", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A remark-lint rule to check language syntax in a code block.", | ||
@@ -26,2 +26,3 @@ "author": "Masafumi Koba", | ||
"test": "NODE_OPTIONS=--experimental-vm-modules jest", | ||
"test:watch": "npm run test -- --watch", | ||
"lint": "npm run eslint && npm run prettier:check", | ||
@@ -39,2 +40,3 @@ "lint:fix": "npm run eslint:fix && npm run prettier:write", | ||
"js-yaml": "^4.0.0", | ||
"postcss": "^8.4.12", | ||
"unified-lint-rule": "^2.0.0", | ||
@@ -41,0 +43,0 @@ "unist-util-visit": "^4.0.0" |
@@ -5,3 +5,3 @@ [![npm](https://img.shields.io/npm/v/remark-lint-code-block-syntax?style=flat-square)](https://www.npmjs.com/package/remark-lint-code-block-syntax) | ||
A remark-lint rule to check language syntax in a code block. | ||
A [remark-lint](https://github.com/remarkjs/remark-lint) rule to check language syntax in a code block. | ||
@@ -13,2 +13,3 @@ ## Supported languages | ||
- YAML | ||
- CSS | ||
@@ -38,1 +39,21 @@ ## Install | ||
``` | ||
Via JavaScript API: | ||
```js | ||
import { reporter } from "vfile-reporter"; | ||
import { remark } from "remark"; | ||
import remarkLintCodeBlockSyntax from "remark-lint-code-block-syntax"; | ||
main(); | ||
async function main() { | ||
const code = ` | ||
\`\`\`js | ||
const sum = 1 +; | ||
\`\`\` | ||
`; | ||
const file = await remark().use(remarkLintCodeBlockSyntax).process(code); | ||
console.error(reporter(file)); | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5920
88
57
5
+ Addedpostcss@^8.4.12
+ Addednanoid@3.3.8(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedpostcss@8.5.1(transitive)
+ Addedsource-map-js@1.2.1(transitive)