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 listItemMarkerInterruptsRe = /^[\s>]*(?:[*+-]|1\.)\s+/;
const blankOrListRe = /^[\s>]*($|\s)/;
const blankLineRe = /^[\s>]*$/;
module.exports = {
"names": [ "MD032", "blanks-around-lists" ],
"description": "Lists should be surrounded by blank lines",
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
"function": function MD032(params, onError) {
let inList = false;
let prevLine = "";
shared.forEachLine(
function forLine(line, lineIndex, inCode, onFence) {
if (!inCode || onFence) {
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)) {
shared.addErrorContext(onError, lineIndex, lineTrim);
}
inList = listMarker;
}
prevLine = line;
shared.flattenLists().filter((list) => !list.nesting).forEach((list) => {
const firstLineIndex = list.open.map[0];
if (!blankLineRe.test(params.lines[firstLineIndex - 1] || "")) {
shared.addErrorContext(
onError, firstLineIndex + 1, params.lines[firstLineIndex].trim());
}
);
const bottomLineIndex = list.lastLineIndex - 1;
if (!blankLineRe.test(params.lines[bottomLineIndex + 1] || "")) {
shared.addErrorContext(
onError, bottomLineIndex + 1, params.lines[bottomLineIndex].trim());
}
});
}
};

View file

@ -37,7 +37,7 @@
"istanbul": "~0.4.5",
"js-yaml": "~3.12.0",
"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-sup": "~1.0.0",
"nodeunit": "~0.11.3",

View file

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

View file

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

View file

@ -17,6 +17,15 @@
"errorContext": "* List",
"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,
"ruleNames": [ "MD033", "no-inline-html" ],

View file

@ -21,7 +21,7 @@ This non-paragraph has a dash
This non-paragraph has a dash
- 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 is a list. {MD030} {MD032}

View file

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

View file

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

View file

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

View file

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