Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for newer dictionaries #27

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
*/

/**
* @callback Dictionary
* @typedef Dictionary
* A hunspell dictionary.
* @property {Uint8Array} aff
* Data for the affix file (defines the language, keyboard, flags, and more).
* @property {Uint8Array} dic
* Data for the dictionary file (contains words and flags applying to those words).
*
* @callback DictionaryCallback
* Dictionary function.
* @param {DictionaryOnLoad} onload
* Callback called when the dictionary is loaded.
Expand All @@ -18,7 +25,7 @@
* Callback called when the dictionary is loaded.
* @param {Error | undefined} error
* Error.
* @param {unknown} [result]
* @param {Dictionary} [result]
* Dictionary.
* @returns {undefined | void}
* Nothing.
Expand All @@ -27,8 +34,8 @@
*
* @typedef Options
* Configuration.
* @property {Dictionary} dictionary
* Dictionary function (required);
* @property {Dictionary | DictionaryCallback} dictionary
* Dictionary (required);
* result of importing one of the dictionaries in `wooorm/dictionaries`.
* @property {ReadonlyArray<string> | null | undefined} [ignore]
* List of words to ignore (optional).
Expand Down Expand Up @@ -80,15 +87,17 @@ const emptyIgnore = []
/**
* Check spelling.
*
* @param {Readonly<Options> | Dictionary} options
* @param {Readonly<Options> | DictionaryCallback | Dictionary} options
* Configuration or dictionary (required).
* @returns
* Transform.
*/
export default function retextSpell(options) {
const settings =
typeof options === 'function' ? {dictionary: options} : options || {}
const dictionary = settings.dictionary
typeof options === 'function' ||
(options && 'aff' in options && 'dic' in options)
? {dictionary: options}
: options || {}
const ignore = settings.ignore || emptyIgnore
const ignoreLiteral =
typeof settings.ignoreLiteral === 'boolean' ? settings.ignoreLiteral : true
Expand All @@ -102,7 +111,7 @@ export default function retextSpell(options) {
: true
const personal = settings.personal

if (typeof dictionary !== 'function') {
if (!settings.dictionary) {
throw new TypeError('Missing `dictionary` in options')
}

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

// Callback called when a `dictionary` is loaded or when `load`ing failed.
// Flushes the queue when available, and sets the results on the parent scope.
dictionary(onload)
if (typeof settings.dictionary === 'function') {
settings.dictionary(onload)
} else {
onload(undefined, settings.dictionary)
}

/**
* Transform.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/node": "^20.0.0",
"c8": "^8.0.0",
"dictionary-en": "^3.0.0",
"dictionary-en-v4": "npm:dictionary-en@^4.0.0",
"prettier": "^3.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import dictionaryEn from 'dictionary-en'
import dictionaryEnV4 from 'dictionary-en-v4'
import {retext} from 'retext'
import retextEmoji from 'retext-emoji'
import retextSpell from 'retext-spell'
Expand Down Expand Up @@ -78,6 +79,16 @@ test('retextSpell', async function (t) {
])
})

await t.test('should work with newer dictionary versions', async function () {
const file = await retext()
.use(retextSpell, dictionaryEnV4)
.process('kolor')

assert.deepEqual(file.messages.map(String), [
'1:1-1:6: Unexpected unknown word `kolor`, expected for example `color`, `dolor`'
])
})

await t.test('should work w/ repeated misspellings', async function () {
const file = await retext()
.use(retextSpell, dictionaryEn)
Expand Down
Loading