2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-06-08 14:44:49 -07:00
|
|
|
const { addErrorDetailIf } = require("../helpers");
|
2024-06-09 17:09:03 -07:00
|
|
|
const { filterByTypes, getHeadingLevel, getHeadingStyle } =
|
2024-06-08 14:44:49 -07:00
|
|
|
require("../helpers/micromark.cjs");
|
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-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-06-08 14:44:49 -07:00
|
|
|
const headings = filterByTypes(
|
|
|
|
params.parsers.micromark.tokens,
|
|
|
|
[ "atxHeading", "setextHeading" ]
|
|
|
|
);
|
|
|
|
for (const heading of headings) {
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|