Update MD043/required-headings to treat "*" as "zero or more" and "+" as "one or more" (fixes #281).

This commit is contained in:
David Anson 2020-11-23 20:47:28 -08:00
parent c693a9a3d8
commit e20502c494
12 changed files with 126 additions and 13 deletions

View file

@ -1678,8 +1678,9 @@ To allow optional headings as with the following structure:
### Notes (optional)
```
Use the special value `"*"` meaning "one or more unspecified headings" and set
the `headings` parameter to:
Use the special value `"*"` meaning "zero or more unspecified headings" or the
special value `"+"` meaning "one or more unspecified headings" and set the
`headings` parameter to:
```json
[

View file

@ -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]);
}

View file

@ -0,0 +1,6 @@
{
"default": true,
"MD043": {
"headings": [ "+" ]
}
}

View file

@ -0,0 +1,11 @@
# One
## Two
### THREE
#### four
##### Five
###### SiX

View file

@ -0,0 +1,9 @@
{
"default": true,
"MD041": false,
"MD043": {
"headings": [
"+"
]
}
}

View file

@ -0,0 +1,5 @@
Text
Text
{MD043:6}

View file

@ -0,0 +1,9 @@
{
"default": true,
"MD041": false,
"MD043": {
"headings": [
"*"
]
}
}

View file

@ -0,0 +1,5 @@
Text
Text
Text

View file

@ -0,0 +1,14 @@
{
"default": true,
"MD043": {
"headings": [
"# One",
"+",
"### Three",
"+",
"### Six",
"+",
"#### 7"
]
}
}

View file

@ -0,0 +1,15 @@
# One
## Two
### THREE
## four
## Five
### SiX
#### 7
{MD043:16}

View file

@ -0,0 +1,14 @@
{
"default": true,
"MD043": {
"headings": [
"# One",
"*",
"### Three",
"*",
"### Six",
"*",
"#### 7"
]
}
}

View file

@ -0,0 +1,13 @@
# One
## Two
### THREE
## four
## Five
### SiX
#### 7