2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-04-27 22:05:34 -07:00
|
|
|
const shared = require("./shared");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD041", "first-line-h1" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "First line in file should be a top level heading",
|
|
|
|
"tags": [ "headings", "headers" ],
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD041(params, onError) {
|
2018-04-27 22:05:34 -07:00
|
|
|
const level = params.config.level || 1;
|
|
|
|
const frontMatterTitle = params.config.front_matter_title;
|
|
|
|
const tag = "h" + level;
|
|
|
|
const frontMatterTitleRe =
|
2018-01-21 21:44:25 -08:00
|
|
|
new RegExp(frontMatterTitle || "^\\s*title\\s*[:=]", "i");
|
2018-04-29 12:02:47 -07:00
|
|
|
params.tokens.every(function forToken(token) {
|
|
|
|
if (token.type === "html_block") {
|
|
|
|
return true;
|
|
|
|
} else if (token.type === "heading_open") {
|
|
|
|
if (token.tag !== tag) {
|
2018-01-21 21:44:25 -08:00
|
|
|
shared.addErrorContext(onError, token.lineNumber, token.line);
|
|
|
|
}
|
2018-04-29 12:02:47 -07:00
|
|
|
} else if (((frontMatterTitle !== undefined) && !frontMatterTitle) ||
|
2018-01-21 21:44:25 -08:00
|
|
|
!params.frontMatterLines.some(function forLine(line) {
|
|
|
|
return frontMatterTitleRe.test(line);
|
|
|
|
})) {
|
|
|
|
shared.addErrorContext(onError, token.lineNumber, token.line);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|