forked from sindresorhus/awesome-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-item.js
350 lines (283 loc) · 8.32 KB
/
list-item.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import case_ from 'case';
import emojiRegex from 'emoji-regex';
import {find} from 'unist-util-find';
import {findAllAfter} from 'unist-util-find-all-after';
import isUrl from 'is-url-superb';
import {lintRule} from 'unified-lint-rule';
import {toString} from 'mdast-util-to-string';
import {visit} from 'unist-util-visit';
import identifierAllowList from '../lib/identifier-allow-list.js';
const {of: caseOf} = case_;
// Valid casings for first text word in list item descriptions
const listItemPrefixCaseAllowList = new Set([
'camel',
'capital',
'constant',
'pascal',
'upper',
]);
// Valid node types in list item link
const listItemLinkNodeAllowList = new Set([
'emphasis',
'inlineCode',
'text',
]);
// Valid node types in list item descriptions
const listItemDescriptionNodeAllowList = new Set([
'emphasis',
'footnoteReference',
'html',
'image',
'inlineCode',
'link',
'linkReference',
'strong',
'text',
]);
// Valid node types in list item description suffix
const listItemDescriptionSuffixNodeAllowList = new Set([
'emphasis',
'html',
'image',
'link',
'strong',
'text',
]);
const listItemRule = lintRule('remark-lint:awesome-list-item', (ast, file) => {
let lists = findAllLists(ast);
const toc = find(ast, node => (
node.type === 'heading'
&& node.depth === 2
&& toString(node).replaceAll(/<!--.*?-->/g, '').trim() === 'Contents'
));
if (toc) {
const postContentsHeading = findAllAfter(ast, toc, {
type: 'heading',
})[0];
if (!postContentsHeading) {
return;
}
lists = extractSublists(findAllAfter(ast, postContentsHeading, {type: 'list'}));
}
for (const list of lists) {
validateList(list, file);
}
});
function findAllLists(ast) {
const lists = [];
visit(ast, 'list', list => {
lists.push(list);
});
return lists;
}
function extractSublists(lists) {
let allLists = [];
for (const list of lists) {
allLists = [...allLists, ...findAllLists(list)];
}
return allLists;
}
function validateList(list, file) {
for (const listItem of list.children) {
const [paragraph] = listItem.children;
if (!paragraph || paragraph.type !== 'paragraph' || paragraph.children.length === 0) {
file.message('Invalid list item', paragraph);
continue;
}
const [link, ...description] = paragraph.children;
if (link.type === 'text') {
continue;
}
if (!validateListItemLink(link, file)) {
continue;
}
validateListItemDescription(description, file);
}
}
function validateListItemLink(link, file) {
if (link.type !== 'link') {
file.message('Invalid list item link', link);
return false;
}
if (!isUrl(link.url)) {
file.message('Invalid list item link URL', link);
return false;
}
const linkText = toString(link);
if (!linkText) {
file.message('Invalid list item link text', link);
return false;
}
for (const node of link.children) {
if (!listItemLinkNodeAllowList.has(node.type)) {
file.message('Invalid list item link', node);
return false;
}
}
return true;
}
function validateListItemDescription(description, file) {
if (description.length === 0) {
// In certain, rare cases it's okay to leave off an item's description.
return;
}
const prefix = description[0];
const suffix = description.at(-1);
const descriptionText = toString({type: 'root', children: description});
const prefixText = toString(prefix);
const suffixText = toString(suffix);
// Check for special-cases with simple descriptions
if (validateListItemSpecialCases(description, descriptionText)) {
return true;
}
// Ensure description starts with a dash separator or an acceptable special-case
if (prefix.type !== 'text' || !validateListItemPrefix(descriptionText, prefixText)) {
if (/^[\s\u00A0]-[\s\u00A0]/.test(prefixText)) {
file.message('List item link and description separated by invalid whitespace', prefix);
return false;
}
if (/^\s*—/.test(prefixText)) {
file.message('List item link and description separated by invalid en-dash', prefix);
return false;
}
file.message('List item link and description must be separated with a dash', prefix);
return false;
}
// Ensure description ends with an acceptable node type
if (!listItemDescriptionSuffixNodeAllowList.has(suffix.type)) {
file.message('List item description must end with proper punctuation', suffix);
return false;
}
// Ensure description ends with '.', '!', '?', '…' or an acceptable special-case
if (suffix.type === 'text' && !validateListItemSuffix(descriptionText, suffixText)) {
file.message('List item description must end with proper punctuation', suffix);
return false;
}
if (prefix === suffix) {
// Description contains pure text
if (!validateListItemPrefixCasing(prefix, file)) {
return false;
}
} else {
// Description contains mixed node types
for (const node of description) {
if (!listItemDescriptionNodeAllowList.has(node.type)) {
file.message('List item description contains invalid markdown', node);
return false;
}
}
if (prefix.length > 3 && !validateListItemPrefixCasing(prefix, file)) {
return false;
}
}
return true;
}
function validateListItemSpecialCases(description, descriptionText) {
if (descriptionText.startsWith(' - ')) {
return false;
}
const text = descriptionText
.replace(emojiRegex(), '')
.trim();
if (!text) {
// Description contains only emoji and spaces
return true;
}
if (/^\s\([^)]+\)\s*$/.test(descriptionText)) {
// Description contains only a parenthetical
return true;
}
if (/^\([^)]+\)$/.test(text)) {
// Description contains only a parenthetical and emojis
return true;
}
return false;
}
function tokenizeWords(text) {
return text.split(/[- ;./']/).filter(Boolean);
}
function validateListItemPrefixCasing(prefix, file) {
const strippedPrefix = prefix.value.slice(3);
const [firstWord] = tokenizeWords(strippedPrefix);
if (!firstWord) {
file.message('List item description must start with a non-empty string', prefix);
return false;
}
if (!listItemPrefixCaseAllowList.has(caseOf(firstWord.replaceAll(/\W+/g, ''))) && !/\d/.test(firstWord) && !/^["“'(]/.test(firstWord) && !identifierAllowList.has(firstWord)) {
file.message('List item description must start with valid casing', prefix);
return false;
}
return true;
}
function validateListItemPrefix(descriptionText, prefixText) {
if (prefixText.startsWith(' - ')) {
// Description starts with a dash
return true;
}
if (textEndsWithEmoji(prefixText) && descriptionText === prefixText) {
// Description ends with an emojii
return true;
}
return false;
}
function validateListItemSuffix(descriptionText, suffixText) {
// Punctuation rules are available at: https://www.thepunctuationguide.com
// Descriptions are not allowed to be fully backticked quotes, whatever the
// ending punctuation and its position.
if (/^`.*[.!?…]*`[.!?…]*$/.test(descriptionText)) {
// Still allow multiple backticks if the whole description is not fully
// quoted.
if (/^`.+`.+`.+$/.test(descriptionText)) {
return true;
}
return false;
}
// Any kind of quote followed by one of our punctuaction marker is perfect,
// but only if not following a punctuation itself. Uses positive lookbehind
// to search for punctuation following a quote.
if (/.*(?<=["”])[.!?…]+$/.test(descriptionText)) {
// If the quote follows a regular punctuation, this is wrong.
if (/.*[.!?…]["”][.!?…]+$/.test(descriptionText)) {
return false;
}
return true;
}
// Any of our punctuation marker eventually closed by any kind of quote is
// good.
if (/.*[.!?…]["”]?$/.test(descriptionText)) {
return true;
}
if (!/[.!?…]/.test(descriptionText)) {
// Description contains no punctuation
const tokens = tokenizeWords(descriptionText);
if (tokens.length > 2 || !textEndsWithEmoji(tokens.at(-1))) {
return false;
}
}
if (/\)\s*$/.test(suffixText)) {
// Description contains punctuation and ends with a parenthesis
return true;
}
if (textEndsWithEmoji(suffixText)) {
// Description contains punctuation and ends with an emoji
return true;
}
return false;
}
function textEndsWithEmoji(text) {
const regex = emojiRegex();
let match;
let emoji;
let emojiIndex;
// Find last emoji in text (if any exist)
while ((match = regex.exec(text))) {
const {index} = match;
emoji = match[0];
emojiIndex = index;
}
if (emoji && emoji.length + emojiIndex >= text.length) {
return true;
}
return false;
}
export default listItemRule;