-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
Copy pathvalidate-json.cjs
34 lines (31 loc) · 958 Bytes
/
validate-json.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// @ts-check
"use strict";
const { parse, printParseErrorCode } = require("jsonc-parser");
/** @type {import("markdownlint").Rule} */
module.exports = {
"names": [ "validate-json" ],
"description": "Rule that validates JSON code",
"tags": [ "test", "validate", "json" ],
"parser": "markdownit",
"asynchronous": true,
"function": (params, onError) => {
const fences = params.parsers.markdownit.tokens
.filter((token) => token.type === "fence");
for (const fence of fences) {
if (/jsonc?/i.test(fence.info)) {
const errors = [];
parse(fence.content, errors);
if (errors.length > 0) {
const detail = errors.map(
(err) => `${printParseErrorCode(err.error)} (offset ${err.offset}, length ${err.length})`
).join(", ");
onError({
// @ts-ignore
"lineNumber": fence.lineNumber,
detail
});
}
}
}
}
};