2019-10-08 21:10:02 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2020-01-23 19:42:46 -08:00
|
|
|
const { addErrorDetailIf, fencedCodeBlockStyleFor } = require("../helpers");
|
2024-02-19 20:42:12 -08:00
|
|
|
const { filterByTypes, tokenIfType } = require("../helpers/micromark.cjs");
|
2019-10-08 21:10:02 -07:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2019-10-08 21:10:02 -07:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD048", "code-fence-style" ],
|
|
|
|
"description": "Code fence style",
|
|
|
|
"tags": [ "code" ],
|
|
|
|
"function": function MD048(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
const style = 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;
|
2019-10-08 21:10:02 -07:00
|
|
|
let expectedStyle = style;
|
2024-02-28 21:01:23 -08:00
|
|
|
const codeFenceds = filterByTypes(micromarkTokens, [ "codeFenced" ]);
|
2024-02-19 20:42:12 -08:00
|
|
|
for (const codeFenced of codeFenceds) {
|
|
|
|
const codeFencedFence = tokenIfType(codeFenced.children[0], "codeFencedFence");
|
|
|
|
if (codeFencedFence) {
|
|
|
|
const codeFencedFenceSequence = tokenIfType(codeFencedFence.children[0], "codeFencedFenceSequence");
|
|
|
|
if (codeFencedFenceSequence) {
|
|
|
|
const { startLine, text } = codeFencedFenceSequence;
|
|
|
|
if (expectedStyle === "consistent") {
|
|
|
|
expectedStyle = fencedCodeBlockStyleFor(text);
|
|
|
|
}
|
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
startLine,
|
|
|
|
expectedStyle,
|
|
|
|
fencedCodeBlockStyleFor(text)
|
|
|
|
);
|
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
|
|
|
}
|
2019-10-08 21:10:02 -07:00
|
|
|
}
|
|
|
|
};
|