Allow for first level indentation of lists in MD007/ul-indent (fixes #242).

This commit is contained in:
Jeremy Thomerson 2020-01-01 15:53:03 -05:00 committed by David Anson
parent 5634b317d6
commit 9aeddedb7d
7 changed files with 81 additions and 3 deletions

View file

@ -273,7 +273,7 @@ Tags: bullet, ul, indentation
Aliases: ul-indent Aliases: ul-indent
Parameters: indent (number; default 2) Parameters: indent, start_indented (number; default 2, boolean; default false)
This rule is triggered when list items are not indented by the configured This rule is triggered when list items are not indented by the configured
number of spaces (default: 2). number of spaces (default: 2).
@ -310,6 +310,10 @@ Note: This rule applies to a sublist only if its parent lists are all also
unordered (otherwise, extra indentation of ordered lists interferes with the unordered (otherwise, extra indentation of ordered lists interferes with the
rule). rule).
The `start_indented` parameter allows the first level of lists to be indented by
the configured number of spaces rather than starting at zero (the inverse of
MD006).
<a name="md009"></a> <a name="md009"></a>
## MD009 - Trailing spaces ## MD009 - Trailing spaces

View file

@ -11,12 +11,14 @@ module.exports = {
"description": "Unordered list indentation", "description": "Unordered list indentation",
"tags": [ "bullet", "ul", "indentation" ], "tags": [ "bullet", "ul", "indentation" ],
"function": function MD007(params, onError) { "function": function MD007(params, onError) {
const optionsIndent = params.config.indent || 2; const indent = params.config.indent || 2;
const startIndented = !!params.config.start_indented;
flattenedLists().forEach((list) => { flattenedLists().forEach((list) => {
if (list.unordered && list.parentsUnordered) { if (list.unordered && list.parentsUnordered) {
list.items.forEach((item) => { list.items.forEach((item) => {
const { lineNumber, line } = item; const { lineNumber, line } = item;
const expectedIndent = list.nesting * optionsIndent; const expectedNesting = list.nesting + (startIndented ? 1 : 0);
const expectedIndent = expectedNesting * indent;
const actualIndent = indentFor(item); const actualIndent = indentFor(item);
let range = null; let range = null;
let editColumn = 1; let editColumn = 1;

View file

@ -95,6 +95,11 @@ rules.forEach(function forRule(rule) {
"description": "Spaces for indent", "description": "Spaces for indent",
"type": "integer", "type": "integer",
"default": 2 "default": 2
},
"start_indented": {
"description": "Whether to indent the first level of the list",
"type": "boolean",
"default": false
} }
}; };
break; break;

View file

@ -230,6 +230,11 @@
"description": "Spaces for indent", "description": "Spaces for indent",
"type": "integer", "type": "integer",
"default": 2 "default": 2
},
"start_indented": {
"description": "Whether to indent the first level of the list",
"type": "boolean",
"default": false
} }
}, },
"additionalProperties": false "additionalProperties": false
@ -246,6 +251,11 @@
"description": "Spaces for indent", "description": "Spaces for indent",
"type": "integer", "type": "integer",
"default": 2 "default": 2
},
"start_indented": {
"description": "Whether to indent the first level of the list",
"type": "boolean",
"default": false
} }
}, },
"additionalProperties": false "additionalProperties": false

View file

@ -0,0 +1,8 @@
{
"default": true,
"MD006": false,
"MD007": {
"indent": 3,
"start_indented": true
}
}

View file

@ -0,0 +1,39 @@
# List Indentation - Start Indented
* item 1
* item 2
* item 2.1
* item 2.2
* item 2.2.1
* item 2.3
* item 3
## Disallowed List Indentation - Starts at Zero
* item 1 {MD007}
* item 2 {MD007}
* item 2.1 {MD007}
* item 2.2 {MD007}
* item 2.2.1 {MD007}
* item 2.3 {MD007}
* item 3 {MD007}
## Disallowed List Indentation - Starts at One
* item 1 {MD007}
* item 2 {MD007}
* item 2.1 {MD007}
* item 2.2 {MD007}
* item 2.2.1 {MD007}
* item 2.3 {MD007}
* item 3 {MD007}
## Disallowed List Indentation - Starts at Two
* item 1 {MD007}
* item 2 {MD007}
* item 2.1 {MD007}
* item 2.2 {MD007}
* item 2.2.1 {MD007}
* item 2.3 {MD007}
* item 3 {MD007}

View file

@ -44,3 +44,13 @@ Text
- Item {MD005} - Item {MD005}
Text Text
## Invalid Indentation - Should Start at Zero
- item 1 {MD006} {MD007}
- item 2 {MD006} {MD007}
- item 2.1 {MD007}
- item 2.2 {MD007}
- item 2.2.1 {MD007}
- item 2.3 {MD007}
- item 3 {MD006} {MD007}