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

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