From 4c97c9deccf9d0b293f564a738b07416fe96f065 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sat, 15 Oct 2022 18:28:09 +0200 Subject: [PATCH] #58 - PDF Indexing ok on mobile --- package.json | 3 ++- pnpm-lock.yaml | 6 ++++++ src/globals.ts | 3 --- src/main.ts | 9 +++------ src/pdf-manager.ts | 4 ++-- src/settings.ts | 28 +++++++++++++--------------- src/utils.ts | 8 +++++++- 7 files changed, 33 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 48cd43f..eca0516 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,8 @@ "lodash-es": "4.17.21", "minisearch": "5.0.0", "p-queue-compat": "1.0.187", - "pako": "^2.0.4" + "pako": "^2.0.4", + "pure-md5": "^0.1.14" }, "pnpm": { "overrides": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0623c12..27380d9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,7 @@ specifiers: pako: ^2.0.4 prettier: ^2.7.1 prettier-plugin-svelte: ^2.8.0 + pure-md5: ^0.1.14 rollup: ^2.79.1 rollup-plugin-base64: ^1.0.1 rollup-plugin-copy: ^3.4.0 @@ -44,6 +45,7 @@ dependencies: minisearch: 5.0.0 p-queue-compat: 1.0.187 pako: 2.0.4 + pure-md5: 0.1.14 devDependencies: '@babel/preset-env': 7.19.4 @@ -4066,6 +4068,10 @@ packages: engines: {node: '>=6'} dev: true + /pure-md5/0.1.14: + resolution: {integrity: sha512-tQ/inCmpbggbJRgJESzyhAj8weRANMMgYvBtVcqDHfTXR88fgU/Ff7YQHm8dGrcenCxkjas10SmbaFROcp2kZQ==} + dev: false + /querystringify/2.2.0: resolution: {integrity: sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==} dev: true diff --git a/src/globals.ts b/src/globals.ts index 953cc3b..0a19881 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -17,9 +17,6 @@ export const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/not export const pdfCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/pdfCache.data` export const historyFilePath = `${app.vault.configDir}/plugins/omnisearch/historyCache.json` -export const oldSearchIndexFilePath = `${app.vault.configDir}/plugins/omnisearch/searchIndex.json` -export const oldNnotesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json` - export const EventNames = { ToggleExcerpts: 'toggle-excerpts', } as const diff --git a/src/main.ts b/src/main.ts index f6def97..d068810 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,12 +2,7 @@ import { Notice, Plugin, TFile } from 'obsidian' import * as Search from './search' import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals' import { loadSettings, settings, SettingsTab, showExcerpt } from './settings' -import { - eventBus, - EventNames, - oldNnotesCacheFilePath, - oldSearchIndexFilePath, -} from './globals' +import { eventBus, EventNames } from './globals' import { registerAPI } from '@vanakat/plugin-api' import api from './api' import { loadSearchHistory } from './search-history' @@ -107,11 +102,13 @@ export default class OmnisearchPlugin extends Plugin { } async function cleanOldCacheFiles() { + const oldSearchIndexFilePath = `${app.vault.configDir}/plugins/omnisearch/searchIndex.json` if (await app.vault.adapter.exists(oldSearchIndexFilePath)) { try { await app.vault.adapter.remove(oldSearchIndexFilePath) } catch (e) {} } + const oldNnotesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json` if (await app.vault.adapter.exists(oldNnotesCacheFilePath)) { try { await app.vault.adapter.remove(oldNnotesCacheFilePath) diff --git a/src/pdf-manager.ts b/src/pdf-manager.ts index 6d93cce..b1f296e 100644 --- a/src/pdf-manager.ts +++ b/src/pdf-manager.ts @@ -3,7 +3,7 @@ import PQueue from 'p-queue-compat' import PDFWorker from 'web-worker:./pdf-worker.ts' import { pdfCacheFilePath } from './globals' import { deflate, inflate } from 'pako' -import { md5 } from './utils' +import { makeMD5 } from './utils' class PDFManager { private cache: Map = new Map() @@ -24,7 +24,7 @@ class PDFManager { public async getPdfText(file: TFile): Promise { const data = new Uint8Array(await app.vault.readBinary(file)) - const hash = md5(data) + const hash = makeMD5(data) if (this.cache.has(hash)) { return this.cache.get(hash)!.content } diff --git a/src/settings.ts b/src/settings.ts index 10addd2..5a25a5a 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -174,24 +174,22 @@ export class SettingsTab extends PluginSettingTab { ) // PDF Indexing - not available on mobile - if (!Platform.isMobileApp) { - const indexPDFsDesc = new DocumentFragment() - indexPDFsDesc.createSpan({}, span => { - span.innerHTML = `Omnisearch will include PDFs in search results. + const indexPDFsDesc = new DocumentFragment() + indexPDFsDesc.createSpan({}, span => { + span.innerHTML = `Omnisearch will include PDFs in search results. This feature is currently a work-in-progress, please report slowdowns or issues that you might experience.
Each PDF can take a few seconds to be indexed, so it may not appear immediately in search results.
Needs a restart to fully take effect.` - }) - new Setting(containerEl) - .setName('BETA - PDF Indexing') - .setDesc(indexPDFsDesc) - .addToggle(toggle => - toggle.setValue(settings.PDFIndexing).onChange(async v => { - settings.PDFIndexing = v - await saveSettings(this.plugin) - }) - ) - } + }) + new Setting(containerEl) + .setName('BETA - PDF Indexing') + .setDesc(indexPDFsDesc) + .addToggle(toggle => + toggle.setValue(settings.PDFIndexing).onChange(async v => { + settings.PDFIndexing = v + await saveSettings(this.plugin) + }) + ) // #endregion Behavior // #region User Interface diff --git a/src/utils.ts b/src/utils.ts index 42f68a0..6ae1658 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,6 +11,7 @@ import { } from './globals' import { settings } from './settings' import { createHash, type BinaryLike } from 'crypto' +import { md5 } from 'pure-md5' export function highlighter(str: string): string { return `${str}` @@ -192,6 +193,11 @@ export function getExtension(path: string): string { return split[split.length - 1] } -export function md5(data: BinaryLike): string { +export function makeMD5(data: BinaryLike): string { + if (Platform.isMobileApp) { + // A node-less implementation, but since we're not hashing the same data + // (arrayBuffer vs stringified array) the hash will be different + return md5(data.toString()) + } return createHash('md5').update(data).digest('hex') }