markdownlint/lib/md041.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

36 lines
1.2 KiB
JavaScript

// @ts-check
"use strict";
var shared = require("./shared");
module.exports = {
"names": [ "MD041", "first-line-h1" ],
"description": "First line in file should be a top level heading",
"tags": [ "headings", "headers" ],
"function": function MD041(params, onError) {
var level = params.config.level || 1;
var frontMatterTitle = params.config.front_matter_title;
var tag = "h" + level;
var frontMatterTitleRe =
new RegExp(frontMatterTitle || "^\\s*title\\s*[:=]", "i");
params.tokens.every(function forToken(token, index) {
if (token.type === "heading_open") {
if (!((token.lineNumber === 1) || (index > 0)) ||
(token.tag !== tag)) {
shared.addErrorContext(onError, token.lineNumber, token.line);
}
return false;
} else if (token.type === "html_block") {
return true;
}
if (((frontMatterTitle !== undefined) && !frontMatterTitle) ||
!params.frontMatterLines.some(function forLine(line) {
return frontMatterTitleRe.test(line);
})) {
shared.addErrorContext(onError, token.lineNumber, token.line);
}
return false;
});
}
};