-
-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathmd024.mjs
45 lines (43 loc) · 1.39 KB
/
md024.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
38
39
40
41
42
43
44
45
// @ts-check
import { addErrorContext } from "../helpers/helpers.cjs";
import { getHeadingLevel, getHeadingText } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD024", "no-duplicate-heading" ],
"description": "Multiple headings with the same content",
"tags": [ "headings" ],
"parser": "micromark",
"function": function MD024(params, onError) {
const siblingsOnly = !!params.config.siblings_only || false;
const knownContents = [ null, [] ];
let lastLevel = 1;
let knownContent = knownContents[lastLevel];
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
const headingText = getHeadingText(heading);
if (siblingsOnly) {
const newLevel = getHeadingLevel(heading);
while (lastLevel < newLevel) {
lastLevel++;
knownContents[lastLevel] = [];
}
while (lastLevel > newLevel) {
knownContents[lastLevel] = [];
lastLevel--;
}
knownContent = knownContents[newLevel];
}
// @ts-ignore
if (knownContent.includes(headingText)) {
addErrorContext(
onError,
heading.startLine,
headingText.trim()
);
} else {
// @ts-ignore
knownContent.push(headingText);
}
}
}
};