markdownlint/lib/rules/firstLineH1.js
Josh Goldberg b63647f6b1 Moved rules into their own files
Everything under `lib/rules/*` is a rules file (with the name of the rule in camelCase), re-exported into an array in `lib/rules.js`.
Moved the regular expressions from `lib/rules.js` to `lib/expressions.js`, and the rest of the variables into `lib/shared.js`.
2017-10-29 19:55:01 -07:00

32 lines
1.1 KiB
JavaScript

module.exports = {
"name": "MD041",
"desc": "First line in file should be a top level header",
"tags": [ "headers" ],
"aliases": [ "first-line-h1" ],
"regexp": null,
"func": function MD041(params, errors) {
var level = params.options.level || 1;
var frontMatterTitle = params.options.front_matter_title;
var tag = "h" + level;
var frontMatterTitleRe =
new RegExp(frontMatterTitle || "^\\s*title:", "i");
params.tokens.every(function forToken(token, index) {
if (token.type === "heading_open") {
if (!((token.lineNumber === 1) || (index > 0)) ||
(token.tag !== tag)) {
errors.addContext(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);
})) {
errors.addContext(token.lineNumber, token.line);
}
return false;
});
}
};