Re-implement MD022/blanks-around-headings to ignore comments and blockquotes.

This commit is contained in:
David Anson 2019-03-21 21:42:24 -07:00
parent 1db87ef0c6
commit debc08bca1
5 changed files with 16 additions and 35 deletions

View file

@ -3,36 +3,18 @@
"use strict";
const shared = require("./shared");
const { addErrorContext, 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 prevHeadingLineNumber = 0;
let prevMaxLineIndex = -1;
let 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;
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());
}
});
}