forked from sindresorhus/awesome-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell-check.js
58 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
58
import isUrl from 'is-url-superb';
import {lintRule} from 'unified-lint-rule';
import {visit} from 'unist-util-visit';
import arrify from 'arrify';
import spellCheckRules from '../lib/spell-check-rules.js';
const wordBreakCharacterAllowList = new Set([
'-',
]);
const spellCheckRule = lintRule('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 previousChar = sanitizedValue[match.index - 1];
const nextChar = sanitizedValue[match.index + match[0].length];
if (wordBreakCharacterAllowList.has(previousChar)) {
continue;
}
if (wordBreakCharacterAllowList.has(nextChar)) {
continue;
}
file.message(`Text "${match[0]}" should be written as "${value}"`, node);
break;
}
}
}
}
});
});
export default spellCheckRule;