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 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 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 configured style). The less-common patterns of using '0.' as a first prefix or
also supported. for all prefixes is also supported.
Example valid list if the style is configured as 'one': 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. 1. Done.
``` ```
Example valid list if the style is configured as 'ordered': Examples of valid lists if the style is configured as 'ordered':
```markdown ```markdown
1. Do this. 1. Do this.
@ -964,7 +964,13 @@ Example valid list if the style is configured as 'ordered':
3. Done. 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': Example valid list if the style is configured as 'zero':

View file

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

View file

@ -60,3 +60,15 @@ text
2. Item {MD029} 2. Item {MD029}
4. Item {MD029} 4. Item {MD029}
- Item - 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 2. Item
4. Item {MD029} 4. Item {MD029}
- Item - 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 2. Item
4. Item {MD029} 4. Item {MD029}
- Item - 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} 1. Item {MD029}
2. Item {MD029} 2. Item {MD029}
- Item - Item
text
0. Item
1. Item {MD029}
2. Item {MD029}
text
0. Item
1. Item {MD029}
3. Item {MD029}