2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorContext, addErrorDetailIf } from "../helpers/helpers.cjs";
|
|
|
|
import { getHeadingLevel, getHeadingText } 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": [ "MD043", "required-headings" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Required heading structure",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings" ],
|
2024-06-22 14:37:32 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD043(params, onError) {
|
2023-11-09 20:05:30 -08:00
|
|
|
const requiredHeadings = params.config.headings;
|
2023-08-29 23:00:27 -07:00
|
|
|
if (!Array.isArray(requiredHeadings)) {
|
|
|
|
// Nothing to check; avoid doing any work
|
|
|
|
return;
|
|
|
|
}
|
2022-10-22 03:15:50 -04:00
|
|
|
const matchCase = params.config.match_case || false;
|
2023-08-29 23:00:27 -07:00
|
|
|
let i = 0;
|
|
|
|
let matchAny = false;
|
|
|
|
let hasError = false;
|
|
|
|
let anyHeadings = false;
|
|
|
|
const getExpected = () => requiredHeadings[i++] || "[None]";
|
|
|
|
const handleCase = (str) => (matchCase ? str : str.toLowerCase());
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
|
2023-08-29 23:00:27 -07:00
|
|
|
if (!hasError) {
|
2024-06-22 14:37:32 -07:00
|
|
|
const headingText = getHeadingText(heading);
|
|
|
|
const headingLevel = getHeadingLevel(heading);
|
2023-08-29 23:00:27 -07:00
|
|
|
anyHeadings = true;
|
2024-06-22 14:37:32 -07:00
|
|
|
const actual = `${"".padEnd(headingLevel, "#")} ${headingText}`;
|
2023-08-29 23:00:27 -07:00
|
|
|
const expected = getExpected();
|
|
|
|
if (expected === "*") {
|
|
|
|
const nextExpected = getExpected();
|
|
|
|
if (handleCase(nextExpected) !== handleCase(actual)) {
|
2020-11-23 20:47:28 -08:00
|
|
|
matchAny = true;
|
2018-01-21 21:44:25 -08:00
|
|
|
i--;
|
|
|
|
}
|
2023-08-29 23:00:27 -07:00
|
|
|
} else if (expected === "+") {
|
|
|
|
matchAny = true;
|
2025-03-10 21:57:28 -07:00
|
|
|
} else if (expected === "?") {
|
|
|
|
// Allow current, match next
|
2023-08-29 23:00:27 -07:00
|
|
|
} else if (handleCase(expected) === handleCase(actual)) {
|
|
|
|
matchAny = false;
|
|
|
|
} else if (matchAny) {
|
|
|
|
i--;
|
|
|
|
} else {
|
2024-06-22 14:37:32 -07:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
heading.startLine,
|
|
|
|
expected,
|
|
|
|
actual
|
|
|
|
);
|
2023-08-29 23:00:27 -07:00
|
|
|
hasError = true;
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
}
|
2024-06-22 14:37:32 -07:00
|
|
|
}
|
2023-08-29 23:00:27 -07:00
|
|
|
const extraHeadings = requiredHeadings.length - i;
|
|
|
|
if (
|
|
|
|
!hasError &&
|
|
|
|
((extraHeadings > 1) ||
|
|
|
|
((extraHeadings === 1) && (requiredHeadings[i] !== "*"))) &&
|
|
|
|
(anyHeadings || !requiredHeadings.every((heading) => heading === "*"))
|
|
|
|
) {
|
2024-06-22 14:37:32 -07:00
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
params.lines.length,
|
|
|
|
requiredHeadings[i]
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|