/
cspell.config.mjs
69 lines (61 loc) · 1.64 KB
/
cspell.config.mjs
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
// Kindly provided by @Jason3S
//
// https://github.com/streetsidesoftware/cspell/discussions/5118#discussioncomment-7972837
import { promises as fs } from 'fs';
function expand(pattern, options = { begin: '(', end: ')', sep: '|' }, start = 0) {
const len = pattern.length;
const parts = [];
function push(word) {
if (Array.isArray(word)) {
parts.push(...word);
} else {
parts.push(word);
}
}
let i = start;
let curWord = '';
while (i < len) {
const ch = pattern[i++];
if (ch === options.end) {
break;
}
if (ch === options.begin) {
const nested = expand(pattern, options, i);
i = nested.idx;
curWord = nested.parts.flatMap((p) => (Array.isArray(curWord) ? curWord.map((w) => w + p) : [curWord + p]));
continue;
}
if (ch === options.sep) {
push(curWord);
curWord = '';
continue;
}
curWord = Array.isArray(curWord) ? curWord.map((w) => w + ch) : curWord + ch;
}
push(curWord);
return { parts, idx: i };
}
function expandWord(pattern, options = { begin: '(', end: ')', sep: '|' }) {
return expand(pattern, options).parts;
}
function expandWords(wordList) {
const words = wordList
.split(/\n/g)
.map((w) => w.trim())
.filter((w) => !!w && !w.startsWith('#'))
.flatMap((w) => expandWord(w));
return words;
}
const wordsFile = new URL('words.txt', import.meta.url);
const wordList = await fs.readFile(wordsFile, 'utf8');
const config = {
id: 'example-config',
dictionaryDefinitions: [
{
name: 'custom-words',
words: expandWords(wordList),
},
],
dictionaries: ['custom-words'],
};
export default config;