Re-implement MD032 to detect missed scenarios and simplify.

This commit is contained in:
David Anson 2019-01-21 18:21:36 -08:00
parent 8a175955d7
commit 3b49414183
10 changed files with 88 additions and 51 deletions

View file

@ -4,35 +4,24 @@
const shared = require("./shared"); const shared = require("./shared");
const listItemMarkerInterruptsRe = /^[\s>]*(?:[*+-]|1\.)\s+/; const blankLineRe = /^[\s>]*$/;
const blankOrListRe = /^[\s>]*($|\s)/;
module.exports = { module.exports = {
"names": [ "MD032", "blanks-around-lists" ], "names": [ "MD032", "blanks-around-lists" ],
"description": "Lists should be surrounded by blank lines", "description": "Lists should be surrounded by blank lines",
"tags": [ "bullet", "ul", "ol", "blank_lines" ], "tags": [ "bullet", "ul", "ol", "blank_lines" ],
"function": function MD032(params, onError) { "function": function MD032(params, onError) {
let inList = false; shared.flattenLists().filter((list) => !list.nesting).forEach((list) => {
let prevLine = ""; const firstLineIndex = list.open.map[0];
shared.forEachLine( if (!blankLineRe.test(params.lines[firstLineIndex - 1] || "")) {
function forLine(line, lineIndex, inCode, onFence) { shared.addErrorContext(
if (!inCode || onFence) { onError, firstLineIndex + 1, params.lines[firstLineIndex].trim());
const lineTrim = line.trim();
let listMarker = shared.listItemMarkerRe.test(lineTrim);
if (listMarker && !inList && !blankOrListRe.test(prevLine)) {
// Check whether this list prefix can interrupt a paragraph
if (listItemMarkerInterruptsRe.test(lineTrim)) {
shared.addErrorContext(onError, lineIndex + 1, lineTrim);
} else {
listMarker = false;
} }
} else if (!listMarker && inList && !blankOrListRe.test(line)) { const bottomLineIndex = list.lastLineIndex - 1;
shared.addErrorContext(onError, lineIndex, lineTrim); if (!blankLineRe.test(params.lines[bottomLineIndex + 1] || "")) {
shared.addErrorContext(
onError, bottomLineIndex + 1, params.lines[bottomLineIndex].trim());
} }
inList = listMarker; });
}
prevLine = line;
}
);
} }
}; };

View file

@ -37,7 +37,7 @@
"istanbul": "~0.4.5", "istanbul": "~0.4.5",
"js-yaml": "~3.12.0", "js-yaml": "~3.12.0",
"markdown-it-for-inline": "~0.1.1", "markdown-it-for-inline": "~0.1.1",
"markdown-it-mathjax": "~2.0.0", "markdown-it-katex": "~2.0.3",
"markdown-it-sub": "~1.0.0", "markdown-it-sub": "~1.0.0",
"markdown-it-sup": "~1.0.0", "markdown-it-sup": "~1.0.0",
"nodeunit": "~0.11.3", "nodeunit": "~0.11.3",

View file

@ -4,8 +4,8 @@
# Heading 3 {MD003} {MD043} # # Heading 3 {MD003} {MD043} #
* list * list {MD032}
+ list {MD004} {MD006} {MD007} {MD030} + list {MD004} {MD006} {MD007} {MD030} {MD032}
* list * list
* list {MD007} * list {MD007}

View file

@ -43,3 +43,6 @@ Text
text `code text `code
span ` text span ` text
text. text.
* List
---

View file

@ -17,6 +17,15 @@
"errorContext": "* List", "errorContext": "* List",
"errorRange": null "errorRange": null
}, },
{
"lineNumber": 47,
"ruleNames": [ "MD032", "blanks-around-lists" ],
"ruleDescription": "Lists should be surrounded by blank lines",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.11.0/doc/Rules.md#md032",
"errorDetail": null,
"errorContext": "* List",
"errorRange": null
},
{ {
"lineNumber": 6, "lineNumber": 6,
"ruleNames": [ "MD033", "no-inline-html" ], "ruleNames": [ "MD033", "no-inline-html" ],

View file

@ -21,7 +21,7 @@ This non-paragraph has a dash
This non-paragraph has a dash This non-paragraph has a dash
- in its list. It also has a plus {MD032} - in its list. It also has a plus {MD032}
+ in its list. {MD004} + in its list. {MD004} {MD032}
This non-paragraph has spaces and a dash This non-paragraph has spaces and a dash
- This is a list. {MD030} {MD032} - This is a list. {MD030} {MD032}

View file

@ -32,6 +32,27 @@ Text
> - Item {MD004} > - Item {MD004}
> - Item {MD004} > - Item {MD004}
Text
> Text
>
> + Item
> + Item
>
> Text
Text
> Text
> + Item {MD032}
> + Item
Text
> + Item
> + Item {MD032}
> ---
Text Text
> 1. Item > 1. Item
@ -49,22 +70,42 @@ Text
Text Text
> Text > Text
>
> > - Item {MD004} > > - Item {MD004}
> > - Item {MD004} {MD027} > > - Item {MD004} {MD027}
> > - Item {MD004} > > - Item {MD004}
> >
> > > - Item {MD004} > > > - Item {MD004}
> > > - Item {MD004} {MD027} > > > - Item {MD004} {MD027}
> > > - Item {MD004} {MD027} > > > - Item {MD004} {MD027}
Text
> Text
> > - Item {MD004} {MD032}
> > - Item {MD004} {MD027}
> > - Item {MD004} {MD032}
> > > - Item {MD004} {MD032}
> > > - Item {MD004} {MD027}
> > > - Item {MD004} {MD027}
Text Text
>+ Item >+ Item
> + Item > + Item
>>
>>+ Item >>+ Item
>> + Item >> + Item
Text Text
>+ Item
> + Item {MD032}
>>+ Item {MD032}
>> + Item
Text
> Quoted text > Quoted text
> >
> * A list {MD004} > * A list {MD004}

View file

@ -14,15 +14,15 @@
## Insufficient nesting ## Insufficient nesting
- one - one {MD032}
1. two 1. two {MD032}
1. three 1. three {MD032}
- four - four {MD032}
1. one 1. one {MD032}
- two {MD006} - two {MD006} {MD032}
- three - three {MD032}
1. four 1. four {MD032}
## Correct nesting, same type ## Correct nesting, same type

View file

@ -4,19 +4,19 @@ text
* list * list
text ---
* list {MD032} * list {MD032}
text ---
+ list {MD032} + list {MD032}
text ---
- list {MD032} - list {MD032}
text ---
1. list {MD032} 1. list {MD032}
text ---
* list * list
* list {MD032} * list {MD032}
text ---
text text
1. list {MD032} 1. list {MD032}

View file

@ -5,7 +5,7 @@ const path = require("path");
const { URL } = require("url"); const { URL } = require("url");
const md = require("markdown-it")(); const md = require("markdown-it")();
const pluginInline = require("markdown-it-for-inline"); const pluginInline = require("markdown-it-for-inline");
const pluginMathjax = require("markdown-it-mathjax"); const pluginKatex = require("markdown-it-katex");
const pluginSub = require("markdown-it-sub"); const pluginSub = require("markdown-it-sub");
const pluginSup = require("markdown-it-sup"); const pluginSup = require("markdown-it-sup");
const tv4 = require("tv4"); const tv4 = require("tv4");
@ -792,7 +792,7 @@ module.exports.styleAll = function styleAll(test) {
"MD029": [ 47 ], "MD029": [ 47 ],
"MD030": [ 8 ], "MD030": [ 8 ],
"MD031": [ 50 ], "MD031": [ 50 ],
"MD032": [ 51 ], "MD032": [ 7, 8, 51 ],
"MD033": [ 55 ], "MD033": [ 55 ],
"MD034": [ 57 ], "MD034": [ 57 ],
"MD035": [ 61 ], "MD035": [ 61 ],
@ -839,7 +839,7 @@ module.exports.styleRelaxed = function styleRelaxed(test) {
"MD026": [ 40 ], "MD026": [ 40 ],
"MD029": [ 47 ], "MD029": [ 47 ],
"MD031": [ 50 ], "MD031": [ 50 ],
"MD032": [ 51 ], "MD032": [ 7, 8, 51 ],
"MD035": [ 61 ], "MD035": [ 61 ],
"MD036": [ 65 ], "MD036": [ 65 ],
"MD042": [ 77 ], "MD042": [ 77 ],
@ -2519,15 +2519,10 @@ module.exports.markdownItPluginsMathjax =
"+ 2\n" + "+ 2\n" +
"+ 3$$\n" "+ 3$$\n"
}, },
"markdownItPlugins": [ [ pluginMathjax ] ], "markdownItPlugins": [ [ pluginKatex ] ]
"resultVersion": 0
}, function callback(err, actual) { }, function callback(err, actual) {
test.ifError(err); test.ifError(err);
const expected = { const expected = { "string": [] };
"string": {
"MD032": [ 8 ]
}
};
test.deepEqual(actual, expected, "Unexpected issues."); test.deepEqual(actual, expected, "Unexpected issues.");
test.done(); test.done();
}); });