-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
spell-check.js
57 lines (47 loc) · 1.34 KB
/
spell-check.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
'use strict';
const isUrl = require('is-url-superb');
const rule = require('unified-lint-rule');
const visit = require('unist-util-visit');
const arrify = require('arrify');
const spellCheckRules = require('../lib/spell-check-rules');
const wordBreakCharacterAllowList = new Set([
'-'
]);
module.exports = rule('remark-lint:awesome-spell-check', (ast, file) => {
visit(ast, 'text', node => {
if (!node.value) {
return;
}
const sanitizedSegments = [];
for (const segment of node.value.split(/\s/g)) {
if (!isUrl(segment)) {
sanitizedSegments.push(segment);
}
}
const sanitizedValue = sanitizedSegments.join(' ');
for (const rule of spellCheckRules) {
const {test, value} = rule;
const regs = arrify(test).map(reg => new RegExp(reg));
for (const re of regs) {
for (;;) {
const match = re.exec(sanitizedValue);
if (!match) {
break;
}
if (match[0] !== value) {
const previousCharacter = sanitizedValue[match.index - 1];
const nextCharacter = sanitizedValue[match.index + match[0].length];
if (wordBreakCharacterAllowList.has(previousCharacter)) {
continue;
}
if (wordBreakCharacterAllowList.has(nextCharacter)) {
continue;
}
file.message(`Text "${match[0]}" should be written as "${value}"`, node);
break;
}
}
}
}
});
});