Allow '0'- and ' '-prefixed ordered list markers in MD005/MD029 (fixes #126).

This commit is contained in:
David Anson 2018-06-15 22:37:12 -07:00
parent 2710c375b3
commit 4a1e42d942
7 changed files with 190 additions and 8 deletions

View file

@ -190,6 +190,31 @@ for the list to fix it:
* Nested Item 3 * Nested Item 3
``` ```
Sequentially-ordered list markers are usually left-aligned such that all items
have the same starting column:
```markdown
...
8. Item
9. Item
10. Item
11. Item
...
```
This rule also supports right-alignment of list markers such that all items have
the same ending column:
```markdown
...
8. Item
9. Item
10. Item
11. Item
...
```
<a name="md006"></a> <a name="md006"></a>
## MD006 - Consider starting bulleted lists at the beginning of the line ## MD006 - Consider starting bulleted lists at the beginning of the line
@ -850,6 +875,17 @@ Example invalid list for all styles:
3. Done. 3. Done.
``` ```
This rule supports 0-prefixing ordered list items for uniform indentation:
```markdown
...
08. Item
09. Item
10. Item
11. Item
...
```
<a name="md030"></a> <a name="md030"></a>
## MD030 - Spaces after list markers ## MD030 - Spaces after list markers

View file

@ -10,10 +10,32 @@ module.exports = {
"tags": [ "bullet", "ul", "indentation" ], "tags": [ "bullet", "ul", "indentation" ],
"function": function MD005(params, onError) { "function": function MD005(params, onError) {
shared.flattenLists().forEach(function forList(list) { shared.flattenLists().forEach(function forList(list) {
const expectedIndent = list.indent;
let expectedEnd = 0;
let actualEnd = -1;
let endMatching = false;
list.items.forEach(function forItem(item) { list.items.forEach(function forItem(item) {
shared.addErrorDetailIf(onError, item.lineNumber, list.indent, const actualIndent = shared.indentFor(item);
shared.indentFor(item), null, if (list.unordered) {
shared.addErrorDetailIf(onError, item.lineNumber,
expectedIndent, actualIndent, null,
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe)); shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
} else {
const match = shared.orderedListItemMarkerRe.exec(item.line);
actualEnd = match && match[0].length;
expectedEnd = expectedEnd || actualEnd;
if ((expectedIndent !== actualIndent) || endMatching) {
if (expectedEnd === actualEnd) {
endMatching = true;
} else {
const detail = endMatching ?
`Expected: (${expectedEnd}); Actual: (${actualEnd})` :
`Expected: ${expectedIndent}; Actual: ${actualIndent}`;
shared.addError(onError, item.lineNumber, detail, null,
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
}
}
}
}); });
}); });
} }

View file

@ -4,8 +4,6 @@
const shared = require("./shared"); const shared = require("./shared");
const numberRe = /^[\s>]*([^.)]*)[.)]/;
module.exports = { module.exports = {
"names": [ "MD029", "ol-prefix" ], "names": [ "MD029", "ol-prefix" ],
"description": "Ordered list item prefix", "description": "Ordered list item prefix",
@ -17,12 +15,12 @@ module.exports = {
let listStyle = style; let listStyle = style;
if (listStyle === "one_or_ordered") { if (listStyle === "one_or_ordered") {
const second = (list.items.length > 1) && const second = (list.items.length > 1) &&
numberRe.exec(list.items[1].line); shared.orderedListItemMarkerRe.exec(list.items[1].line);
listStyle = (second && (second[1] !== "1")) ? "ordered" : "one"; listStyle = (second && (second[1] !== "1")) ? "ordered" : "one";
} }
let number = 1; let number = 1;
list.items.forEach(function forItem(item) { list.items.forEach(function forItem(item) {
const match = numberRe.exec(item.line); const match = shared.orderedListItemMarkerRe.exec(item.line);
shared.addErrorDetailIf(onError, item.lineNumber, shared.addErrorDetailIf(onError, item.lineNumber,
String(number), !match || match[1], String(number), !match || match[1],
"Style: " + (listStyle === "one" ? "1/1/1" : "1/2/3"), "Style: " + (listStyle === "one" ? "1/1/1" : "1/2/3"),

View file

@ -17,7 +17,8 @@ module.exports.inlineCommentRe = inlineCommentRe;
// Regular expressions for range matching // Regular expressions for range matching
module.exports.atxHeadingSpaceRe = /^#+\s*\S/; module.exports.atxHeadingSpaceRe = /^#+\s*\S/;
module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s]*/i; module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s]*/i;
module.exports.listItemMarkerRe = /^[\s>]*(?:[*+-]|\d+\.)\s+/; module.exports.listItemMarkerRe = /^[\s>]*(?:[*+-]|\d+[.)])\s+/;
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
// readFile options for reading with the UTF-8 encoding // readFile options for reading with the UTF-8 encoding
module.exports.utf8Encoding = { "encoding": "utf8" }; module.exports.utf8Encoding = { "encoding": "utf8" };

View file

@ -15,3 +15,16 @@
Text Text
Text text Text text
1. One
2. Two
3. Three
4. Four
5. Five
6. Six
7. Seven
8. Eight
9. Nine
10. Ten
11. Eleven
12. Twelve

View file

@ -39,6 +39,22 @@
"errorContext": null, "errorContext": null,
"errorRange": [1, 3] "errorRange": [1, 3]
}, },
{
"lineNumber": 23,
"ruleNames": [ "MD005", "list-indent" ],
"ruleDescription": "Inconsistent indentation for list items at the same level",
"errorDetail": "Expected: 1; Actual: 0",
"errorContext": null,
"errorRange": [1, 3]
},
{
"lineNumber": 29,
"ruleNames": [ "MD005", "list-indent" ],
"ruleDescription": "Inconsistent indentation for list items at the same level",
"errorDetail": "Expected: (3); Actual: (4)",
"errorContext": null,
"errorRange": [1, 5]
},
{ {
"lineNumber": 12, "lineNumber": 12,
"ruleNames": [ "MD006", "ul-start-left" ], "ruleNames": [ "MD006", "ul-start-left" ],

View file

@ -0,0 +1,96 @@
# List Alignment
## Leading Spaces
1. One
2. Two
3. Three
4. Four
5. Five
6. Six
7. Seven
8. Eight
9. Nine
10. Ten
11. Eleven
12. Twelve
## Leading Zeroes
01. One
02. Two
03. Three
04. Four
05. Five
06. Six
07. Seven
08. Eight
09. Nine
10. Ten
11. Eleven
12. Twelve
## Double Leading Zeroes
001. One
002. Two
003. Three
## Nested Lists
- Item
- Item
01. One
02. Two
03. Three
04. Four
05. Five
06. Six
07. Seven
08. Eight
09. Nine
10. Ten
11. Eleven
12. Twelve
- Item
- Item
1. One
2. Two
3. Three
4. Four
5. Five
6. Six
7. Seven
8. Eight
9. Nine
10. Ten
11. Eleven
12. Twelve
- Item
- Item
## Another Nested List
01. One
02. Two
01. One
02. Two
03. Three
04. Four
## Leading Spaces Errors
1. One
2. Two
3. Three
4. Four
5. Five {MD005}
6. Six
7. Seven
8. Eight
9. Nine
10. Ten
11. Eleven {MD005}
12. Twelve
13. Thirteen {MD005}
14. Fourteen