#106 - Basic, English-only OCR
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
$: {
|
||||
imagePath = null
|
||||
if (isFileImage(note.path)) {
|
||||
console.log(note.path)
|
||||
// @ts-ignore
|
||||
const file = app.vault.getFiles().find(f => f.path === note.path)
|
||||
if (file) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
extractHeadingsFromCache,
|
||||
getAliasesFromMetadata,
|
||||
getTagsFromMetadata,
|
||||
isFileImage,
|
||||
isFilePlaintext,
|
||||
removeDiacritics,
|
||||
} from './tools/utils'
|
||||
@@ -10,7 +11,7 @@ import * as NotesIndex from './notes-index'
|
||||
import type { TFile } from 'obsidian'
|
||||
import type { IndexedDocument } from './globals'
|
||||
import { getNonExistingNotes } from './tools/notes'
|
||||
import { getPdfText } from 'obsidian-text-extract'
|
||||
import { getPdfText, getImageText } from 'obsidian-text-extract'
|
||||
|
||||
/**
|
||||
* Return all plaintext files as IndexedDocuments
|
||||
@@ -27,20 +28,33 @@ export async function getPlainTextFiles(): Promise<IndexedDocument[]> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all PDF files as IndexedDocuments.
|
||||
* If a PDF isn't cached, it will be read from the disk and added to the IndexedDB
|
||||
* Return all PDFs as IndexedDocuments.
|
||||
*/
|
||||
export async function getPDFFiles(): Promise<IndexedDocument[]> {
|
||||
export async function getPDFAsDocuments(): Promise<IndexedDocument[]> {
|
||||
const files = app.vault.getFiles().filter(f => f.path.endsWith('.pdf'))
|
||||
return await getBinaryFiles(files)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all imageas as IndexedDocuments.
|
||||
*/
|
||||
export async function getImagesAsDocuments(): Promise<IndexedDocument[]> {
|
||||
const files = app.vault.getFiles().filter(f => isFileImage(f.path))
|
||||
return await getBinaryFiles(files)
|
||||
}
|
||||
|
||||
async function getBinaryFiles(files: TFile[]): Promise<IndexedDocument[]> {
|
||||
const data: IndexedDocument[] = []
|
||||
const input = []
|
||||
for (const file of files) {
|
||||
input.push(new Promise(async (resolve, reject) => {
|
||||
const doc = await fileToIndexedDocument(file)
|
||||
await cacheManager.updateLiveDocument(file.path, doc)
|
||||
data.push(doc)
|
||||
return resolve(null)
|
||||
}))
|
||||
input.push(
|
||||
new Promise(async (resolve, reject) => {
|
||||
const doc = await fileToIndexedDocument(file)
|
||||
await cacheManager.updateLiveDocument(file.path, doc)
|
||||
data.push(doc)
|
||||
return resolve(null)
|
||||
})
|
||||
)
|
||||
}
|
||||
await Promise.all(input)
|
||||
return data
|
||||
@@ -51,13 +65,15 @@ export async function getPDFFiles(): Promise<IndexedDocument[]> {
|
||||
* Will use the cache if possible.
|
||||
*/
|
||||
export async function fileToIndexedDocument(
|
||||
file: TFile,
|
||||
file: TFile
|
||||
): Promise<IndexedDocument> {
|
||||
let content: string
|
||||
if (isFilePlaintext(file.path)) {
|
||||
content = await app.vault.cachedRead(file)
|
||||
} else if (file.path.endsWith('.pdf')) {
|
||||
content = await getPdfText(file)
|
||||
} else if (isFileImage(file.path)) {
|
||||
content = await getImageText(file)
|
||||
} else {
|
||||
throw new Error('Invalid file: ' + file.path)
|
||||
}
|
||||
|
||||
50
src/main.ts
50
src/main.ts
@@ -84,9 +84,7 @@ export default class OmnisearchPlugin extends Plugin {
|
||||
showWelcomeNotice(this)
|
||||
}
|
||||
|
||||
onunload(): void {
|
||||
|
||||
}
|
||||
onunload(): void {}
|
||||
|
||||
addRibbonButton(): void {
|
||||
this.ribbonButton = this.addRibbonIcon('search', 'Omnisearch', _evt => {
|
||||
@@ -110,13 +108,13 @@ async function populateIndex(): Promise<void> {
|
||||
// Initialize minisearch
|
||||
let engine = SearchEngine.getEngine()
|
||||
|
||||
// No cache for iOS
|
||||
// if not iOS, load data from cache
|
||||
if (!Platform.isIosApp) {
|
||||
engine = await SearchEngine.initFromCache()
|
||||
}
|
||||
|
||||
// Load plaintext files
|
||||
console.log('Omnisearch - Fetching notes')
|
||||
console.log('Omnisearch - Reading notes')
|
||||
const plainTextFiles = await FileLoader.getPlainTextFiles()
|
||||
let allFiles = [...plainTextFiles]
|
||||
// iOS: since there's no cache, directly index the documents
|
||||
@@ -127,15 +125,28 @@ async function populateIndex(): Promise<void> {
|
||||
|
||||
// Load PDFs
|
||||
if (settings.PDFIndexing) {
|
||||
console.log('Omnisearch - Fetching PDFs')
|
||||
const pdfs = await FileLoader.getPDFFiles()
|
||||
console.log('Omnisearch - Reading PDFs')
|
||||
const pdfDocuments = await FileLoader.getPDFAsDocuments()
|
||||
// iOS: since there's no cache, just index the documents
|
||||
if (Platform.isIosApp) {
|
||||
await wait(1000)
|
||||
await engine.addAllToMinisearch(pdfs)
|
||||
await engine.addAllToMinisearch(pdfDocuments)
|
||||
}
|
||||
// Add PDFs to the files list
|
||||
allFiles = [...allFiles, ...pdfs]
|
||||
allFiles = [...allFiles, ...pdfDocuments]
|
||||
}
|
||||
|
||||
// Load Images
|
||||
if (settings.imagesIndexing) {
|
||||
console.log('Omnisearch - Reading Images')
|
||||
const imagesDocuments = await FileLoader.getImagesAsDocuments()
|
||||
// iOS: since there's no cache, just index the documents
|
||||
if (Platform.isIosApp) {
|
||||
await wait(1000)
|
||||
await engine.addAllToMinisearch(imagesDocuments)
|
||||
}
|
||||
// Add Images to the files list
|
||||
allFiles = [...allFiles, ...imagesDocuments]
|
||||
}
|
||||
|
||||
console.log('Omnisearch - Total number of files: ' + allFiles.length)
|
||||
@@ -146,6 +157,7 @@ async function populateIndex(): Promise<void> {
|
||||
console.log('Omnisearch - Checking index cache diff...')
|
||||
// Check which documents need to be removed/added/updated
|
||||
const diffDocs = await cacheManager.getDiffDocuments(allFiles)
|
||||
console.log(`Omnisearch - Files to add/remove/update: ${diffDocs.toAdd.length}/${diffDocs.toDelete.length}/${diffDocs.toUpdate.length}`)
|
||||
needToUpdateCache = !!(
|
||||
diffDocs.toAdd.length ||
|
||||
diffDocs.toDelete.length ||
|
||||
@@ -154,23 +166,19 @@ async function populateIndex(): Promise<void> {
|
||||
|
||||
// Add
|
||||
await engine.addAllToMinisearch(diffDocs.toAdd)
|
||||
console.log(`Omnisearch - ${diffDocs.toAdd.length} files to add`)
|
||||
diffDocs.toAdd.forEach(doc =>
|
||||
cacheManager.updateLiveDocument(doc.path, doc)
|
||||
)
|
||||
|
||||
// Delete
|
||||
console.log(`Omnisearch - ${diffDocs.toDelete.length} files to remove`)
|
||||
diffDocs.toDelete.forEach(d => engine.removeFromMinisearch(d))
|
||||
diffDocs.toDelete.forEach(doc => cacheManager.deleteLiveDocument(doc.path))
|
||||
|
||||
// Update (delete + add)
|
||||
console.log(`Omnisearch - ${diffDocs.toUpdate.length} files to update`)
|
||||
diffDocs.toUpdate
|
||||
.forEach(({ oldDoc, newDoc }) => {
|
||||
engine.removeFromMinisearch(oldDoc)
|
||||
cacheManager.updateLiveDocument(oldDoc.path, newDoc)
|
||||
})
|
||||
diffDocs.toUpdate.forEach(({ oldDoc, newDoc }) => {
|
||||
engine.removeFromMinisearch(oldDoc)
|
||||
cacheManager.updateLiveDocument(oldDoc.path, newDoc)
|
||||
})
|
||||
await engine.addAllToMinisearch(diffDocs.toUpdate.map(d => d.newDoc))
|
||||
}
|
||||
|
||||
@@ -205,13 +213,13 @@ async function cleanOldCacheFiles() {
|
||||
}
|
||||
|
||||
function showWelcomeNotice(plugin: Plugin) {
|
||||
const code = '1.7.6'
|
||||
const code = '1.8.0-beta.3'
|
||||
if (settings.welcomeMessage !== code) {
|
||||
const welcome = new DocumentFragment()
|
||||
welcome.createSpan({}, span => {
|
||||
span.innerHTML = `<strong>Omnisearch has been updated</strong>
|
||||
New beta feature: PDF search 🔎📄
|
||||
<small>Toggle "<i>BETA - Index PDFs</i>" in Omnisearch settings page.</small>`
|
||||
span.innerHTML = `<strong>Omnisearch BETA has been updated</strong>
|
||||
You can now enable "Images Indexing" to use Optical Character Recognition on your scanned documents
|
||||
🔎🖼`
|
||||
})
|
||||
new Notice(welcome, 30000)
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ export interface OmnisearchSettings extends WeightingSettings {
|
||||
indexedFileTypes: string[]
|
||||
/** Enable PDF indexing */
|
||||
PDFIndexing: boolean
|
||||
/** Enable PDF indexing */
|
||||
imagesIndexing: boolean
|
||||
/** Display Omnisearch popup notices over Obsidian */
|
||||
showIndexingNotices: boolean
|
||||
/** Activate the small 🔍 button on Obsidian's ribbon */
|
||||
@@ -147,7 +149,7 @@ export class SettingsTab extends PluginSettingTab {
|
||||
indexPDFsDesc.createSpan({}, span => {
|
||||
span.innerHTML = `Omnisearch will include PDFs in search results.
|
||||
<ul>
|
||||
<li>⚠️ Depending on their size, PDFs can take anywhere from a few seconds to 2 minutes to be processed.</li>
|
||||
<li>⚠️ Each PDF can take anywhere from a few seconds to 2 minutes to be processed.</li>
|
||||
<li>⚠️ Texts extracted from PDFs may contain errors such as missing spaces, or spaces in the middle of words.</li>
|
||||
<li>⚠️ Some PDFs can't be processed correctly and will return an empty text.</li>
|
||||
<li>This feature is currently a work-in-progress, please report issues that you might experience.</li>
|
||||
@@ -164,6 +166,26 @@ export class SettingsTab extends PluginSettingTab {
|
||||
})
|
||||
)
|
||||
|
||||
// PDF Indexing
|
||||
const indexImagesDesc = new DocumentFragment()
|
||||
indexImagesDesc.createSpan({}, span => {
|
||||
span.innerHTML = `Omnisearch will use <a href="https://en.wikipedia.org/wiki/Tesseract_(software)">Tesseract</a> to index images from their text.
|
||||
<ul>
|
||||
<li>Only English is supported at the moment.</li>
|
||||
<li>Not all images can be correctly read by the OCR, this feature works best with scanned documents.</li>
|
||||
</ul>
|
||||
<strong style="color: var(--text-accent)">Needs a restart to fully take effect.</strong>`
|
||||
})
|
||||
new Setting(containerEl)
|
||||
.setName('BETA - Images Indexing')
|
||||
.setDesc(indexImagesDesc)
|
||||
.addToggle(toggle =>
|
||||
toggle.setValue(settings.imagesIndexing).onChange(async v => {
|
||||
settings.imagesIndexing = v
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
|
||||
// #endregion Behavior
|
||||
|
||||
// #region User Interface
|
||||
@@ -315,6 +337,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
|
||||
ignoreDiacritics: true,
|
||||
indexedFileTypes: [] as string[],
|
||||
PDFIndexing: false,
|
||||
imagesIndexing: false,
|
||||
|
||||
showIndexingNotices: false,
|
||||
showShortName: false,
|
||||
|
||||
@@ -184,7 +184,9 @@ export function getCtrlKeyLabel(): 'ctrl' | '⌘' {
|
||||
|
||||
export function isFileIndexable(path: string): boolean {
|
||||
return (
|
||||
(settings.PDFIndexing && path.endsWith('.pdf')) || isFilePlaintext(path)
|
||||
(settings.PDFIndexing && path.endsWith('.pdf')) ||
|
||||
isFilePlaintext(path) ||
|
||||
(settings.imagesIndexing && isFileImage(path))
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user