From 0b47e23408b1a203f3fd1281f9ca422db7ea3342 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Thu, 14 Jul 2022 20:59:16 +0200 Subject: [PATCH] #83 --- src/globals.ts | 3 +++ src/notes.ts | 24 ++++++++++++++---------- src/search.ts | 2 +- src/settings.ts | 28 ++++++++++++++++++++-------- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/globals.ts b/src/globals.ts index 772a6be..cab3c0d 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -14,6 +14,9 @@ export const highlightClass = 'suggestion-highlight omnisearch-highlight' export const eventBus = new EventBus() +export const searchIndexFilePath = `${app.vault.configDir}/plugins/omnisearch/searchIndex.json` +export const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json` + export type SearchNote = { path: string basename: string diff --git a/src/notes.ts b/src/notes.ts index f6a5db0..4d56e08 100644 --- a/src/notes.ts +++ b/src/notes.ts @@ -1,10 +1,9 @@ +import { MarkdownView, TFile, type CachedMetadata } from 'obsidian' import { - MarkdownView, - TFile, - WorkspaceLeaf, - type CachedMetadata, -} from 'obsidian' -import type { IndexedNote, ResultNote } from './globals' + notesCacheFilePath, + type IndexedNote, + type ResultNote, +} from './globals' import { stringsToRegex } from './utils' import { settings } from './settings' @@ -15,8 +14,6 @@ import { settings } from './settings' */ export let notesCache: Record = {} -const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json` - export function resetNotesCache(): void { notesCache = {} } @@ -67,7 +64,11 @@ export async function openNote( let alreadyOpenAndPinned = false app.workspace.iterateAllLeaves(leaf => { if (leaf.view instanceof MarkdownView) { - if (!newPane && leaf.getViewState().state?.file === item.path && leaf.getViewState()?.pinned) { + if ( + !newPane && + leaf.getViewState().state?.file === item.path && + leaf.getViewState()?.pinned + ) { app.workspace.setActiveLeaf(leaf, false, true) alreadyOpenAndPinned = true } @@ -95,7 +96,10 @@ export async function openNote( export async function createNote(name: string): Promise { try { - const file = await app.vault.create(`${app.vault.getConfig('newFileFolderPath')}/${name}.md`, '') + const file = await app.vault.create( + `${app.vault.getConfig('newFileFolderPath')}/${name}.md`, + '', + ) await app.workspace.openLinkText(file.path, '') const view = app.workspace.getActiveViewOfType(MarkdownView) if (!view) { diff --git a/src/search.ts b/src/search.ts index 1f978c3..0985479 100644 --- a/src/search.ts +++ b/src/search.ts @@ -2,6 +2,7 @@ import { Notice, TAbstractFile, TFile } from 'obsidian' import MiniSearch, { type Options, type SearchResult } from 'minisearch' import { chsRegex, + searchIndexFilePath, SPACE_OR_PUNCTUATION, type IndexedNote, type ResultNote, @@ -33,7 +34,6 @@ import { let minisearchInstance: MiniSearch let isIndexChanged: boolean -const searchIndexFilePath = `${app.vault.configDir}/plugins/omnisearch/searchIndex.json` const tokenize = (text: string): string[] => { const tokens = text.split(SPACE_OR_PUNCTUATION) diff --git a/src/settings.ts b/src/settings.ts index 8db26be..0bdfee7 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,4 +1,5 @@ import { Plugin, PluginSettingTab, Setting, SliderComponent } from 'obsidian' +import { notesCacheFilePath, searchIndexFilePath } from './globals' import type OmnisearchPlugin from './main' interface WeightingSettings { @@ -51,11 +52,15 @@ export class SettingsTab extends PluginSettingTab { ) // Ignore diacritics + const diacriticsDesc = new DocumentFragment() + diacriticsDesc.createSpan({}, span => { + span.innerHTML = `Normalize diacritics in search terms. Words like "brûlée" or "žluťoučký" will be indexed as "brulee" and "zlutoucky".
+ Needs a restart to fully take effect. + ` + }) new Setting(containerEl) .setName('Ignore diacritics') - .setDesc( - 'Normalize diacritics in search terms. Words like "brûlée" or "žluťoučký" will be indexed as "brulee" and "zlutoucky". Needs a restart to take effect.', - ) + .setDesc(diacriticsDesc) .addToggle(toggle => toggle.setValue(settings.ignoreDiacritics).onChange(async v => { settings.ignoreDiacritics = v @@ -63,13 +68,20 @@ export class SettingsTab extends PluginSettingTab { }), ) + const serializedIndexDesc = new DocumentFragment() + serializedIndexDesc.createSpan({}, span => { + span.innerHTML = `The search index is stored on disk, instead of being rebuilt at every startup. This results in faster loading times for bigger vaults and mobile devices.
+ ⚠️ Note: the index can become corrupted - if you notice any issue, disable and re-enable this option to clear the cache.
+ Needs a restart to fully take effect. + ` + }) new Setting(containerEl) - .setName('Store index in file') - .setDesc( - 'Index is stored on disk, instead of being rebuilt at every startup.', - ) + .setName('EXPERIMENTAL - Store index in file') + .setDesc(serializedIndexDesc) .addToggle(toggle => toggle.setValue(settings.storeIndexInFile).onChange(async v => { + app.vault.adapter.remove(notesCacheFilePath) + app.vault.adapter.remove(searchIndexFilePath) settings.storeIndexInFile = v await saveSettings(this.plugin) }), @@ -173,7 +185,7 @@ export class SettingsTab extends PluginSettingTab { export const DEFAULT_SETTINGS: OmnisearchSettings = { respectExcluded: true, - ignoreDiacritics: false, + ignoreDiacritics: true, showIndexingNotices: false, showShortName: false,