Update MD029/ol-prefix to treat 0/1/2 as "ordered" (fixes #250).

This commit is contained in:
David Anson 2020-03-18 21:50:10 -07:00
parent 2e6f024565
commit 742f2a8d79
6 changed files with 97 additions and 22 deletions

View file

@ -945,8 +945,8 @@ Parameters: style ("one", "ordered", "one_or_ordered", "zero"; default "one_or_o
This rule is triggered for ordered lists that do not either start with '1.' or
do not have a prefix that increases in numerical order (depending on the
configured style). The less-common pattern of using '0.' for all prefixes is
also supported.
configured style). The less-common patterns of using '0.' as a first prefix or
for all prefixes is also supported.
Example valid list if the style is configured as 'one':
@ -956,7 +956,7 @@ Example valid list if the style is configured as 'one':
1. Done.
```
Example valid list if the style is configured as 'ordered':
Examples of valid lists if the style is configured as 'ordered':
```markdown
1. Do this.
@ -964,7 +964,13 @@ Example valid list if the style is configured as 'ordered':
3. Done.
```
Both examples are valid when the style is configured as 'one_or_ordered'.
```markdown
0. Do this.
1. Do that.
2. Done.
```
All three examples are valid when the style is configured as 'one_or_ordered'.
Example valid list if the style is configured as 'zero':

View file

@ -18,26 +18,47 @@ module.exports = {
"tags": [ "ol" ],
"function": function MD029(params, onError) {
const style = String(params.config.style || "one_or_ordered");
flattenedLists().forEach((list) => {
if (!list.unordered) {
flattenedLists().filter((list) => !list.unordered).forEach((list) => {
const { items } = list;
let current = 1;
let incrementing = false;
// Check for incrementing number pattern 1/2/3 or 0/1/2
if (items.length >= 2) {
const first = orderedListItemMarkerRe.exec(items[0].line);
const second = orderedListItemMarkerRe.exec(items[1].line);
if (first && second) {
const [ , firstNumber ] = first;
const [ , secondNumber ] = second;
if ((secondNumber !== "1") || (firstNumber === "0")) {
incrementing = true;
if (firstNumber === "0") {
current = 0;
}
}
}
}
// Determine effective style
let listStyle = style;
if (listStyle === "one_or_ordered") {
const second = (list.items.length > 1) &&
orderedListItemMarkerRe.exec(list.items[1].line);
listStyle = (second && (second[1] !== "1")) ? "ordered" : "one";
listStyle = incrementing ? "ordered" : "one";
}
let number = (listStyle === "zero") ? 0 : 1;
list.items.forEach((item) => {
// Force expected value for 0/0/0 and 1/1/1 patterns
if (listStyle === "zero") {
current = 0;
} else if (listStyle === "one") {
current = 1;
}
// Validate each list item marker
items.forEach((item) => {
const match = orderedListItemMarkerRe.exec(item.line);
addErrorDetailIf(onError, item.lineNumber,
String(number), !match || match[1],
String(current), !match || match[1],
"Style: " + listStyleExamples[listStyle], null,
rangeFromRegExp(item.line, listItemMarkerRe));
if (listStyle === "ordered") {
number++;
current++;
}
});
}
});
}
};

View file

@ -60,3 +60,15 @@ text
2. Item {MD029}
4. Item {MD029}
- Item
text
0. Item {MD029}
1. Item
2. Item {MD029}
text
0. Item {MD029}
1. Item
3. Item {MD029}

View file

@ -60,3 +60,15 @@ text
2. Item
4. Item {MD029}
- Item
text
0. Item
1. Item
2. Item
text
0. Item
1. Item
3. Item {MD029}

View file

@ -60,3 +60,15 @@ text
2. Item
4. Item {MD029}
- Item
text
0. Item
1. Item
2. Item
text
0. Item
1. Item
3. Item {MD029}

View file

@ -60,3 +60,15 @@ text
1. Item {MD029}
2. Item {MD029}
- Item
text
0. Item
1. Item {MD029}
2. Item {MD029}
text
0. Item
1. Item {MD029}
3. Item {MD029}