markdownlint/lib/md003.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

// @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" ],
"parser": "markdownit",
"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);
}
}
});
}
};