-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
Copy pathmd041.js
42 lines (39 loc) · 1.34 KB
/
md041.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// @ts-check
"use strict";
const { addErrorContext, frontMatterHasTitle } = require("../helpers");
module.exports = {
"names": [ "MD041", "first-line-heading", "first-line-h1" ],
"description": "First line in a file should be a top-level heading",
"tags": [ "headings", "headers" ],
"function": function MD041(params, onError) {
const level = Number(params.config.level || 1);
const tag = "h" + level;
const foundFrontMatterTitle =
frontMatterHasTitle(
params.frontMatterLines,
params.config.front_matter_title
);
if (!foundFrontMatterTitle) {
const htmlHeadingRe = new RegExp(`^<h${level}[ />]`, "i");
params.parsers.markdownit.tokens.every((token) => {
let isError = false;
if (token.type === "html_block") {
if (token.content.startsWith("<!--")) {
// Ignore leading HTML comments
return true;
} else if (!htmlHeadingRe.test(token.content)) {
// Something other than an HTML heading
isError = true;
}
} else if ((token.type !== "heading_open") || (token.tag !== tag)) {
// Something other than a Markdown heading
isError = true;
}
if (isError) {
addErrorContext(onError, token.lineNumber, token.line);
}
return false;
});
}
}
};