Small rework

This commit is contained in:
Simon Cambier
2022-10-20 17:27:15 +02:00
parent c2535b0ebd
commit 1376cea282
6 changed files with 24 additions and 21 deletions

View File

@@ -2,7 +2,6 @@
import { cacheManager } from 'src/cache-manager'
import { settings, showExcerpt } from 'src/settings'
import type { ResultNote } from '../globals'
import * as Search from '../search'
import { highlighter, makeExcerpt, stringsToRegex } from '../utils'
import ResultItemContainer from './ResultItemContainer.svelte'
@@ -10,7 +9,6 @@
export let note: ResultNote
$: reg = stringsToRegex(note.foundWords)
$: matches = Search.getMatches(note.content, reg)
$: cleanedContent = makeExcerpt(note.content, note.matches[0]?.offset ?? -1)
$: glyph = cacheManager.getNoteFromMemCache(note.path)?.doesNotExist
$: title = settings.showShortName ? note.basename : note.path
@@ -27,9 +25,9 @@
{@html title.replace(reg, highlighter)}
</span>
{#if matches.length > 0}
{#if note.matches.length > 0}
<span class="omnisearch-result__counter">
{matches.length}&nbsp;{matches.length > 1 ? 'matches' : 'match'}
{note.matches.length}&nbsp;{note.matches.length > 1 ? 'matches' : 'match'}
</span>
{/if}
</div>

View File

@@ -1,4 +1,6 @@
import pLimit from 'p-limit'
import { EventBus } from './event-bus'
import { settings } from './settings'
export const regexLineSplit = /\r?\n|\r|((\.|\?|!)( |\r?\n|\r))/g
export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms

View File

@@ -59,7 +59,7 @@ export default class OmnisearchPlugin extends Plugin {
// Listeners to keep the search index up-to-date
this.registerEvent(
this.app.vault.on('create', file => {
NotesIndex.addToIndexAndCache(file)
NotesIndex.addToIndexAndMemCache(file)
})
)
this.registerEvent(
@@ -76,7 +76,7 @@ export default class OmnisearchPlugin extends Plugin {
this.app.vault.on('rename', async (file, oldPath) => {
if (file instanceof TFile && isFilePlaintext(file.path)) {
NotesIndex.removeFromIndex(oldPath)
await NotesIndex.addToIndexAndCache(file)
await NotesIndex.addToIndexAndMemCache(file)
}
})
)
@@ -89,7 +89,7 @@ export default class OmnisearchPlugin extends Plugin {
onunload(): void {
console.log('Omnisearch - Interrupting PDF indexing')
NotesIndex.pdfQueue.clearQueue()
NotesIndex.processQueue.clearQueue()
}
addRibbonButton(): void {

View File

@@ -9,21 +9,26 @@ import {
} from './utils'
import { getNonExistingNotes, removeAnchors } from './notes'
import { pdfManager } from './pdf-manager'
import type { IndexedDocument } from './globals'
import { settings } from './settings'
import * as Search from './search'
// import PQueue from 'p-queue-compat'
import pLimit from 'p-limit'
import { cacheManager } from './cache-manager'
import pLimit from 'p-limit'
import type { IndexedDocument } from './globals'
export const pdfQueue = pLimit(settings.backgroundProcesses)
/**
* Use this processing queue to handle all heavy work
*/
export const processQueue = pLimit(settings.backgroundProcesses)
/**
* Adds a file to the search index
* @param file
* @returns
*/
export async function addToIndexAndCache(file: TAbstractFile): Promise<void> {
export async function addToIndexAndMemCache(
file: TAbstractFile
): Promise<void> {
if (!(file instanceof TFile) || !isFileIndexable(file.path)) {
return
}
@@ -154,7 +159,7 @@ export async function refreshIndex(): Promise<void> {
}
for (const note of notesToReindex) {
removeFromIndex(note.path)
await addToIndexAndCache(note)
await addToIndexAndMemCache(note)
await wait(0)
}
notesToReindex.clear()
@@ -173,8 +178,8 @@ export async function indexPDFs() {
removeFromIndex(file.path)
}
input.push(
pdfQueue(async () => {
await addToIndexAndCache(file)
processQueue(async () => {
await addToIndexAndMemCache(file)
await cacheManager.writeMinisearchIndex(Search.minisearchInstance)
})
)

View File

@@ -17,7 +17,6 @@ import {
import type { Query } from './query'
import { settings } from './settings'
import * as NotesIndex from './notes-index'
import pLimit from 'p-limit'
import { cacheManager } from './cache-manager'
export let minisearchInstance: MiniSearch<IndexedDocument>
@@ -98,13 +97,14 @@ export async function initGlobalSearchIndex(): Promise<void> {
}
// Read and index all the files into the search engine
const queue = pLimit(settings.backgroundProcesses)
const input = []
for (const file of files) {
if (cacheManager.getNoteFromMemCache(file.path)) {
NotesIndex.removeFromIndex(file.path)
}
input.push(queue(() => NotesIndex.addToIndexAndCache(file)))
input.push(
NotesIndex.processQueue(() => NotesIndex.addToIndexAndMemCache(file))
)
}
await Promise.all(input)
@@ -148,7 +148,7 @@ async function search(query: Query): Promise<SearchResult[]> {
headings3: settings.weightH3,
},
})
// Downrank files that are in Obsidian's excluded list
if (settings.respectExcluded) {
results.forEach(result => {
@@ -218,9 +218,7 @@ export async function getSuggestions(
options?: Partial<{ singleFilePath: string | null }>
): Promise<ResultNote[]> {
// Get the raw results
console.time('search')
let results = await search(query)
console.timeEnd('search')
if (!results.length) return []
// Extract tags from the query