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());
}
});
}
};