mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
// @ts-check
|
|
|
|
"use strict";
|
|
|
|
const { addErrorDetailIf, fencedCodeBlockStyleFor } = require("../helpers");
|
|
const { filterByTypes, tokenIfType } = require("../helpers/micromark.cjs");
|
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
/** @type import("./markdownlint").Rule */
|
|
module.exports = {
|
|
"names": [ "MD048", "code-fence-style" ],
|
|
"description": "Code fence style",
|
|
"tags": [ "code" ],
|
|
"parser": "micromark",
|
|
"function": function MD048(params, onError) {
|
|
const style = String(params.config.style || "consistent");
|
|
let expectedStyle = style;
|
|
const codeFenceds = filterByTypes(
|
|
params.parsers.micromark.tokens,
|
|
[ "codeFenced" ]
|
|
);
|
|
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)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|