2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorContext, isBlankLine } from "../helpers/helpers.cjs";
|
|
|
|
import { filterByPredicate, getBlockQuotePrefixText, nonContentTokens } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2023-07-18 22:33:57 -07:00
|
|
|
|
|
|
|
const isList = (token) => (
|
|
|
|
(token.type === "listOrdered") || (token.type === "listUnordered")
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2018-01-21 21:44:25 -08:00
|
|
|
"names": [ "MD032", "blanks-around-lists" ],
|
|
|
|
"description": "Lists should be surrounded by blank lines",
|
|
|
|
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD032(params, onError) {
|
2024-06-21 21:03:30 -07:00
|
|
|
const { lines, parsers } = params;
|
2024-10-04 22:41:34 -07:00
|
|
|
const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
|
2023-07-18 22:33:57 -07:00
|
|
|
|
|
|
|
// For every top-level list...
|
|
|
|
const topLevelLists = filterByPredicate(
|
2024-06-21 21:03:30 -07:00
|
|
|
parsers.micromark.tokens,
|
2023-07-18 22:33:57 -07:00
|
|
|
isList,
|
2023-09-02 12:07:14 -07:00
|
|
|
(token) => (
|
|
|
|
(isList(token) || (token.type === "htmlFlow")) ? [] : token.children
|
|
|
|
)
|
2023-07-18 22:33:57 -07:00
|
|
|
);
|
|
|
|
for (const list of topLevelLists) {
|
|
|
|
|
|
|
|
// Look for a blank line above the list
|
2024-10-04 22:41:34 -07:00
|
|
|
const firstLineNumber = list.startLine;
|
|
|
|
if (!isBlankLine(lines[firstLineNumber - 2])) {
|
|
|
|
addErrorContext(
|
2024-06-01 21:32:10 -07:00
|
|
|
onError,
|
2024-10-04 22:41:34 -07:00
|
|
|
firstLineNumber,
|
|
|
|
lines[firstLineNumber - 1].trim(),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, firstLineNumber)
|
|
|
|
}
|
2024-06-01 21:32:10 -07:00
|
|
|
);
|
2023-07-18 22:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the "visual" end of the list
|
2024-12-16 20:54:37 -08:00
|
|
|
const flattenedChildren = filterByPredicate(
|
|
|
|
list.children,
|
|
|
|
(token) => !nonContentTokens.has(token.type),
|
|
|
|
(token) => nonContentTokens.has(token.type) ? [] : token.children
|
|
|
|
);
|
2023-07-18 22:33:57 -07:00
|
|
|
let endLine = list.endLine;
|
2024-12-16 20:54:37 -08:00
|
|
|
if (flattenedChildren.length > 0) {
|
|
|
|
endLine = flattenedChildren[flattenedChildren.length - 1].endLine;
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2023-07-18 22:33:57 -07:00
|
|
|
|
|
|
|
// Look for a blank line below the list
|
2024-10-04 22:41:34 -07:00
|
|
|
const lastLineNumber = endLine;
|
|
|
|
if (!isBlankLine(lines[lastLineNumber])) {
|
|
|
|
addErrorContext(
|
2024-06-01 21:32:10 -07:00
|
|
|
onError,
|
2024-10-04 22:41:34 -07:00
|
|
|
lastLineNumber,
|
|
|
|
lines[lastLineNumber - 1].trim(),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
"lineNumber": lastLineNumber + 1,
|
|
|
|
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, lastLineNumber)
|
|
|
|
}
|
2024-06-01 21:32:10 -07:00
|
|
|
);
|
2019-01-21 18:21:36 -08:00
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|