forked from sindresorhus/awesome-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheading.js
52 lines (39 loc) · 1.02 KB
/
heading.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
43
44
45
46
47
48
49
50
51
52
import {lintRule} from 'unified-lint-rule';
import {visit} from 'unist-util-visit';
import case_ from 'case';
const {of: caseOf, title: titleCase} = case_;
const listHeadingCaseAllowList = new Set([
'title',
'capital',
]);
const headingRule = lintRule('remark-lint:awesome-heading', (ast, file) => {
let headings = 0;
visit(ast, (node, index) => {
if (node.type !== 'heading') {
return;
}
if (node.depth > 1) {
if (index !== 0) {
return;
}
file.message('Main list heading must be of depth 1', node);
}
for (const child of node.children) {
if (child.type !== 'text') {
continue;
}
const headingText = child.value;
if (!listHeadingCaseAllowList.has(caseOf(headingText)) && titleCase(headingText) !== headingText) {
file.message('Main heading must be in title case', node);
}
}
headings++;
if (headings > 1) {
file.message('List can only have one heading', node);
}
});
if (headings === 0) {
file.message('Missing main list heading');
}
});
export default headingRule;