2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorDetailIf } from "../helpers/helpers.cjs";
|
|
|
|
import { getHeadingLevel, getHeadingStyle } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
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-06-08 14:44:49 -07:00
|
|
|
"parser": "micromark",
|
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");
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
|
2024-06-08 14:44:49 -07:00
|
|
|
const styleForToken = getHeadingStyle(heading);
|
2018-01-21 21:44:25 -08:00
|
|
|
if (style === "consistent") {
|
|
|
|
style = styleForToken;
|
|
|
|
}
|
2024-06-09 17:09:03 -07:00
|
|
|
if (styleForToken !== style) {
|
2024-06-08 14:44:49 -07:00
|
|
|
const h12 = getHeadingLevel(heading) <= 2;
|
2018-04-27 22:05:34 -07:00
|
|
|
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";
|
|
|
|
}
|
2024-06-08 14:44:49 -07:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
heading.startLine,
|
|
|
|
expected,
|
|
|
|
styleForToken
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
}
|
2024-06-08 14:44:49 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|