forked from sindresorhus/awesome-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoc.js
187 lines (153 loc) · 4.75 KB
/
toc.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import {find} from 'unist-util-find';
import {findAllAfter} from 'unist-util-find-all-after';
import {findAllBefore} from 'unist-util-find-all-before';
import findAllBetween from 'unist-util-find-all-between';
import {lintRule} from 'unified-lint-rule';
import GitHubSlugger from 'github-slugger';
import {toString} from 'mdast-util-to-string';
import {visit} from 'unist-util-visit';
const slugger = new GitHubSlugger();
const maxListItemDepth = 1;
const sectionHeadingDenylist = new Set([
'Contributing',
'Footnotes',
]);
const tocRule = lintRule('remark-lint:awesome-toc', (ast, file) => {
slugger.reset();
// Heading links are order-dependent, so it's important to gather them up front
const headingLinks = buildHeadingLinks(ast);
const toc = find(ast, node => (
node.type === 'heading'
&& node.depth === 2
&& toString(node).replaceAll(/<!--.*?-->/g, '').trim() === 'Contents'
));
if (!toc) {
file.message('Missing or invalid Table of Contents', ast);
return;
}
const headingsPre = findAllBefore(ast, toc, {
type: 'heading',
});
const htmlPre = findAllBefore(ast, toc, {
type: 'html',
});
if (headingsPre.length > 1) {
file.message('Table of Contents must be the first section', toc);
} else if (headingsPre.length === 0 && htmlPre.length === 0) {
file.message('First heading should be name of awesome list', toc);
}
const headingsPost = findAllAfter(ast, toc, {
type: 'heading',
depth: 2,
}).filter(node => !sectionHeadingDenylist.has(toString(node)));
if (headingsPost.length === 0) {
file.message('Missing content headers', ast);
return;
}
const tocLists = findAllBetween(ast, toc, headingsPost[0], 'list');
if (tocLists.length === 0) {
file.message('Missing or invalid Table of Contents list', toc);
} else if (tocLists.length > 1) {
file.message('Multiple Table of Contents lists found', toc);
} else {
const tocList = tocLists[0];
// Validate list items against heading sections recursively
validateListItems({
ast,
file,
list: tocList,
headingLinks,
headings: headingsPost,
depth: 0,
});
}
});
function buildHeadingLinks(ast) {
const links = {};
visit(ast, 'heading', node => {
const text = toString(node);
const slug = slugger.slug(text);
const link = `#${slug}`;
links[link] = node;
});
return links;
}
function validateListItems({ast, file, list, headingLinks, headings, depth}) {
let index = 0;
if (list) {
for (; index < list.children.length; ++index) {
const listItem = list.children[index];
const link = find(listItem, n => n.type === 'link');
if (!link) {
file.message(`ToC item "${index}" missing link "${toString(listItem)}"`, listItem);
return;
}
const {url} = link;
const text = toString(link);
const heading = headings[index];
const headingText = heading && toString(heading);
if (!text) {
file.message(`ToC item "${url}" missing link text`, listItem);
return;
}
if (!headingText) {
if (sectionHeadingDenylist.has(text)) {
file.message(`ToC should not contain section "${text}"`, listItem);
} else {
file.message(`ToC item "${text}" missing corresponding heading`, listItem);
}
return;
}
if (text !== headingText) {
file.message(`ToC item "${text}" does not match corresponding heading "${headingText}"`, listItem);
return;
}
const headingLink = headingLinks[url];
if (headingLink) {
// Remember that we've referenced this link previously
headingLinks[url] = false;
} else if (headingLink === undefined) {
// This link doesn't exist as a section in the content
file.message(`ToC item "${text}" link "${url}" not found`, listItem);
return;
} else {
// This link was used previously, so it must be a duplicate
file.message(`ToC item "${text}" has duplicate link "${url}"`, listItem);
return;
}
const subList = find(listItem, n => n.type === 'list');
if (subList) {
if (depth < maxListItemDepth) {
const nextHeading = headings[index + 1];
const subHeadings = nextHeading ? findAllBetween(ast, heading, nextHeading, {
type: 'heading',
depth: depth + 3,
}) : findAllAfter(ast, heading, {
type: 'heading',
depth: depth + 3,
});
validateListItems({
ast,
file,
list: subList,
headingLinks,
headings: subHeadings,
depth: depth + 1,
});
} else {
file.message(`Exceeded max depth of ${maxListItemDepth + 1} levels`);
}
} else {
// No need to enforce the existence of a subList, even if there are corresponding
// subHeadings.
}
}
}
if (index < headings.length) {
for (; index < headings.length; ++index) {
const heading = headings[index];
file.message(`ToC missing item for "${toString(heading)}"`, list);
}
}
}
export default tocRule;