mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 14:30:12 +01:00
Reimplement MD032/blanks-around-lists using micromark tokens, add newly-detected violations to test snapshot.
This commit is contained in:
parent
900fb349ee
commit
9646590496
5 changed files with 143 additions and 51 deletions
88
lib/md032.js
88
lib/md032.js
|
|
@ -4,46 +4,72 @@
|
|||
|
||||
const { addErrorContext, blockquotePrefixRe, isBlankLine } =
|
||||
require("../helpers");
|
||||
const { flattenedLists } = require("./cache");
|
||||
const { filterByPredicate, flattenedChildren } =
|
||||
require("../helpers/micromark.cjs");
|
||||
|
||||
const nonContentTokens = new Set([
|
||||
"blockQuoteMarker",
|
||||
"blockQuotePrefix",
|
||||
"blockQuotePrefixWhitespace",
|
||||
"lineEnding",
|
||||
"lineEndingBlank",
|
||||
"linePrefix",
|
||||
"listItemIndent"
|
||||
]);
|
||||
const isList = (token) => (
|
||||
(token.type === "listOrdered") || (token.type === "listUnordered")
|
||||
);
|
||||
const addBlankLineError = (onError, lines, lineIndex, lineNumber) => {
|
||||
const line = lines[lineIndex];
|
||||
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
|
||||
addErrorContext(
|
||||
onError,
|
||||
lineIndex + 1,
|
||||
line.trim(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
{
|
||||
lineNumber,
|
||||
"insertText": `${quotePrefix}\n`
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
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) {
|
||||
const { lines } = params;
|
||||
const filteredLists = flattenedLists().filter((list) => !list.nesting);
|
||||
for (const list of filteredLists) {
|
||||
const firstIndex = list.open.map[0];
|
||||
const { lines, parsers } = params;
|
||||
|
||||
// For every top-level list...
|
||||
const topLevelLists = filterByPredicate(
|
||||
parsers.micromark.tokens,
|
||||
isList,
|
||||
(token) => (isList(token) ? [] : token.children)
|
||||
);
|
||||
for (const list of topLevelLists) {
|
||||
|
||||
// Look for a blank line above the list
|
||||
const firstIndex = list.startLine - 1;
|
||||
if (!isBlankLine(lines[firstIndex - 1])) {
|
||||
const line = lines[firstIndex];
|
||||
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
|
||||
addErrorContext(
|
||||
onError,
|
||||
firstIndex + 1,
|
||||
line.trim(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
{
|
||||
"insertText": `${quotePrefix}\n`
|
||||
});
|
||||
addBlankLineError(onError, lines, firstIndex);
|
||||
}
|
||||
const lastIndex = list.lastLineIndex - 1;
|
||||
|
||||
// Find the "visual" end of the list
|
||||
let endLine = list.endLine;
|
||||
for (const child of flattenedChildren(list).reverse()) {
|
||||
if (!nonContentTokens.has(child.type)) {
|
||||
endLine = child.endLine;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for a blank line below the list
|
||||
const lastIndex = endLine - 1;
|
||||
if (!isBlankLine(lines[lastIndex + 1])) {
|
||||
const line = lines[lastIndex];
|
||||
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
|
||||
addErrorContext(
|
||||
onError,
|
||||
lastIndex + 1,
|
||||
line.trim(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
{
|
||||
"lineNumber": lastIndex + 2,
|
||||
"insertText": `${quotePrefix}\n`
|
||||
});
|
||||
addBlankLineError(onError, lines, lastIndex, lastIndex + 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue