-
Notifications
You must be signed in to change notification settings - Fork 5
/
alphabetical-list-items.js
38 lines (33 loc) · 1.13 KB
/
alphabetical-list-items.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
const rule = require('unified-lint-rule');
const visit = require('unist-util-visit');
const toString = require('mdast-util-to-string');
function normalize(text) {
const removeAtBeginning = /^([-._(《"'])*/;
const removeInside = /[,:]/;
const replaceWithSpace = /-/;
return text.toLowerCase()
.trim()
.replace(removeAtBeginning, '')
.replace(removeInside, '')
.replace(replaceWithSpace, ' ');
}
function alphaCheck(tree, file, language = 'en-US') {
visit(tree, 'list', (node) => {
const items = node.children;
let lastLine = -1;
let lastText = '';
items.forEach((item) => {
if (item.children.length) {
const text = normalize(toString(item.children[0].children[0]));
const line = item.position.start.line;
const comp = new Intl.Collator(language).compare(lastText, text);
if (comp > 0) {
file.message(`Alphabetical ordering: swap l.${item.children[0].children[0].position.start.line} and l.${lastLine}`, node);
}
lastLine = line;
lastText = text;
}
});
});
}
module.exports = rule('remark-lint:alphabetize-lists', alphaCheck);