2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-10-04 22:41:34 -07:00
|
|
|
const { addErrorDetailIf, isBlankLine } = require("../helpers");
|
|
|
|
const { getBlockQuotePrefixText, getHeadingLevel } = require("../helpers/micromark-helpers.cjs");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { filterByTypesCached } = require("./cache");
|
2023-08-08 22:56:47 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2022-12-07 21:16:05 -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 = {
|
2023-11-09 20:05:30 -08:00
|
|
|
"names": [ "MD022", "blanks-around-headings" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Headings should be surrounded by blank lines",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings", "blank_lines" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD022(params, onError) {
|
2023-08-08 22:56:47 -07:00
|
|
|
const getLinesAbove = getLinesFunction(params.config.lines_above);
|
|
|
|
const getLinesBelow = getLinesFunction(params.config.lines_below);
|
2024-02-28 21:01:23 -08:00
|
|
|
const { lines } = params;
|
2024-10-04 22:41:34 -07:00
|
|
|
const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
|
2023-08-05 23:09:07 -07:00
|
|
|
const { startLine, endLine } = heading;
|
|
|
|
const line = lines[startLine - 1].trim();
|
2023-08-06 15:24:35 -07:00
|
|
|
|
|
|
|
// Check lines above
|
2023-08-08 22:56:47 -07:00
|
|
|
const linesAbove = getLinesAbove(heading);
|
2023-08-06 15:24:35 -07:00
|
|
|
if (linesAbove >= 0) {
|
|
|
|
let actualAbove = 0;
|
2023-08-08 22:56:47 -07:00
|
|
|
for (
|
|
|
|
let i = 0;
|
|
|
|
(i < linesAbove) && isBlankLine(lines[startLine - 2 - i]);
|
|
|
|
i++
|
|
|
|
) {
|
|
|
|
actualAbove++;
|
2019-03-24 21:50:56 -07:00
|
|
|
}
|
2023-08-06 15:24:35 -07:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
startLine,
|
|
|
|
linesAbove,
|
|
|
|
actualAbove,
|
|
|
|
"Above",
|
|
|
|
line,
|
2024-06-09 17:09:03 -07:00
|
|
|
undefined,
|
2023-08-06 15:24:35 -07:00
|
|
|
{
|
2024-10-04 22:41:34 -07:00
|
|
|
"insertText": getBlockQuotePrefixText(
|
|
|
|
blockQuotePrefixes,
|
|
|
|
startLine - 1,
|
2023-08-08 22:56:47 -07:00
|
|
|
linesAbove - actualAbove
|
|
|
|
)
|
2023-08-06 15:24:35 -07:00
|
|
|
}
|
|
|
|
);
|
2019-03-24 21:50:56 -07:00
|
|
|
}
|
2023-08-06 15:24:35 -07:00
|
|
|
|
|
|
|
// Check lines below
|
2023-08-08 22:56:47 -07:00
|
|
|
const linesBelow = getLinesBelow(heading);
|
2023-08-06 15:24:35 -07:00
|
|
|
if (linesBelow >= 0) {
|
|
|
|
let actualBelow = 0;
|
2023-08-08 22:56:47 -07:00
|
|
|
for (
|
|
|
|
let i = 0;
|
|
|
|
(i < linesBelow) && isBlankLine(lines[endLine + i]);
|
|
|
|
i++
|
|
|
|
) {
|
|
|
|
actualBelow++;
|
2019-03-24 21:50:56 -07:00
|
|
|
}
|
2023-08-06 15:24:35 -07:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
startLine,
|
|
|
|
linesBelow,
|
|
|
|
actualBelow,
|
|
|
|
"Below",
|
|
|
|
line,
|
2024-06-09 17:09:03 -07:00
|
|
|
undefined,
|
2023-08-06 15:24:35 -07:00
|
|
|
{
|
|
|
|
"lineNumber": endLine + 1,
|
2024-10-04 22:41:34 -07:00
|
|
|
"insertText": getBlockQuotePrefixText(
|
|
|
|
blockQuotePrefixes,
|
|
|
|
endLine + 1,
|
2023-08-08 22:56:47 -07:00
|
|
|
linesBelow - actualBelow
|
|
|
|
)
|
2023-08-06 15:24:35 -07:00
|
|
|
}
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2023-08-05 23:09:07 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|