forked from markuplint/markuplint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (49 loc) · 1.36 KB
/
index.ts
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
53
54
55
import { Element, Result, createRule } from '@markuplint/ml-core';
export type Value = boolean;
export interface RequiredH1Options {
'expected-once': boolean;
'in-document-fragment': boolean;
}
export default createRule<Value, RequiredH1Options>({
name: 'required-h1',
defaultValue: true,
defaultOptions: {
'expected-once': true,
'in-document-fragment': false,
},
async verify(document, translate, globalRule) {
const reports: Result[] = [];
const h1Stack: Element<Value, RequiredH1Options>[] = [];
if (document.nodeList.length === 0) {
return reports;
}
if (!globalRule.option['in-document-fragment'] && document.isFragment) {
return reports;
}
await document.walkOn('Element', async node => {
if (node.nodeName.toLowerCase() === 'h1') {
h1Stack.push(node);
}
});
if (h1Stack.length === 0) {
const message = translate('Missing the {0} {1}', 'h1', 'element');
reports.push({
severity: globalRule.severity,
message,
line: 1,
col: 1,
raw: document.nodeList[0].raw.slice(0, 1),
});
} else if (globalRule.option['expected-once'] && h1Stack.length > 1) {
const message = translate('Duplicate the {0} {1}', 'h1', 'element');
reports.push({
severity: globalRule.severity,
message,
line: h1Stack[1].startLine,
col: h1Stack[1].startCol,
raw: h1Stack[1].raw,
});
}
return reports;
},
});