2022-01-11 23:08:53 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-01-21 20:14:18 -08:00
|
|
|
const { parse, printParseErrorCode } = require("jsonc-parser");
|
2022-01-11 23:08:53 -08:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2022-01-11 23:08:53 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "validate-json" ],
|
|
|
|
"description": "Rule that validates JSON code",
|
|
|
|
"tags": [ "test", "validate", "json" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "markdownit",
|
2022-01-11 23:08:53 -08:00
|
|
|
"asynchronous": true,
|
2024-01-21 20:14:18 -08:00
|
|
|
"function": (params, onError) => {
|
2024-09-01 16:16:05 -07:00
|
|
|
const fences = params.parsers.markdownit.tokens
|
2024-10-06 21:03:07 -07:00
|
|
|
.filter((token) => token.type === "fence");
|
2024-08-18 15:34:26 -07:00
|
|
|
for (const fence of fences) {
|
2022-01-11 23:08:53 -08:00
|
|
|
if (/jsonc?/i.test(fence.info)) {
|
2024-01-21 20:14:18 -08:00
|
|
|
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(", ");
|
2022-01-11 23:08:53 -08:00
|
|
|
onError({
|
2024-01-21 20:14:18 -08:00
|
|
|
// @ts-ignore
|
2022-01-11 23:08:53 -08:00
|
|
|
"lineNumber": fence.lineNumber,
|
2024-01-21 20:14:18 -08:00
|
|
|
detail
|
2022-01-11 23:08:53 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-08-18 15:34:26 -07:00
|
|
|
}
|
2022-01-11 23:08:53 -08:00
|
|
|
}
|
|
|
|
};
|