2019-04-17 14:42:17 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const { addErrorDetailIf } = require("../helpers");
|
2024-02-19 20:30:35 -08:00
|
|
|
const { filterByTypes } = require("../helpers/micromark.cjs");
|
2019-04-17 14:42:17 -07:00
|
|
|
|
|
|
|
|
const tokenTypeToStyle = {
|
2024-02-19 20:30:35 -08:00
|
|
|
"codeFenced": "fenced",
|
|
|
|
|
"codeIndented": "indented"
|
2019-04-17 14:42:17 -07:00
|
|
|
};
|
|
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2019-04-17 14:42:17 -07:00
|
|
|
module.exports = {
|
|
|
|
|
"names": [ "MD046", "code-block-style" ],
|
|
|
|
|
"description": "Code block style",
|
|
|
|
|
"tags": [ "code" ],
|
|
|
|
|
"function": function MD046(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
let expectedStyle = String(params.config.style || "consistent");
|
2024-02-28 21:01:23 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
|
/** @type import("../helpers/micromark.cjs").Token[] */
|
|
|
|
|
const micromarkTokens =
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
params.parsers.micromark.tokens;
|
|
|
|
|
const codeBlocksAndFences = filterByTypes(micromarkTokens, [ "codeFenced", "codeIndented" ]);
|
2022-06-08 22:10:27 -07:00
|
|
|
for (const token of codeBlocksAndFences) {
|
2024-02-19 20:30:35 -08:00
|
|
|
const { startLine, type } = token;
|
2022-06-08 22:10:27 -07:00
|
|
|
if (expectedStyle === "consistent") {
|
|
|
|
|
expectedStyle = tokenTypeToStyle[type];
|
|
|
|
|
}
|
|
|
|
|
addErrorDetailIf(
|
|
|
|
|
onError,
|
2024-02-19 20:30:35 -08:00
|
|
|
startLine,
|
2022-06-08 22:10:27 -07:00
|
|
|
expectedStyle,
|
|
|
|
|
tokenTypeToStyle[type]);
|
|
|
|
|
}
|
2019-04-17 14:42:17 -07:00
|
|
|
}
|
|
|
|
|
};
|