2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-04-13 11:18:57 -07:00
|
|
|
const { addErrorDetailIf, filterTokens, headingStyleFor } =
|
|
|
|
require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
2023-11-09 20:05:30 -08:00
|
|
|
"names": [ "MD003", "heading-style" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Heading style",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "markdownit",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD003(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
let style = String(params.config.style || "consistent");
|
2019-04-13 11:18:57 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
|
|
|
const styleForToken = headingStyleFor(token);
|
2018-01-21 21:44:25 -08:00
|
|
|
if (style === "consistent") {
|
|
|
|
style = styleForToken;
|
|
|
|
}
|
|
|
|
if (styleForToken !== style) {
|
2018-04-27 22:05:34 -07:00
|
|
|
const h12 = /h[12]/.test(token.tag);
|
|
|
|
const setextWithAtx =
|
2018-01-21 21:44:25 -08:00
|
|
|
(style === "setext_with_atx") &&
|
|
|
|
((h12 && (styleForToken === "setext")) ||
|
|
|
|
(!h12 && (styleForToken === "atx")));
|
2018-04-27 22:05:34 -07:00
|
|
|
const setextWithAtxClosed =
|
2018-01-21 21:44:25 -08:00
|
|
|
(style === "setext_with_atx_closed") &&
|
|
|
|
((h12 && (styleForToken === "setext")) ||
|
|
|
|
(!h12 && (styleForToken === "atx_closed")));
|
|
|
|
if (!setextWithAtx && !setextWithAtxClosed) {
|
2018-04-27 22:05:34 -07:00
|
|
|
let expected = style;
|
2018-01-21 21:44:25 -08:00
|
|
|
if (style === "setext_with_atx") {
|
|
|
|
expected = h12 ? "setext" : "atx";
|
|
|
|
} else if (style === "setext_with_atx_closed") {
|
|
|
|
expected = h12 ? "setext" : "atx_closed";
|
|
|
|
}
|
2019-04-13 11:18:57 -07:00
|
|
|
addErrorDetailIf(onError, token.lineNumber,
|
2018-01-21 21:44:25 -08:00
|
|
|
expected, styleForToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|