markdownlint/lib/md022.js
Milos Levacic 45424cf459 Change "header" to "heading" across the library
This should be backward compatible, as all "header" aliases are still
available, though documented as discouraged for future use.
2018-04-18 22:27:48 -07:00

39 lines
1.2 KiB
JavaScript

// @ts-check
"use strict";
var shared = require("./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) {
var prevHeadingLineNumber = 0;
var prevMaxLineIndex = -1;
var needBlankLine = false;
params.tokens.forEach(function forToken(token) {
if (token.type === "heading_open") {
if ((token.map[0] - prevMaxLineIndex) === 0) {
shared.addErrorContext(onError, token.lineNumber,
token.line.trim());
}
} else if (token.type === "heading_close") {
needBlankLine = true;
}
if (token.map) {
if (needBlankLine) {
if ((token.map[0] - prevMaxLineIndex) === 0) {
shared.addErrorContext(onError, prevHeadingLineNumber,
params.lines[prevHeadingLineNumber - 1].trim());
}
needBlankLine = false;
}
prevMaxLineIndex = Math.max(prevMaxLineIndex, token.map[1]);
}
if (token.type === "heading_open") {
prevHeadingLineNumber = token.lineNumber;
}
});
}
};