mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
// @ts-check
|
|
|
|
"use strict";
|
|
|
|
const { addErrorDetailIf, filterTokens, headingStyleFor } =
|
|
require("../helpers");
|
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
/** @type import("./markdownlint").Rule */
|
|
module.exports = {
|
|
"names": [ "MD003", "heading-style" ],
|
|
"description": "Heading style",
|
|
"tags": [ "headings" ],
|
|
"function": function MD003(params, onError) {
|
|
let style = String(params.config.style || "consistent");
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
|
const styleForToken = headingStyleFor(token);
|
|
if (style === "consistent") {
|
|
style = styleForToken;
|
|
}
|
|
if (styleForToken !== style) {
|
|
const h12 = /h[12]/.test(token.tag);
|
|
const setextWithAtx =
|
|
(style === "setext_with_atx") &&
|
|
((h12 && (styleForToken === "setext")) ||
|
|
(!h12 && (styleForToken === "atx")));
|
|
const setextWithAtxClosed =
|
|
(style === "setext_with_atx_closed") &&
|
|
((h12 && (styleForToken === "setext")) ||
|
|
(!h12 && (styleForToken === "atx_closed")));
|
|
if (!setextWithAtx && !setextWithAtxClosed) {
|
|
let expected = style;
|
|
if (style === "setext_with_atx") {
|
|
expected = h12 ? "setext" : "atx";
|
|
} else if (style === "setext_with_atx_closed") {
|
|
expected = h12 ? "setext" : "atx_closed";
|
|
}
|
|
addErrorDetailIf(onError, token.lineNumber,
|
|
expected, styleForToken);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
};
|