-
-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathmd022.mjs
97 lines (91 loc) · 2.78 KB
/
md022.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// @ts-check
import { addErrorDetailIf, isBlankLine } from "../helpers/helpers.cjs";
import { getBlockQuotePrefixText, getHeadingLevel } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
const defaultLines = 1;
const getLinesFunction = (linesParam) => {
if (Array.isArray(linesParam)) {
const linesArray = new Array(6).fill(defaultLines);
for (const [ index, value ] of [ ...linesParam.entries() ].slice(0, 6)) {
linesArray[index] = value;
}
return (heading) => linesArray[getHeadingLevel(heading) - 1];
}
// Coerce linesParam to a number
const lines = (linesParam === undefined) ? defaultLines : Number(linesParam);
return () => lines;
};
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD022", "blanks-around-headings" ],
"description": "Headings should be surrounded by blank lines",
"tags": [ "headings", "blank_lines" ],
"parser": "micromark",
"function": function MD022(params, onError) {
const getLinesAbove = getLinesFunction(params.config.lines_above);
const getLinesBelow = getLinesFunction(params.config.lines_below);
const { lines } = params;
const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
const { startLine, endLine } = heading;
const line = lines[startLine - 1].trim();
// Check lines above
const linesAbove = getLinesAbove(heading);
if (linesAbove >= 0) {
let actualAbove = 0;
for (
let i = 0;
(i < linesAbove) && isBlankLine(lines[startLine - 2 - i]);
i++
) {
actualAbove++;
}
addErrorDetailIf(
onError,
startLine,
linesAbove,
actualAbove,
"Above",
line,
undefined,
{
"insertText": getBlockQuotePrefixText(
blockQuotePrefixes,
startLine - 1,
linesAbove - actualAbove
)
}
);
}
// Check lines below
const linesBelow = getLinesBelow(heading);
if (linesBelow >= 0) {
let actualBelow = 0;
for (
let i = 0;
(i < linesBelow) && isBlankLine(lines[endLine + i]);
i++
) {
actualBelow++;
}
addErrorDetailIf(
onError,
startLine,
linesBelow,
actualBelow,
"Below",
line,
undefined,
{
"lineNumber": endLine + 1,
"insertText": getBlockQuotePrefixText(
blockQuotePrefixes,
endLine + 1,
linesBelow - actualBelow
)
}
);
}
}
}
};