@@ -14,10 +14,10 @@ import type { ocrLangs } from './ocr-langs'
14
14
* Concatenates an array of langs to a single string to be passed to Tesseract
15
15
* e.g. ['fra', 'eng'] => 'eng+fra'
16
16
* The langs are sorted alphabetically because it's also used a cache key
17
- * @param langs
18
- * @returns
17
+ * @param langs
18
+ * @returns
19
19
*/
20
- function concatLangs ( langs : Array < typeof ocrLangs [ number ] > ) : string {
20
+ function concatLangs ( langs : Array < ( typeof ocrLangs ) [ number ] > ) : string {
21
21
return langs . sort ( ) . join ( '+' )
22
22
}
23
23
@@ -102,7 +102,11 @@ class OCRManager {
102
102
*/
103
103
public async getImageText ( file : TFile , options : OcrOptions ) : Promise < string > {
104
104
try {
105
- return await imagesProcessQueue . add ( ( ) => this . #getImageText( file , options ) ) ?? ''
105
+ return (
106
+ ( await imagesProcessQueue . add ( ( ) =>
107
+ this . #getImageText( file , options )
108
+ ) ) ?? ''
109
+ )
106
110
} catch ( e ) {
107
111
console . warn (
108
112
`Text Extractor - Error while extracting text from ${ file . basename } `
@@ -113,9 +117,8 @@ class OCRManager {
113
117
}
114
118
115
119
async #getImageText( file : TFile , options : OcrOptions ) : Promise < string > {
116
- const langs = concatLangs ( options . langs )
117
120
// Get the text from the cache if it exists
118
- const cache = await readCache ( file , langs )
121
+ const cache = await readCache ( file )
119
122
if ( cache ) {
120
123
return cache . text ?? FAILED_TO_EXTRACT
121
124
}
@@ -128,6 +131,7 @@ class OCRManager {
128
131
const cachePath = getCachePath ( file )
129
132
const data = new Uint8ClampedArray ( await app . vault . readBinary ( file ) )
130
133
const worker = OCRWorker . getWorker ( )
134
+ const langs = concatLangs ( options . langs )
131
135
132
136
return new Promise ( async ( resolve , reject ) => {
133
137
try {
@@ -144,12 +148,24 @@ class OCRManager {
144
148
. trim ( )
145
149
146
150
// Add it to the cache
147
- await writeCache ( cachePath . folder , cachePath . filename , text , file . path , langs )
151
+ await writeCache (
152
+ cachePath . folder ,
153
+ cachePath . filename ,
154
+ text ,
155
+ file . path ,
156
+ langs
157
+ )
148
158
resolve ( text )
149
159
} catch ( e ) {
150
160
// In case of error (unreadable PDF or timeout) just add
151
161
// an empty string to the cache
152
- await writeCache ( cachePath . folder , cachePath . filename , '' , file . path , langs )
162
+ await writeCache (
163
+ cachePath . folder ,
164
+ cachePath . filename ,
165
+ '' ,
166
+ file . path ,
167
+ langs
168
+ )
153
169
resolve ( '' )
154
170
}
155
171
} )
0 commit comments