diff --git a/doc/Rules.md b/doc/Rules.md index 36f075cc..a72d73d8 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -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': diff --git a/lib/md029.js b/lib/md029.js index d546f0cd..b5c5d4b1 100644 --- a/lib/md029.js +++ b/lib/md029.js @@ -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) { - 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"; - } - let number = (listStyle === "zero") ? 0 : 1; - list.items.forEach((item) => { - const match = orderedListItemMarkerRe.exec(item.line); - addErrorDetailIf(onError, item.lineNumber, - String(number), !match || match[1], - "Style: " + listStyleExamples[listStyle], null, - rangeFromRegExp(item.line, listItemMarkerRe)); - if (listStyle === "ordered") { - number++; + 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") { + 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++; + } + }); }); } }; diff --git a/test/ordered-list-item-prefix-one.md b/test/ordered-list-item-prefix-one.md index 05f9d520..83c28912 100644 --- a/test/ordered-list-item-prefix-one.md +++ b/test/ordered-list-item-prefix-one.md @@ -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} diff --git a/test/ordered-list-item-prefix-one_or_ordered.md b/test/ordered-list-item-prefix-one_or_ordered.md index 9d926e1c..c4264452 100644 --- a/test/ordered-list-item-prefix-one_or_ordered.md +++ b/test/ordered-list-item-prefix-one_or_ordered.md @@ -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} diff --git a/test/ordered-list-item-prefix-ordered.md b/test/ordered-list-item-prefix-ordered.md index c4c003b1..7306373f 100644 --- a/test/ordered-list-item-prefix-ordered.md +++ b/test/ordered-list-item-prefix-ordered.md @@ -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} diff --git a/test/ordered-list-item-prefix-zero.md b/test/ordered-list-item-prefix-zero.md index ea321753..d424287b 100644 --- a/test/ordered-list-item-prefix-zero.md +++ b/test/ordered-list-item-prefix-zero.md @@ -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}