Skip to content

Commit 03f0ad1

Browse files
authored
Add support for newer dictionaries
Closes GH-26. Closes GH-27. Reviewed-by: Titus Wormer <[email protected]>
1 parent bea2f5c commit 03f0ad1

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

lib/index.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
*/
66

77
/**
8-
* @callback Dictionary
8+
* @typedef Dictionary
9+
* A hunspell dictionary.
10+
* @property {Uint8Array} aff
11+
* Data for the affix file (defines the language, keyboard, flags, and more).
12+
* @property {Uint8Array} dic
13+
* Data for the dictionary file (contains words and flags applying to those words).
14+
*
15+
* @callback DictionaryCallback
916
* Dictionary function.
1017
* @param {DictionaryOnLoad} onload
1118
* Callback called when the dictionary is loaded.
@@ -18,7 +25,7 @@
1825
* Callback called when the dictionary is loaded.
1926
* @param {Error | undefined} error
2027
* Error.
21-
* @param {unknown} [result]
28+
* @param {Dictionary} [result]
2229
* Dictionary.
2330
* @returns {undefined | void}
2431
* Nothing.
@@ -27,8 +34,8 @@
2734
*
2835
* @typedef Options
2936
* Configuration.
30-
* @property {Dictionary} dictionary
31-
* Dictionary function (required);
37+
* @property {Dictionary | DictionaryCallback} dictionary
38+
* Dictionary (required);
3239
* result of importing one of the dictionaries in `wooorm/dictionaries`.
3340
* @property {ReadonlyArray<string> | null | undefined} [ignore]
3441
* List of words to ignore (optional).
@@ -80,15 +87,17 @@ const emptyIgnore = []
8087
/**
8188
* Check spelling.
8289
*
83-
* @param {Readonly<Options> | Dictionary} options
90+
* @param {Readonly<Options> | DictionaryCallback | Dictionary} options
8491
* Configuration or dictionary (required).
8592
* @returns
8693
* Transform.
8794
*/
8895
export default function retextSpell(options) {
8996
const settings =
90-
typeof options === 'function' ? {dictionary: options} : options || {}
91-
const dictionary = settings.dictionary
97+
typeof options === 'function' ||
98+
(options && 'aff' in options && 'dic' in options)
99+
? {dictionary: options}
100+
: options || {}
92101
const ignore = settings.ignore || emptyIgnore
93102
const ignoreLiteral =
94103
typeof settings.ignoreLiteral === 'boolean' ? settings.ignoreLiteral : true
@@ -102,7 +111,7 @@ export default function retextSpell(options) {
102111
: true
103112
const personal = settings.personal
104113

105-
if (typeof dictionary !== 'function') {
114+
if (!settings.dictionary) {
106115
throw new TypeError('Missing `dictionary` in options')
107116
}
108117

@@ -125,7 +134,11 @@ export default function retextSpell(options) {
125134

126135
// Callback called when a `dictionary` is loaded or when `load`ing failed.
127136
// Flushes the queue when available, and sets the results on the parent scope.
128-
dictionary(onload)
137+
if (typeof settings.dictionary === 'function') {
138+
settings.dictionary(onload)
139+
} else {
140+
onload(undefined, settings.dictionary)
141+
}
129142

130143
/**
131144
* Transform.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@types/node": "^20.0.0",
5151
"c8": "^8.0.0",
5252
"dictionary-en": "^3.0.0",
53+
"dictionary-en-v4": "npm:dictionary-en@^4.0.0",
5354
"prettier": "^3.0.0",
5455
"remark-cli": "^11.0.0",
5556
"remark-preset-wooorm": "^9.0.0",

test.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'node:assert/strict'
22
import test from 'node:test'
33
import dictionaryEn from 'dictionary-en'
4+
import dictionaryEnV4 from 'dictionary-en-v4'
45
import {retext} from 'retext'
56
import retextEmoji from 'retext-emoji'
67
import retextSpell from 'retext-spell'
@@ -78,6 +79,16 @@ test('retextSpell', async function (t) {
7879
])
7980
})
8081

82+
await t.test('should work with newer dictionary versions', async function () {
83+
const file = await retext()
84+
.use(retextSpell, dictionaryEnV4)
85+
.process('kolor')
86+
87+
assert.deepEqual(file.messages.map(String), [
88+
'1:1-1:6: Unexpected unknown word `kolor`, expected for example `color`, `dolor`'
89+
])
90+
})
91+
8192
await t.test('should work w/ repeated misspellings', async function () {
8293
const file = await retext()
8394
.use(retextSpell, dictionaryEn)

0 commit comments

Comments
 (0)