@@ -19,4 +19,6 @@ export type ResultNote = {
|
|||||||
path: string
|
path: string
|
||||||
basename: string
|
basename: string
|
||||||
content: string
|
content: string
|
||||||
|
keyword: string
|
||||||
|
occurence: number
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Notice, Plugin, TAbstractFile, TFile } from 'obsidian'
|
import { Notice, Plugin, TAbstractFile, TFile } from 'obsidian'
|
||||||
import MiniSearch from 'minisearch'
|
import MiniSearch from 'minisearch'
|
||||||
import { clearContent, getTitleLine, removeTitleLine, wait } from './utils'
|
import { clearContent, getTitleLine, wait } from './utils'
|
||||||
import { IndexedNote } from './globals'
|
import { IndexedNote } from './globals'
|
||||||
import { OmnisearchModal } from './modal'
|
import { OmnisearchModal } from './modal'
|
||||||
|
|
||||||
@@ -66,7 +66,9 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
|
|
||||||
// This is basically the same behavior as MiniSearch's `addAllAsync()`.
|
// This is basically the same behavior as MiniSearch's `addAllAsync()`.
|
||||||
// We index files by batches of 10
|
// We index files by batches of 10
|
||||||
if (files.length) { console.log('Omnisearch - indexing ' + files.length + ' files') }
|
if (files.length) {
|
||||||
|
console.log('Omnisearch - indexing ' + files.length + ' files')
|
||||||
|
}
|
||||||
for (let i = 0; i < files.length; ++i) {
|
for (let i = 0; i < files.length; ++i) {
|
||||||
if (i % 10 === 0) await wait(0)
|
if (i % 10 === 0) await wait(0)
|
||||||
const file = files[i]
|
const file = files[i]
|
||||||
@@ -86,6 +88,8 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
async addToIndex(file: TAbstractFile): Promise<void> {
|
async addToIndex(file: TAbstractFile): Promise<void> {
|
||||||
if (!(file instanceof TFile) || file.extension !== 'md') return
|
if (!(file instanceof TFile) || file.extension !== 'md') return
|
||||||
try {
|
try {
|
||||||
|
// console.log(`Omnisearch - adding ${file.path} to index`)
|
||||||
|
|
||||||
if (this.indexedNotes[file.path]) {
|
if (this.indexedNotes[file.path]) {
|
||||||
throw new Error(`${file.basename} is already indexed`)
|
throw new Error(`${file.basename} is already indexed`)
|
||||||
}
|
}
|
||||||
@@ -114,6 +118,7 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
|
|
||||||
removeFromIndex(file: TAbstractFile): void {
|
removeFromIndex(file: TAbstractFile): void {
|
||||||
if (file instanceof TFile && file.path.endsWith('.md')) {
|
if (file instanceof TFile && file.path.endsWith('.md')) {
|
||||||
|
// console.log(`Omnisearch - removing ${file.path} from index`)
|
||||||
return this.removeFromIndexByPath(file.path)
|
return this.removeFromIndexByPath(file.path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/modal.ts
28
src/modal.ts
@@ -1,4 +1,4 @@
|
|||||||
import { SuggestModal } from 'obsidian'
|
import { MarkdownView, SuggestModal, TFile } from 'obsidian'
|
||||||
import { ResultNote } from './globals'
|
import { ResultNote } from './globals'
|
||||||
import OmnisearchPlugin from './main'
|
import OmnisearchPlugin from './main'
|
||||||
import { escapeRegex, highlighter } from './utils'
|
import { escapeRegex, highlighter } from './utils'
|
||||||
@@ -133,7 +133,14 @@ export class OmnisearchModal extends SuggestModal<ResultNote> {
|
|||||||
content = content.replace(reg, highlighter)
|
content = content.replace(reg, highlighter)
|
||||||
basename = basename.replace(reg, highlighter)
|
basename = basename.replace(reg, highlighter)
|
||||||
|
|
||||||
return { content, basename, path: note.path }
|
const resultNote: ResultNote = {
|
||||||
|
content,
|
||||||
|
basename,
|
||||||
|
path: note.path,
|
||||||
|
keyword: result.terms[0],
|
||||||
|
occurence: 0,
|
||||||
|
}
|
||||||
|
return resultNote
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +157,20 @@ export class OmnisearchModal extends SuggestModal<ResultNote> {
|
|||||||
body.innerHTML = value.content
|
body.innerHTML = value.content
|
||||||
}
|
}
|
||||||
|
|
||||||
onChooseSuggestion(item: ResultNote): void {
|
async onChooseSuggestion(item: ResultNote): Promise<void> {
|
||||||
this.app.workspace.openLinkText(item.path, '')
|
const file = this.app.vault.getAbstractFileByPath(item.path) as TFile
|
||||||
|
const content = (await this.app.vault.cachedRead(file)).toLowerCase()
|
||||||
|
const offset = content.indexOf(item.keyword.toLowerCase())
|
||||||
|
await this.app.workspace.openLinkText(item.path, '')
|
||||||
|
|
||||||
|
const view = this.app.workspace.getActiveViewOfType(MarkdownView)
|
||||||
|
const pos = view.editor.offsetToPos(offset)
|
||||||
|
pos.ch = 0
|
||||||
|
|
||||||
|
view.editor.setCursor(pos)
|
||||||
|
view.editor.scrollIntoView({
|
||||||
|
from: { line: pos.line - 10, ch: 0 },
|
||||||
|
to: { line: pos.line + 10, ch: 0 },
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user