2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2018-04-27 22:05:34 -07:00
|
|
|
const shared = require("./shared");
|
2019-03-24 21:50:56 -07:00
|
|
|
const { addErrorDetailIf, filterTokens, isBlankLine } = shared;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
|
module.exports = {
|
2018-03-19 23:39:42 +01:00
|
|
|
"names": [ "MD022", "blanks-around-headings", "blanks-around-headers" ],
|
|
|
|
|
"description": "Headings should be surrounded by blank lines",
|
|
|
|
|
"tags": [ "headings", "headers", "blank_lines" ],
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD022(params, onError) {
|
2019-03-24 21:50:56 -07:00
|
|
|
let linesAbove = params.config.lines_above;
|
|
|
|
|
if (linesAbove === undefined) {
|
|
|
|
|
linesAbove = 1;
|
|
|
|
|
}
|
|
|
|
|
let linesBelow = params.config.lines_below;
|
|
|
|
|
if (linesBelow === undefined) {
|
|
|
|
|
linesBelow = 1;
|
|
|
|
|
}
|
2019-03-21 21:42:24 -07:00
|
|
|
const { lines } = params;
|
|
|
|
|
filterTokens(params, "heading_open", (token) => {
|
|
|
|
|
const [ topIndex, nextIndex ] = token.map;
|
2019-03-24 21:50:56 -07:00
|
|
|
for (let i = 0; i < linesAbove; i++) {
|
|
|
|
|
if (!isBlankLine(lines[topIndex - i - 1])) {
|
|
|
|
|
addErrorDetailIf(onError, topIndex + 1, linesAbove, i, "Above",
|
|
|
|
|
lines[topIndex].trim());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (let i = 0; i < linesBelow; i++) {
|
|
|
|
|
if (!isBlankLine(lines[nextIndex + i])) {
|
|
|
|
|
addErrorDetailIf(onError, topIndex + 1, linesBelow, i, "Below",
|
|
|
|
|
lines[topIndex].trim());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|