mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Convert markdownlint library to an ECMAScript module, replace markdownlint-micromark with micromark, stop publishing (large) markdownlint-browser.js, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c for guidance.
This commit is contained in:
parent
191226f070
commit
1e71f6f44e
140 changed files with 1087 additions and 10428 deletions
70
lib/md043.mjs
Normal file
70
lib/md043.mjs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// @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.mjs").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 = () => requiredHeadings[i++] || "[None]";
|
||||
const handleCase = (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 (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]
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue