2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2024-06-01 21:32:10 -07:00
|
|
|
const { addErrorContextForLine, isBlankLine } = require("../helpers");
|
2024-03-18 20:48:22 -07:00
|
|
|
const { filterByPredicate, nonContentTokens } = require("../helpers/micromark.cjs");
|
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-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
|
"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;
|
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
|
|
|
|
|
const firstIndex = list.startLine - 1;
|
2019-03-20 21:48:18 -07:00
|
|
|
if (!isBlankLine(lines[firstIndex - 1])) {
|
2024-06-01 21:32:10 -07:00
|
|
|
addErrorContextForLine(
|
|
|
|
|
onError,
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
lines,
|
|
|
|
|
firstIndex
|
|
|
|
|
);
|
2023-07-18 22:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the "visual" end of the list
|
|
|
|
|
let endLine = list.endLine;
|
2023-10-25 20:05:19 -07:00
|
|
|
const flattenedChildren = filterByPredicate(list.children);
|
2023-08-29 22:38:00 -07:00
|
|
|
for (const child of flattenedChildren.reverse()) {
|
2023-07-18 22:33:57 -07:00
|
|
|
if (!nonContentTokens.has(child.type)) {
|
|
|
|
|
endLine = child.endLine;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2023-07-18 22:33:57 -07:00
|
|
|
|
|
|
|
|
// Look for a blank line below the list
|
|
|
|
|
const lastIndex = endLine - 1;
|
2019-03-20 21:48:18 -07:00
|
|
|
if (!isBlankLine(lines[lastIndex + 1])) {
|
2024-06-01 21:32:10 -07:00
|
|
|
addErrorContextForLine(
|
|
|
|
|
onError,
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
lines,
|
|
|
|
|
lastIndex,
|
|
|
|
|
lastIndex + 2
|
|
|
|
|
);
|
2019-01-21 18:21:36 -08:00
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
|
};
|