markdownlint/lib/md043.mjs
David Anson 7beb9fc9d0
Some checks are pending
Checkers / linkcheck (push) Waiting to run
Checkers / spellcheck (push) Waiting to run
CI / build (20, macos-latest) (push) Waiting to run
CI / build (20, ubuntu-latest) (push) Waiting to run
CI / build (20, windows-latest) (push) Waiting to run
CI / build (22, macos-latest) (push) Waiting to run
CI / build (22, ubuntu-latest) (push) Waiting to run
CI / build (22, windows-latest) (push) Waiting to run
CI / build (24, macos-latest) (push) Waiting to run
CI / build (24, ubuntu-latest) (push) Waiting to run
CI / build (24, windows-latest) (push) Waiting to run
CI / pnpm (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
TestRepos / build (latest, ubuntu-latest) (push) Waiting to run
UpdateTestRepos / update (push) Waiting to run
Address new TypeScript warnings in core files, improve type definitions.
2025-10-11 16:48:18 -07:00

72 lines
2.3 KiB
JavaScript

// @ts-check
import { addErrorContext, addErrorDetailIf } from "../helpers/helpers.cjs";
import { getHeadingLevel, getHeadingText } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD043", "required-headings" ],
"description": "Required heading structure",
"tags": [ "headings" ],
"parser": "micromark",
"function": function MD043(params, onError) {
const requiredHeadings = params.config.headings;
if (!Array.isArray(requiredHeadings)) {
// Nothing to check; avoid doing any work
return;
}
const matchCase = params.config.match_case || false;
let i = 0;
let matchAny = false;
let hasError = false;
let anyHeadings = false;
const getExpected = () => String(requiredHeadings[i++] || "[None]");
const handleCase = (/** @type {string} */ str) => (matchCase ? str : str.toLowerCase());
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
if (!hasError) {
const headingText = getHeadingText(heading);
const headingLevel = getHeadingLevel(heading);
anyHeadings = true;
const actual = `${"".padEnd(headingLevel, "#")} ${headingText}`;
const expected = getExpected();
if (expected === "*") {
const nextExpected = getExpected();
if (handleCase(nextExpected) !== handleCase(actual)) {
matchAny = true;
i--;
}
} else if (expected === "+") {
matchAny = true;
} else if (expected === "?") {
// Allow current, match next
} else if (handleCase(expected) === handleCase(actual)) {
matchAny = false;
} else if (matchAny) {
i--;
} else {
addErrorDetailIf(
onError,
heading.startLine,
expected,
actual
);
hasError = true;
}
}
}
const extraHeadings = requiredHeadings.length - i;
if (
!hasError &&
((extraHeadings > 1) ||
((extraHeadings === 1) && (requiredHeadings[i] !== "*"))) &&
(anyHeadings || !requiredHeadings.every((heading) => heading === "*"))
) {
addErrorContext(
onError,
params.lines.length,
requiredHeadings[i]
);
}
}
};