-
-
Notifications
You must be signed in to change notification settings - Fork 758
/
Copy pathmd025.mjs
37 lines (35 loc) · 1.23 KB
/
md025.mjs
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
// @ts-check
import { addErrorContext, frontMatterHasTitle } from "../helpers/helpers.cjs";
import { getHeadingLevel, getHeadingText } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD025", "single-title", "single-h1" ],
"description": "Multiple top-level headings in the same document",
"tags": [ "headings" ],
"parser": "micromark",
"function": function MD025(params, onError) {
const level = Number(params.config.level || 1);
const foundFrontMatterTitle =
frontMatterHasTitle(
params.frontMatterLines,
params.config.front_matter_title
);
let hasTopLevelHeading = false;
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
const headingLevel = getHeadingLevel(heading);
if (headingLevel === level) {
if (hasTopLevelHeading || foundFrontMatterTitle) {
const headingText = getHeadingText(heading);
addErrorContext(
onError,
heading.startLine,
headingText
);
} else if (heading.startLine === 1) {
hasTopLevelHeading = true;
}
}
}
}
};