Add "sublist" style to MD004 (fixes #21).

This commit is contained in:
David Anson 2016-07-04 14:32:40 -07:00
parent c8ecec1953
commit 7a1773ea77
4 changed files with 91 additions and 7 deletions

View file

@ -98,7 +98,8 @@ Tags: bullet, ul
Aliases: ul-style
Parameters: style ("consistent", "asterisk", "plus", "dash"; default "consistent")
Parameters: style ("consistent", "asterisk", "plus", "dash", "sublist"; default
"consistent")
This rule is triggered when the symbols used in the document for unordered
list items do not match the configured unordered list style:
@ -114,9 +115,19 @@ document:
* Item 2
* Item 3
Note: the configured list style can be a specific symbol to use (asterisk,
plus, dash), or simply require that the usage be consistent within the
document.
The configured list style can be a specific symbol to use (asterisk, plus, dash),
can require that usage be consistent within the document, or can require that each
sublist have a consistent symbol that is different from its parent list.
For example, the following is valid for the `sublist` style because the outer-most
indent uses asterisk, the middle indent uses plus, and the inner-most indent uses dash:
* Item 1
+ Item 2
- Item 3
+ Item 4
* Item 4
+ Item 5
## MD005 - Inconsistent indentation for list items at the same level

View file

@ -224,13 +224,24 @@ module.exports = [
"aliases": [ "ul-style" ],
"func": function MD004(params, errors) {
var style = params.options.style || "consistent";
var expectedStyle = style;
var nestingStyles = [];
flattenLists(params).forEach(function forList(list) {
if (list.unordered) {
if (style === "consistent") {
style = unorderedListStyleFor(list.items[0]);
if (expectedStyle === "consistent") {
expectedStyle = unorderedListStyleFor(list.items[0]);
}
list.items.forEach(function forItem(item) {
if (unorderedListStyleFor(item) !== style) {
var itemStyle = unorderedListStyleFor(item);
if (style === "sublist") {
var nesting = list.nesting;
if (!nestingStyles[nesting] &&
(itemStyle !== nestingStyles[nesting - 1])) {
nestingStyles[nesting] = itemStyle;
} else if (itemStyle !== nestingStyles[nesting]) {
errors.push(item.lineNumber);
}
} else if (itemStyle !== expectedStyle) {
errors.push(item.lineNumber);
}
});

View file

@ -0,0 +1,6 @@
{
"default": true,
"MD004": {
"style": "sublist"
}
}

View file

@ -0,0 +1,56 @@
1. item
1. item
+ item
1. item
- item {MD004}
- item
* item
+ item
- item
+ item
* item
- item
* item
+ item
- item
+ item {MD004}
+ item {MD004}
+ item
+ item {MD004}
- item
* item
* item {MD004}
- item
* item
+ item {MD004}
- item {MD004}
* item {MD004}
- item
1. item
+ item
1. item
* item
1. item
* item
- item
* item
+ item
- item
* item
* item {MD004}
+ item
- item
* item
+ item
- item
* item
- item {MD004}
+ item