Enhance MD022/blanks-around-headings with lines_above/lines_below parameters (fixes #143).

This commit is contained in:
David Anson 2019-03-24 21:50:56 -07:00
parent debc08bca1
commit fa04d29485
24 changed files with 278 additions and 24 deletions

View file

@ -3,18 +3,37 @@
"use strict";
const shared = require("./shared");
const { addErrorContext, filterTokens, isBlankLine } = shared;
const { addErrorDetailIf, filterTokens, isBlankLine } = shared;
module.exports = {
"names": [ "MD022", "blanks-around-headings", "blanks-around-headers" ],
"description": "Headings should be surrounded by blank lines",
"tags": [ "headings", "headers", "blank_lines" ],
"function": function MD022(params, onError) {
let linesAbove = params.config.lines_above;
if (linesAbove === undefined) {
linesAbove = 1;
}
let linesBelow = params.config.lines_below;
if (linesBelow === undefined) {
linesBelow = 1;
}
const { lines } = params;
filterTokens(params, "heading_open", (token) => {
const [ topIndex, nextIndex ] = token.map;
if (!isBlankLine(lines[topIndex - 1]) || !isBlankLine(lines[nextIndex])) {
addErrorContext(onError, topIndex + 1, lines[topIndex].trim());
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;
}
}
});
}