Skip to content

Commit 816eda6

Browse files
committed
Quick fix for #1
1 parent 65d3a5e commit 816eda6

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/src/ocr/ocr-manager.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ import {
88
workerTimeout,
99
} from '../globals'
1010
import type { OcrOptions } from '../types'
11+
import type { ocrLangs } from './ocr-langs'
12+
13+
/**
14+
* Concatenates an array of langs to a single string to be passed to Tesseract
15+
* e.g. ['fra', 'eng'] => 'eng+fra'
16+
* The langs are sorted alphabetically because it's also used a cache key
17+
* @param langs
18+
* @returns
19+
*/
20+
function concatLangs (langs: Array<typeof ocrLangs[number]>): string {
21+
return langs.sort().join('+')
22+
}
1123

1224
class OCRWorker {
1325
static #pool: OCRWorker[] = []
@@ -51,12 +63,12 @@ class OCRWorker {
5163
}): Promise<{ text: string; langs: string }> {
5264
return new Promise(async (resolve, reject) => {
5365
this.#running = true
54-
const langs = msg.options.langs.join('+')
66+
const langs = concatLangs(msg.options.langs)
5567

5668
if (!this.#ready) {
5769
await this.worker.load()
5870
await this.worker.loadLanguage(langs)
59-
await this.worker.initialize(msg.options.langs[0])
71+
await this.worker.initialize(langs)
6072
this.#ready = true
6173
}
6274

@@ -101,9 +113,9 @@ class OCRManager {
101113
}
102114

103115
async #getImageText(file: TFile, options: OcrOptions): Promise<string> {
104-
const optLangs = options.langs.sort().join('+')
116+
const langs = concatLangs(options.langs)
105117
// Get the text from the cache if it exists
106-
const cache = await readCache(file, optLangs)
118+
const cache = await readCache(file, langs)
107119
if (cache) {
108120
return cache.text ?? FAILED_TO_EXTRACT
109121
}
@@ -132,12 +144,12 @@ class OCRManager {
132144
.trim()
133145

134146
// Add it to the cache
135-
await writeCache(cachePath.folder, cachePath.filename, text, optLangs)
147+
await writeCache(cachePath.folder, cachePath.filename, text, langs)
136148
resolve(text)
137149
} catch (e) {
138150
// In case of error (unreadable PDF or timeout) just add
139151
// an empty string to the cache
140-
await writeCache(cachePath.folder, cachePath.filename, '', optLangs)
152+
await writeCache(cachePath.folder, cachePath.filename, '', langs)
141153
resolve('')
142154
}
143155
})

0 commit comments

Comments
 (0)