OCR bases
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
|||||||
extractHeadingsFromCache,
|
extractHeadingsFromCache,
|
||||||
getAliasesFromMetadata,
|
getAliasesFromMetadata,
|
||||||
getTagsFromMetadata,
|
getTagsFromMetadata,
|
||||||
|
isFileImage,
|
||||||
isFilePlaintext,
|
isFilePlaintext,
|
||||||
removeDiacritics,
|
removeDiacritics,
|
||||||
} from './tools/utils'
|
} from './tools/utils'
|
||||||
@@ -10,7 +11,7 @@ import * as NotesIndex from './notes-index'
|
|||||||
import type { TFile } from 'obsidian'
|
import type { TFile } from 'obsidian'
|
||||||
import type { IndexedDocument } from './globals'
|
import type { IndexedDocument } from './globals'
|
||||||
import { getNonExistingNotes } from './tools/notes'
|
import { getNonExistingNotes } from './tools/notes'
|
||||||
import { getPdfText } from 'obsidian-text-extract'
|
import { getImageText, getPdfText } from 'obsidian-text-extract'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all plaintext files as IndexedDocuments
|
* Return all plaintext files as IndexedDocuments
|
||||||
@@ -48,6 +49,35 @@ export async function getPDFFiles(): Promise<IndexedDocument[]> {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all Image files as IndexedDocuments.
|
||||||
|
* If a PDF isn't cached, it will be read from the disk and added to the IndexedDB
|
||||||
|
*/
|
||||||
|
export async function getImageFiles(): Promise<IndexedDocument[]> {
|
||||||
|
const allFiles = app.vault
|
||||||
|
.getFiles()
|
||||||
|
.filter(
|
||||||
|
f =>
|
||||||
|
f.path.endsWith('.png') ||
|
||||||
|
f.path.endsWith('.jpg') ||
|
||||||
|
f.path.endsWith('.jpeg')
|
||||||
|
)
|
||||||
|
const data: IndexedDocument[] = []
|
||||||
|
|
||||||
|
const input = []
|
||||||
|
for (const file of allFiles) {
|
||||||
|
input.push(
|
||||||
|
NotesIndex.processQueue(async () => {
|
||||||
|
const doc = await fileToIndexedDocument(file)
|
||||||
|
await cacheManager.updateDocument(file.path, doc)
|
||||||
|
data.push(doc)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
await Promise.all(input)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a file into an IndexedDocument.
|
* Convert a file into an IndexedDocument.
|
||||||
* Will use the cache if possible.
|
* Will use the cache if possible.
|
||||||
@@ -61,6 +91,8 @@ export async function fileToIndexedDocument(
|
|||||||
content = removeDiacritics(await app.vault.cachedRead(file))
|
content = removeDiacritics(await app.vault.cachedRead(file))
|
||||||
} else if (file.path.endsWith('.pdf')) {
|
} else if (file.path.endsWith('.pdf')) {
|
||||||
content = removeDiacritics(await getPdfText(file))
|
content = removeDiacritics(await getPdfText(file))
|
||||||
|
} else if (isFileImage(file.path)) {
|
||||||
|
content = removeDiacritics(await getImageText(file))
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid file: ' + file.path)
|
throw new Error('Invalid file: ' + file.path)
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/main.ts
12
src/main.ts
@@ -1,5 +1,5 @@
|
|||||||
import { Notice, Plugin, TFile } from 'obsidian'
|
import { Notice, Plugin, TFile } from 'obsidian'
|
||||||
import {SearchEngine} from './search/search-engine'
|
import { SearchEngine } from './search/search-engine'
|
||||||
import {
|
import {
|
||||||
OmnisearchInFileModal,
|
OmnisearchInFileModal,
|
||||||
OmnisearchVaultModal,
|
OmnisearchVaultModal,
|
||||||
@@ -128,6 +128,16 @@ async function populateIndex(): Promise<void> {
|
|||||||
console.timeEnd('Omnisearch - Timing')
|
console.timeEnd('Omnisearch - Timing')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load Images
|
||||||
|
// if (settings.PDFIndexing) {
|
||||||
|
console.time('Omnisearch - Timing')
|
||||||
|
const images = await FileLoader.getImageFiles()
|
||||||
|
// Index them
|
||||||
|
await tmpEngine.addAllToMinisearch(images)
|
||||||
|
console.log(`Omnisearch - Indexed ${images.length} Images`)
|
||||||
|
console.timeEnd('Omnisearch - Timing')
|
||||||
|
// }
|
||||||
|
|
||||||
// Load PDFs into the main search engine, and write cache
|
// Load PDFs into the main search engine, and write cache
|
||||||
SearchEngine.loadTmpDataIntoMain()
|
SearchEngine.loadTmpDataIntoMain()
|
||||||
SearchEngine.isIndexing.set(false)
|
SearchEngine.isIndexing.set(false)
|
||||||
|
|||||||
@@ -188,6 +188,12 @@ export function isFileIndexable(path: string): boolean {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isFileImage(path: string): boolean {
|
||||||
|
return (
|
||||||
|
path.endsWith('.png') || path.endsWith('.jpg') || path.endsWith('.jpeg')
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export function isFilePlaintext(path: string): boolean {
|
export function isFilePlaintext(path: string): boolean {
|
||||||
return getPlaintextExtensions().some(t => path.endsWith(`.${t}`))
|
return getPlaintextExtensions().some(t => path.endsWith(`.${t}`))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user