WIP in-file search

This commit is contained in:
Simon Cambier
2022-04-17 22:03:06 +02:00
parent 34c771cc31
commit cfa24b9617
10 changed files with 164 additions and 94 deletions

View File

@@ -13,7 +13,6 @@
"license": "MIT",
"devDependencies": {
"@tsconfig/svelte": "^3.0.0",
"@types/lodash-es": "^4.17.6",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
@@ -36,7 +35,6 @@
"typescript": "^4.6.3"
},
"dependencies": {
"lodash-es": "^4.17.21",
"minisearch": "^5.0.0-beta1"
}
}

18
pnpm-lock.yaml generated
View File

@@ -2,7 +2,6 @@ lockfileVersion: 5.3
specifiers:
'@tsconfig/svelte': ^3.0.0
'@types/lodash-es': ^4.17.6
'@types/node': ^16.11.6
'@typescript-eslint/eslint-plugin': ^5.18.0
'@typescript-eslint/parser': ^5.18.0
@@ -16,7 +15,6 @@ specifiers:
eslint-plugin-node: 11.1.0
eslint-plugin-promise: 5.0.0
eslint-plugin-svelte3: ^3.4.1
lodash-es: ^4.17.21
minisearch: ^5.0.0-beta1
obsidian: latest
prettier: ^2.6.2
@@ -27,12 +25,10 @@ specifiers:
typescript: ^4.6.3
dependencies:
lodash-es: 4.17.21
minisearch: 5.0.0-beta1
devDependencies:
'@tsconfig/svelte': 3.0.0
'@types/lodash-es': 4.17.6
'@types/node': 16.11.26
'@typescript-eslint/eslint-plugin': 5.18.0_2e93aa916703472007e9b5dfec98785b
'@typescript-eslint/parser': 5.18.0_eslint@7.12.1+typescript@4.6.3
@@ -168,16 +164,6 @@ packages:
resolution: {integrity: sha1-7ihweulOEdK4J7y+UnC86n8+ce4=}
dev: true
/@types/lodash-es/4.17.6:
resolution: {integrity: sha512-R+zTeVUKDdfoRxpAryaQNRKk3105Rrgx2CFRClIgRGaqDTdjsm8h6IYA8ir584W3ePzkZfst5xIgDwYrlh9HLg==}
dependencies:
'@types/lodash': 4.14.181
dev: true
/@types/lodash/4.14.181:
resolution: {integrity: sha512-n3tyKthHJbkiWhDZs3DkhkCzt2MexYHXlX0td5iMplyfwketaOeKboEVBqzceH7juqvEg3q5oUoBFxSLu7zFag==}
dev: true
/@types/node/16.11.26:
resolution: {integrity: sha512-GZ7bu5A6+4DtG7q9GsoHXy3ALcgeIHP4NnL0Vv2wu0uUB/yQex26v0tf6/na1mm0+bS9Uw+0DFex7aaKr2qawQ==}
dev: true
@@ -1561,10 +1547,6 @@ packages:
path-exists: 3.0.0
dev: true
/lodash-es/4.17.21:
resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==}
dev: false
/lodash.merge/4.6.2:
resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==}
dev: true

View File

@@ -11,6 +11,7 @@ onMount(async () => {
await tick()
input.focus()
input.select()
input.value = $searchQuery
})
const debouncedOnInput = debounce(() => $searchQuery = inputValue, 100)
@@ -41,6 +42,9 @@ function moveNoteSelection(ev: KeyboardEvent): void {
} else if (ev.shiftKey) {
// Create a new note
dispatch("shift-enter", $selectedNote)
} else if (ev.altKey) {
// Create a new note
dispatch("alt-enter", $selectedNote)
} else {
// Open in current pane
dispatch("enter", $selectedNote)

View File

@@ -1,11 +1,13 @@
<script lang="ts">
import { tick } from "svelte"
import { onMount, tick } from "svelte"
import CmpInput from "./CmpInput.svelte"
import CmpNoteInternalResult from "./CmpNoteInternalResult.svelte"
import CmpNoteResult from "./CmpNoteResult.svelte"
import type { ResultNote } from "./globals"
import { openNote } from "./notes"
import { getSuggestions } from "./search"
import {
inFileSearch,
modal,
plugin,
resultNotes,
@@ -13,8 +15,18 @@ import {
selectedNote,
} from "./stores"
$: firstResult = $resultNotes[0]
searchQuery.subscribe(async (q) => {
const results = getSuggestions(q)
// If we're in "single file" mode, the search results array
// will contain a single result, related to this file
const results = $inFileSearch
? getSuggestions(q, { singleFile: $inFileSearch })
: getSuggestions(q)
console.log(results)
// Search on the whole vault
resultNotes.set(results)
const firstResult = results[0]
if (firstResult) {
@@ -57,18 +69,32 @@ function onInputShiftEnter(event: CustomEvent<ResultNote>): void {
}
</script>
<div class="modal-title">Omnisearch</div>
<CmpInput
on:enter={onInputEnter}
on:shift-enter={onInputShiftEnter}
on:ctrl-enter={onInputCtrlEnter}
on:enter={onInputEnter}
on:shift-enter={onInputShiftEnter}
on:ctrl-enter={onInputCtrlEnter}
/>
<div class="prompt-results">
<div class="modal-content">
<div class="prompt-results">
{#if $inFileSearch}
<!-- In-file results -->
{#if firstResult}
{#each firstResult.matches as match}
<CmpNoteInternalResult {match} />
{/each}
{:else}
We found 0 result for your search here.
{/if}
{:else}
<!-- Multi-files results -->
{#each $resultNotes as result}
<CmpNoteResult selected={result === $selectedNote} note={result} />
{/each}
</div>
<div class="prompt-instructions">
{/if}
</div>
<div class="prompt-instructions">
<div class="prompt-instruction">
<span class="prompt-instruction-command">↑↓</span><span>to navigate</span>
</div>
@@ -76,15 +102,15 @@ function onInputShiftEnter(event: CustomEvent<ResultNote>): void {
<span class="prompt-instruction-command"></span><span>to open</span>
</div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">ctrl ↵</span><span
>to open in a new pane</span
>
<span class="prompt-instruction-command">ctrl ↵</span>
<span>to open in a new pane</span>
</div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">shift ↵</span><span>to create</span
>
<span class="prompt-instruction-command">shift ↵</span>
<span>to create</span>
</div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">esc</span><span>to dismiss</span>
</div>
</div>
</div>

View File

@@ -0,0 +1,11 @@
<script lang="ts">
import type { SearchMatch } from "./globals"
export let match: SearchMatch
</script>
<div class="suggestion-item omnisearch-result">
<div class="omnisearch-result__body">
{JSON.stringify(match)}
</div>
</div>

View File

@@ -1,9 +1,9 @@
import { Plugin, TFile } from 'obsidian'
import { MarkdownView, Plugin, TFile } from 'obsidian'
import { OmnisearchModal } from './modal'
import { plugin } from './stores'
import {
addToIndex,
instantiateMinisearch,
initGlobalSearchIndex,
removeFromIndex,
removeFromIndexByPath,
} from './search'
@@ -12,18 +12,34 @@ export default class OmnisearchPlugin extends Plugin {
async onload(): Promise<void> {
plugin.set(this)
await instantiateMinisearch()
await initGlobalSearchIndex()
// Commands to display Omnisearch modal
this.addCommand({
id: 'show-modal',
name: 'Open Omnisearch',
name: 'Vault search',
// hotkeys: [{ modifiers: ['Mod'], key: 'o' }],
callback: () => {
new OmnisearchModal(this).open()
},
})
this.addCommand({
id: 'show-modal-infile',
name: 'In-file search',
// hotkeys: [{ modifiers: ['Mod'], key: 'o' }],
checkCallback: (checking: boolean) => {
const view = this.app.workspace.getActiveViewOfType(MarkdownView)
if (view) {
if (!checking) {
new OmnisearchModal(this, view.file).open()
}
return true
}
return false
},
})
// Listeners to keep the search index up-to-date
this.registerEvent(
this.app.vault.on('create', file => {

View File

@@ -1,13 +1,19 @@
import { Modal } from 'obsidian'
import { Modal, TFile } from 'obsidian'
import type OmnisearchPlugin from './main'
import CmpModal from './CmpModal.svelte'
import { modal } from './stores'
import { inFileSearch, modal } from './stores'
export class OmnisearchModal extends Modal {
constructor(plugin: OmnisearchPlugin) {
constructor(plugin: OmnisearchPlugin, file?: TFile) {
super(plugin.app)
// Remove all the default modal's children (except the close button)
// so that we can more easily customize it
const closeEl = this.containerEl.find('.modal-close-button')
this.modalEl.replaceChildren()
this.modalEl.append(closeEl)
this.modalEl.addClass('omnisearch-modal', 'prompt')
this.modalEl.replaceChildren() // Remove all the default Modal's children
inFileSearch.set(file ?? null)
modal.set(this)

View File

@@ -1,21 +1,15 @@
import { Notice, TFile, type TAbstractFile } from 'obsidian'
import MiniSearch from 'minisearch'
import MiniSearch, { type SearchResult } from 'minisearch'
import type { IndexedNote, ResultNote, SearchMatch } from './globals'
import { indexedNotes, plugin } from './stores'
import { get } from 'svelte/store'
import {
escapeRegex,
extractHeadingsFromCache,
getAllIndices,
stringsToRegex,
wait,
} from './utils'
import { extractHeadingsFromCache, stringsToRegex, wait } from './utils'
let minisearch: MiniSearch<IndexedNote>
let minisearchInstance: MiniSearch<IndexedNote>
export async function instantiateMinisearch(): Promise<void> {
export async function initGlobalSearchIndex(): Promise<void> {
indexedNotes.set({})
minisearch = new MiniSearch({
minisearchInstance = new MiniSearch({
idField: 'path',
fields: ['basename', 'content', 'headings1', 'headings2', 'headings3'],
})
@@ -54,9 +48,8 @@ export function getMatches(text: string, reg: RegExp): SearchMatch[] {
return matches
}
export function getSuggestions(query: string): ResultNote[] {
const results = minisearch
.search(query, {
function search(query: string): SearchResult[] {
return minisearchInstance.search(query, {
prefix: true,
fuzzy: term => (term.length > 4 ? 0.2 : false),
combineWith: 'AND',
@@ -67,10 +60,22 @@ export function getSuggestions(query: string): ResultNote[] {
headings3: 1.1,
},
})
.sort((a, b) => b.score - a.score)
.slice(0, 50)
// console.log(`Omnisearch - Results for "${query}"`)
// console.log(results)
}
export function getSuggestions(
query: string,
options?: Partial<{ singleFile: TFile }>,
): ResultNote[] {
let results = search(query)
if (options?.singleFile) {
const file = options.singleFile
const result = results.find(r => r.id === file.path)
if (result) results = [result]
else results = []
}
else {
results = results.sort((a, b) => b.score - a.score).slice(0, 50)
}
const suggestions = results.map(result => {
const note = indexedNotes.get(result.id)
@@ -80,17 +85,11 @@ export function getSuggestions(query: string): ResultNote[] {
const words = Object.keys(result.match)
const matches = getMatches(note.content, stringsToRegex(words))
const resultNote: ResultNote = {
// searchResult: result,
foundWords: words,
occurence: 0,
matches,
...note,
}
// if (note.basename === 'Search') {
// console.log('=======')
// console.log(result)
// console.log(resultNote)
// }
return resultNote
})
@@ -127,7 +126,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
? extractHeadingsFromCache(fileCache, 3).join(' ')
: '',
}
minisearch.add(note)
minisearchInstance.add(note)
indexedNotes.add(note)
}
catch (e) {
@@ -146,7 +145,7 @@ export function removeFromIndex(file: TAbstractFile): void {
export function removeFromIndexByPath(path: string): void {
const note = indexedNotes.get(path)
if (note) {
minisearch.remove(note)
minisearchInstance.remove(note)
indexedNotes.remove(path)
}
}

View File

@@ -1,3 +1,4 @@
import type { TFile } from 'obsidian'
import { get, writable } from 'svelte/store'
import type { IndexedNote, ResultNote } from './globals'
import type OmnisearchPlugin from './main'
@@ -52,9 +53,37 @@ function createSelectedNote() {
}
}
/**
* If this field is set, the search will be limited to the given file
*/
export const inFileSearch = writable<TFile | null>(null)
/**
* The current search query
*/
export const searchQuery = writable<string>('')
/**
* The search results list, according to the current search query
*/
export const resultNotes = writable<ResultNote[]>([])
export const plugin = writable<OmnisearchPlugin>()
export const modal = writable<OmnisearchModal>()
/**
* The currently selected/hovered note in the results list
*/
export const selectedNote = createSelectedNote()
/**
* A reference to the plugin instance
*/
export const plugin = writable<OmnisearchPlugin>()
/**
* A reference to the modal instance
*/
export const modal = writable<OmnisearchModal>()
/**
* The entire list of indexed notes, constantly kept up-to-date.
*/
export const indexedNotes = createIndexedNotes()

View File

@@ -5,7 +5,6 @@ import {
regexYaml,
} from './globals'
import type { SearchMatch } from './globals'
import { uniqBy } from 'lodash-es'
export function highlighter(str: string): string {
return '<span class="search-result-file-matched-text">' + str + '</span>'