Update MD041/first-line-h1 to allow secondary headings if a YAML title is present (refs #116).

This commit is contained in:
David Anson 2019-03-10 22:10:33 -07:00
parent ed295cd947
commit c71bb4fbd5
4 changed files with 46 additions and 27 deletions

View file

@ -10,24 +10,24 @@ module.exports = {
"tags": [ "headings", "headers" ],
"function": function MD041(params, onError) {
const level = params.config.level || 1;
const frontMatterTitle = params.config.front_matter_title;
const tag = "h" + level;
const frontMatterTitle = params.config.front_matter_title;
const ignoreFrontMatter =
(frontMatterTitle !== undefined) && !frontMatterTitle;
const frontMatterTitleRe =
new RegExp(frontMatterTitle || "^\\s*title\\s*[:=]", "i");
params.tokens.every(function forToken(token) {
if (token.type === "html_block") {
return true;
} else if (token.type === "heading_open") {
if (token.tag !== tag) {
const foundFrontMatterTitle = !ignoreFrontMatter &&
params.frontMatterLines.some((line) => frontMatterTitleRe.test(line));
if (!foundFrontMatterTitle) {
params.tokens.every((token) => {
if (token.type === "html_block") {
return true;
}
if ((token.type !== "heading_open") || (token.tag !== tag)) {
shared.addErrorContext(onError, token.lineNumber, token.line);
}
} else if (((frontMatterTitle !== undefined) && !frontMatterTitle) ||
!params.frontMatterLines.some(function forLine(line) {
return frontMatterTitleRe.test(line);
})) {
shared.addErrorContext(onError, token.lineNumber, token.line);
}
return false;
});
return false;
});
}
}
};