From 7a1773ea77e6ea1757fe817f207f97b4a679c6e4 Mon Sep 17 00:00:00 2001 From: David Anson Date: Mon, 4 Jul 2016 14:32:40 -0700 Subject: [PATCH] Add "sublist" style to MD004 (fixes #21). --- doc/Rules.md | 19 +++++++++--- lib/rules.js | 17 +++++++++-- test/sublist-bullet-style.json | 6 ++++ test/sublist-bullet-style.md | 56 ++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 test/sublist-bullet-style.json create mode 100644 test/sublist-bullet-style.md diff --git a/doc/Rules.md b/doc/Rules.md index 18618d6e..8640805b 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -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 diff --git a/lib/rules.js b/lib/rules.js index 9e7072bb..bccdef2d 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -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); } }); diff --git a/test/sublist-bullet-style.json b/test/sublist-bullet-style.json new file mode 100644 index 00000000..58953413 --- /dev/null +++ b/test/sublist-bullet-style.json @@ -0,0 +1,6 @@ +{ + "default": true, + "MD004": { + "style": "sublist" + } +} diff --git a/test/sublist-bullet-style.md b/test/sublist-bullet-style.md new file mode 100644 index 00000000..a61ca6bf --- /dev/null +++ b/test/sublist-bullet-style.md @@ -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