#53 - Fixed some highlighting issues

This commit is contained in:
Simon Cambier
2022-06-08 21:54:27 +02:00
parent 3cfad5c414
commit 8f954a3cda
4 changed files with 33 additions and 26 deletions

View File

@@ -22,13 +22,16 @@ export function resetNotesCache(): void {
} }
export async function loadNotesCache(): Promise<void> { export async function loadNotesCache(): Promise<void> {
if (settings.storeIndexInFile && await app.vault.adapter.exists(notesCacheFilePath)) { if (
settings.storeIndexInFile &&
(await app.vault.adapter.exists(notesCacheFilePath))
) {
try { try {
const json = await app.vault.adapter.read(notesCacheFilePath) const json = await app.vault.adapter.read(notesCacheFilePath)
notesCache = JSON.parse(json) notesCache = JSON.parse(json)
console.log('Notes cache loaded from the file') console.log('Notes cache loaded from the file')
} }
catch(e) { catch (e) {
console.trace('Could not load Notes cache from the file') console.trace('Could not load Notes cache from the file')
console.error(e) console.error(e)
} }

View File

@@ -1,4 +1,5 @@
import { stripSurroundingQuotes } from './utils' import { settings } from './settings'
import { removeDiacritics, stripSurroundingQuotes } from './utils'
import { parseQuery } from './vendor/parse-query' import { parseQuery } from './vendor/parse-query'
type QueryToken = { type QueryToken = {
@@ -21,6 +22,7 @@ export class Query {
public exclusions: QueryToken[] = [] public exclusions: QueryToken[] = []
constructor(text = '') { constructor(text = '') {
if (settings.ignoreDiacritics) text = removeDiacritics(text)
const tokens = parseQuery(text.toLowerCase(), { tokenize: true }) const tokens = parseQuery(text.toLowerCase(), { tokenize: true })
this.exclusions = tokens.exclude.text this.exclusions = tokens.exclude.text
.map(this.formatToken) .map(this.formatToken)
@@ -48,5 +50,3 @@ export class Query {
} }
} }
} }

View File

@@ -27,7 +27,7 @@ import {
getNonExistingNotesFromCache, getNonExistingNotesFromCache,
loadNotesCache, loadNotesCache,
saveNotesCacheToFile, saveNotesCacheToFile,
isCacheOutdated isCacheOutdated,
} from './notes' } from './notes'
let minisearchInstance: MiniSearch<IndexedNote> let minisearchInstance: MiniSearch<IndexedNote>
@@ -53,7 +53,7 @@ const tokenize = (text: string): string[] => {
export async function initGlobalSearchIndex(): Promise<void> { export async function initGlobalSearchIndex(): Promise<void> {
const options = { const options = {
tokenize, tokenize,
processTerm: term => processTerm: (term: string) =>
settings.ignoreDiacritics ? removeDiacritics(term) : term, settings.ignoreDiacritics ? removeDiacritics(term) : term,
idField: 'path', idField: 'path',
fields: [ fields: [
@@ -66,14 +66,17 @@ export async function initGlobalSearchIndex(): Promise<void> {
], ],
} }
if (settings.storeIndexInFile && await app.vault.adapter.exists(searchIndexFilePath)) { if (
settings.storeIndexInFile &&
(await app.vault.adapter.exists(searchIndexFilePath))
) {
try { try {
const json = await app.vault.adapter.read(searchIndexFilePath) const json = await app.vault.adapter.read(searchIndexFilePath)
minisearchInstance = MiniSearch.loadJSON(json, options) minisearchInstance = MiniSearch.loadJSON(json, options)
console.log('MiniSearch index loaded from the file') console.log('MiniSearch index loaded from the file')
await loadNotesCache() await loadNotesCache()
} }
catch(e) { catch (e) {
console.trace('Could not load MiniSearch index from the file') console.trace('Could not load MiniSearch index from the file')
console.error(e) console.error(e)
} }
@@ -94,7 +97,8 @@ export async function initGlobalSearchIndex(): Promise<void> {
if (settings.storeIndexInFile) { if (settings.storeIndexInFile) {
files = allFiles.filter(file => isCacheOutdated(file)) files = allFiles.filter(file => isCacheOutdated(file))
notesSuffix = 'modified notes' notesSuffix = 'modified notes'
} else { }
else {
files = allFiles files = allFiles
notesSuffix = 'notes' notesSuffix = 'notes'
} }
@@ -116,8 +120,8 @@ export async function initGlobalSearchIndex(): Promise<void> {
if (files.length > 0) { if (files.length > 0) {
const message = `Omnisearch - Indexed ${files.length} ${notesSuffix} in ${ const message = `Omnisearch - Indexed ${files.length} ${notesSuffix} in ${
new Date().getTime() - start new Date().getTime() - start
}ms` }ms`
console.log(message) console.log(message)

View File

@@ -71,17 +71,17 @@ export class SettingsTab extends PluginSettingTab {
) )
// Ignore diacritics // Ignore diacritics
// new Setting(containerEl) new Setting(containerEl)
// .setName('Ignore diacritics') .setName('Ignore diacritics')
// .setDesc( .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.', 'EXPERIMENTAL - 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.',
// ) )
// .addToggle(toggle => .addToggle(toggle =>
// toggle.setValue(settings.ignoreDiacritics).onChange(async v => { toggle.setValue(settings.ignoreDiacritics).onChange(async v => {
// settings.ignoreDiacritics = v settings.ignoreDiacritics = v
// await saveSettings(this.plugin) await saveSettings(this.plugin)
// }), }),
// ) )
new Setting(containerEl) new Setting(containerEl)
.setName('Store index in file') .setName('Store index in file')
@@ -194,7 +194,7 @@ export class SettingsTab extends PluginSettingTab {
export const DEFAULT_SETTINGS: OmnisearchSettings = { export const DEFAULT_SETTINGS: OmnisearchSettings = {
respectExcluded: true, respectExcluded: true,
reindexInRealTime: false, reindexInRealTime: false,
ignoreDiacritics: true, ignoreDiacritics: false,
showIndexingNotices: false, showIndexingNotices: false,
showShortName: false, showShortName: false,