mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-21 16:30:12 +01:00
Update MD043/required-headings to treat "*" as "zero or more" and "+" as "one or more" (fixes #281).
This commit is contained in:
parent
c693a9a3d8
commit
e20502c494
12 changed files with 126 additions and 13 deletions
33
lib/md043.js
33
lib/md043.js
|
|
@ -13,30 +13,41 @@ module.exports = {
|
|||
const requiredHeadings = params.config.headings || params.config.headers;
|
||||
if (Array.isArray(requiredHeadings)) {
|
||||
const levels = {};
|
||||
[ 1, 2, 3, 4, 5, 6 ].forEach(function forLevel(level) {
|
||||
[ 1, 2, 3, 4, 5, 6 ].forEach((level) => {
|
||||
levels["h" + level] = "######".substr(-level);
|
||||
});
|
||||
let i = 0;
|
||||
let optional = false;
|
||||
let errorCount = 0;
|
||||
forEachHeading(params, function forHeading(heading, content) {
|
||||
if (!errorCount) {
|
||||
let matchAny = false;
|
||||
let hasError = false;
|
||||
let anyHeadings = false;
|
||||
// eslint-disable-next-line func-style
|
||||
const getExpected = () => requiredHeadings[i++] || "[None]";
|
||||
forEachHeading(params, (heading, content) => {
|
||||
if (!hasError) {
|
||||
anyHeadings = true;
|
||||
const actual = levels[heading.tag] + " " + content;
|
||||
const expected = requiredHeadings[i++] || "[None]";
|
||||
let expected = getExpected();
|
||||
if (expected === "*") {
|
||||
optional = true;
|
||||
matchAny = true;
|
||||
expected = getExpected();
|
||||
} else if (expected === "+") {
|
||||
matchAny = true;
|
||||
} else if (expected.toLowerCase() === actual.toLowerCase()) {
|
||||
optional = false;
|
||||
} else if (optional) {
|
||||
matchAny = false;
|
||||
} else if (matchAny) {
|
||||
i--;
|
||||
} else {
|
||||
addErrorDetailIf(onError, heading.lineNumber,
|
||||
expected, actual);
|
||||
errorCount++;
|
||||
hasError = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
if ((i < requiredHeadings.length) && !errorCount) {
|
||||
if (
|
||||
!hasError &&
|
||||
(i < requiredHeadings.length) &&
|
||||
(anyHeadings || !requiredHeadings.every((heading) => heading === "*"))
|
||||
) {
|
||||
addErrorContext(onError, params.lines.length,
|
||||
requiredHeadings[i]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue