-
-
Notifications
You must be signed in to change notification settings - Fork 753
/
Copy pathmd032.mjs
76 lines (69 loc) · 2.36 KB
/
md032.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// @ts-check
import { addErrorContext, isBlankLine } from "../helpers/helpers.cjs";
import { filterByPredicate, getBlockQuotePrefixText, nonContentTokens } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
const isList = (token) => (
(token.type === "listOrdered") || (token.type === "listUnordered")
);
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD032", "blanks-around-lists" ],
"description": "Lists should be surrounded by blank lines",
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
"parser": "micromark",
"function": function MD032(params, onError) {
const { lines, parsers } = params;
const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
// For every top-level list...
const topLevelLists = filterByPredicate(
parsers.micromark.tokens,
isList,
(token) => (
(isList(token) || (token.type === "htmlFlow")) ? [] : token.children
)
);
for (const list of topLevelLists) {
// Look for a blank line above the list
const firstLineNumber = list.startLine;
if (!isBlankLine(lines[firstLineNumber - 2])) {
addErrorContext(
onError,
firstLineNumber,
lines[firstLineNumber - 1].trim(),
undefined,
undefined,
undefined,
{
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, firstLineNumber)
}
);
}
// Find the "visual" end of the list
const flattenedChildren = filterByPredicate(
list.children,
(token) => !nonContentTokens.has(token.type),
(token) => nonContentTokens.has(token.type) ? [] : token.children
);
let endLine = list.endLine;
if (flattenedChildren.length > 0) {
endLine = flattenedChildren[flattenedChildren.length - 1].endLine;
}
// Look for a blank line below the list
const lastLineNumber = endLine;
if (!isBlankLine(lines[lastLineNumber])) {
addErrorContext(
onError,
lastLineNumber,
lines[lastLineNumber - 1].trim(),
undefined,
undefined,
undefined,
{
"lineNumber": lastLineNumber + 1,
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, lastLineNumber)
}
);
}
}
}
};