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 } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
/** @type {import("./markdownlint.mjs").Rule} */
|
|
|
|
export default {
|
2023-11-09 20:05:30 -08:00
|
|
|
"names": [ "MD001", "heading-increment" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Heading levels should only increment by one level at a time",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings" ],
|
2024-06-06 21:19:14 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD001(params, onError) {
|
2024-06-06 21:19:14 -07:00
|
|
|
let prevLevel = Number.MAX_SAFE_INTEGER;
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
|
2024-06-06 21:19:14 -07:00
|
|
|
const level = getHeadingLevel(heading);
|
|
|
|
if (level > prevLevel) {
|
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
heading.startLine,
|
|
|
|
`h${prevLevel + 1}`,
|
|
|
|
`h${level}`
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
prevLevel = level;
|
2024-06-06 21:19:14 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|