mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +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
|
|
@ -1678,8 +1678,9 @@ To allow optional headings as with the following structure:
|
||||||
### Notes (optional)
|
### Notes (optional)
|
||||||
```
|
```
|
||||||
|
|
||||||
Use the special value `"*"` meaning "one or more unspecified headings" and set
|
Use the special value `"*"` meaning "zero or more unspecified headings" or the
|
||||||
the `headings` parameter to:
|
special value `"+"` meaning "one or more unspecified headings" and set the
|
||||||
|
`headings` parameter to:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
[
|
[
|
||||||
|
|
|
||||||
33
lib/md043.js
33
lib/md043.js
|
|
@ -13,30 +13,41 @@ module.exports = {
|
||||||
const requiredHeadings = params.config.headings || params.config.headers;
|
const requiredHeadings = params.config.headings || params.config.headers;
|
||||||
if (Array.isArray(requiredHeadings)) {
|
if (Array.isArray(requiredHeadings)) {
|
||||||
const levels = {};
|
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);
|
levels["h" + level] = "######".substr(-level);
|
||||||
});
|
});
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let optional = false;
|
let matchAny = false;
|
||||||
let errorCount = 0;
|
let hasError = false;
|
||||||
forEachHeading(params, function forHeading(heading, content) {
|
let anyHeadings = false;
|
||||||
if (!errorCount) {
|
// 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 actual = levels[heading.tag] + " " + content;
|
||||||
const expected = requiredHeadings[i++] || "[None]";
|
let expected = getExpected();
|
||||||
if (expected === "*") {
|
if (expected === "*") {
|
||||||
optional = true;
|
matchAny = true;
|
||||||
|
expected = getExpected();
|
||||||
|
} else if (expected === "+") {
|
||||||
|
matchAny = true;
|
||||||
} else if (expected.toLowerCase() === actual.toLowerCase()) {
|
} else if (expected.toLowerCase() === actual.toLowerCase()) {
|
||||||
optional = false;
|
matchAny = false;
|
||||||
} else if (optional) {
|
} else if (matchAny) {
|
||||||
i--;
|
i--;
|
||||||
} else {
|
} else {
|
||||||
addErrorDetailIf(onError, heading.lineNumber,
|
addErrorDetailIf(onError, heading.lineNumber,
|
||||||
expected, actual);
|
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,
|
addErrorContext(onError, params.lines.length,
|
||||||
requiredHeadings[i]);
|
requiredHeadings[i]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
6
test/required-headings-all-optional-at-least-one.json
Normal file
6
test/required-headings-all-optional-at-least-one.json
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD043": {
|
||||||
|
"headings": [ "+" ]
|
||||||
|
}
|
||||||
|
}
|
||||||
11
test/required-headings-all-optional-at-least-one.md
Normal file
11
test/required-headings-all-optional-at-least-one.md
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
# One
|
||||||
|
|
||||||
|
## Two
|
||||||
|
|
||||||
|
### THREE
|
||||||
|
|
||||||
|
#### four
|
||||||
|
|
||||||
|
##### Five
|
||||||
|
|
||||||
|
###### SiX
|
||||||
9
test/required-headings-none-one-or-more.json
Normal file
9
test/required-headings-none-one-or-more.json
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD041": false,
|
||||||
|
"MD043": {
|
||||||
|
"headings": [
|
||||||
|
"+"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
5
test/required-headings-none-one-or-more.md
Normal file
5
test/required-headings-none-one-or-more.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
Text
|
||||||
|
|
||||||
|
Text
|
||||||
|
|
||||||
|
{MD043:6}
|
||||||
9
test/required-headings-none-zero-or-more.json
Normal file
9
test/required-headings-none-zero-or-more.json
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD041": false,
|
||||||
|
"MD043": {
|
||||||
|
"headings": [
|
||||||
|
"*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
5
test/required-headings-none-zero-or-more.md
Normal file
5
test/required-headings-none-zero-or-more.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
Text
|
||||||
|
|
||||||
|
Text
|
||||||
|
|
||||||
|
Text
|
||||||
14
test/required-headings-one-or-more.json
Normal file
14
test/required-headings-one-or-more.json
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD043": {
|
||||||
|
"headings": [
|
||||||
|
"# One",
|
||||||
|
"+",
|
||||||
|
"### Three",
|
||||||
|
"+",
|
||||||
|
"### Six",
|
||||||
|
"+",
|
||||||
|
"#### 7"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
15
test/required-headings-one-or-more.md
Normal file
15
test/required-headings-one-or-more.md
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
# One
|
||||||
|
|
||||||
|
## Two
|
||||||
|
|
||||||
|
### THREE
|
||||||
|
|
||||||
|
## four
|
||||||
|
|
||||||
|
## Five
|
||||||
|
|
||||||
|
### SiX
|
||||||
|
|
||||||
|
#### 7
|
||||||
|
|
||||||
|
{MD043:16}
|
||||||
14
test/required-headings-zero-or-more.json
Normal file
14
test/required-headings-zero-or-more.json
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD043": {
|
||||||
|
"headings": [
|
||||||
|
"# One",
|
||||||
|
"*",
|
||||||
|
"### Three",
|
||||||
|
"*",
|
||||||
|
"### Six",
|
||||||
|
"*",
|
||||||
|
"#### 7"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
13
test/required-headings-zero-or-more.md
Normal file
13
test/required-headings-zero-or-more.md
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
# One
|
||||||
|
|
||||||
|
## Two
|
||||||
|
|
||||||
|
### THREE
|
||||||
|
|
||||||
|
## four
|
||||||
|
|
||||||
|
## Five
|
||||||
|
|
||||||
|
### SiX
|
||||||
|
|
||||||
|
#### 7
|
||||||
Loading…
Add table
Add a link
Reference in a new issue