Grouping in-file results when matches are close enough in the text

This commit is contained in:
Simon Cambier
2022-04-18 16:22:02 +02:00
parent 3168a89070
commit 41203a11da
4 changed files with 45 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
<script lang="ts">
import type { IndexedNote, SearchMatch } from "./globals"
import { surroundLen, type IndexedNote, type SearchMatch } from "./globals"
import { indexedNotes, inFileSearch } from "./stores"
import { escapeHTML } from "./utils";
@@ -15,7 +15,6 @@ inFileSearch.subscribe((file) => {
function cleanContent(content: string): string {
const pos = match.offset ?? -1
if (pos > -1) {
const surroundLen = 180
const from = Math.max(0, pos - surroundLen)
const to = Math.min(content.length - 1, pos + surroundLen)
content =

View File

@@ -1,12 +1,47 @@
<script lang="ts">
import CmpInput from "./CmpInput.svelte"
import CmpNoteInternalResult from "./CmpInfileResult.svelte"
import type { ResultNote } from "./globals"
import {
resultNotes,
} from "./stores"
import { surroundLen, type ResultNote, type SearchMatch } from "./globals"
import { resultNotes } from "./stores"
import type { SearchResult } from "minisearch"
import { stringsToRegex } from "./utils"
$: firstResult = $resultNotes[0]
$: matches = firstResult?.matches ?? []
$: groupedMatches = (() => {
const groups = getGroups()
return groups.map((group) => ({
match: "",
offset: (group.first()!.offset + group.last()!.offset) / 2,
}))
})()
/**
* Group together close
*/
function getGroups(): SearchMatch[][] {
const groups: SearchMatch[][] = []
let lastOffset = -1
while (true) {
const group = getGroupedMatches(matches, lastOffset, surroundLen)
if (!group.length) break
lastOffset = group.last()!.offset
groups.push(group)
}
return groups
}
function getGroupedMatches(
matches: SearchMatch[],
offsetFrom: number,
maxLen: number
): SearchMatch[] {
const first = matches.find((m) => m.offset >= offsetFrom)
if (!first) return []
return matches.filter(
(m) => m.offset > offsetFrom && m.offset <= first.offset + maxLen
)
}
function onInputEnter(event: CustomEvent<ResultNote>): void {
// console.log(event.detail)
@@ -20,8 +55,8 @@ function onInputEnter(event: CustomEvent<ResultNote>): void {
<div class="modal-content">
<div class="prompt-results">
{#if firstResult}
{#each firstResult.matches as match}
{#if groupedMatches.length}
{#each groupedMatches as match}
<CmpNoteInternalResult {match} />
{/each}
{:else}

View File

@@ -1,5 +1,5 @@
<script lang="ts">
import type { ResultNote } from "./globals"
import { surroundLen, type ResultNote } from "./globals"
import { openNote } from "./notes"
import { getMatches } from "./search";
import { modal, selectedNote } from "./stores"
@@ -20,7 +20,6 @@ export let note: ResultNote
function cleanContent(content: string): string {
const pos = note.matches[0]?.offset ?? -1
if (pos > -1) {
const surroundLen = 180
const from = Math.max(0, pos - surroundLen)
const to = Math.min(content.length - 1, pos + surroundLen)
content =

View File

@@ -1,10 +1,10 @@
import type { SearchResult } from 'minisearch'
// Matches a wikiling that begins a string
export const regexWikilink = /^!?\[\[(?<name>.+?)(\|(?<alias>.+?))?\]\]/
export const regexLineSplit = /\r?\n|\r|((\.|\?|!)( |\r?\n|\r))/g
export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms
export const surroundLen = 180
export type SearchNote = {
path: string
basename: string