Grouping in-file results when matches are close enough in the text
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { IndexedNote, SearchMatch } from "./globals"
|
import { surroundLen, type IndexedNote, type SearchMatch } from "./globals"
|
||||||
import { indexedNotes, inFileSearch } from "./stores"
|
import { indexedNotes, inFileSearch } from "./stores"
|
||||||
import { escapeHTML } from "./utils";
|
import { escapeHTML } from "./utils";
|
||||||
|
|
||||||
@@ -15,7 +15,6 @@ inFileSearch.subscribe((file) => {
|
|||||||
function cleanContent(content: string): string {
|
function cleanContent(content: string): string {
|
||||||
const pos = match.offset ?? -1
|
const pos = match.offset ?? -1
|
||||||
if (pos > -1) {
|
if (pos > -1) {
|
||||||
const surroundLen = 180
|
|
||||||
const from = Math.max(0, pos - surroundLen)
|
const from = Math.max(0, pos - surroundLen)
|
||||||
const to = Math.min(content.length - 1, pos + surroundLen)
|
const to = Math.min(content.length - 1, pos + surroundLen)
|
||||||
content =
|
content =
|
||||||
|
|||||||
@@ -1,12 +1,47 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import CmpInput from "./CmpInput.svelte"
|
import CmpInput from "./CmpInput.svelte"
|
||||||
import CmpNoteInternalResult from "./CmpInfileResult.svelte"
|
import CmpNoteInternalResult from "./CmpInfileResult.svelte"
|
||||||
import type { ResultNote } from "./globals"
|
import { surroundLen, type ResultNote, type SearchMatch } from "./globals"
|
||||||
import {
|
import { resultNotes } from "./stores"
|
||||||
resultNotes,
|
import type { SearchResult } from "minisearch"
|
||||||
} from "./stores"
|
import { stringsToRegex } from "./utils"
|
||||||
|
|
||||||
$: firstResult = $resultNotes[0]
|
$: 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 {
|
function onInputEnter(event: CustomEvent<ResultNote>): void {
|
||||||
// console.log(event.detail)
|
// console.log(event.detail)
|
||||||
@@ -20,8 +55,8 @@ function onInputEnter(event: CustomEvent<ResultNote>): void {
|
|||||||
|
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<div class="prompt-results">
|
<div class="prompt-results">
|
||||||
{#if firstResult}
|
{#if groupedMatches.length}
|
||||||
{#each firstResult.matches as match}
|
{#each groupedMatches as match}
|
||||||
<CmpNoteInternalResult {match} />
|
<CmpNoteInternalResult {match} />
|
||||||
{/each}
|
{/each}
|
||||||
{:else}
|
{:else}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { ResultNote } from "./globals"
|
import { surroundLen, type ResultNote } from "./globals"
|
||||||
import { openNote } from "./notes"
|
import { openNote } from "./notes"
|
||||||
import { getMatches } from "./search";
|
import { getMatches } from "./search";
|
||||||
import { modal, selectedNote } from "./stores"
|
import { modal, selectedNote } from "./stores"
|
||||||
@@ -20,7 +20,6 @@ export let note: ResultNote
|
|||||||
function cleanContent(content: string): string {
|
function cleanContent(content: string): string {
|
||||||
const pos = note.matches[0]?.offset ?? -1
|
const pos = note.matches[0]?.offset ?? -1
|
||||||
if (pos > -1) {
|
if (pos > -1) {
|
||||||
const surroundLen = 180
|
|
||||||
const from = Math.max(0, pos - surroundLen)
|
const from = Math.max(0, pos - surroundLen)
|
||||||
const to = Math.min(content.length - 1, pos + surroundLen)
|
const to = Math.min(content.length - 1, pos + surroundLen)
|
||||||
content =
|
content =
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import type { SearchResult } from 'minisearch'
|
|
||||||
|
|
||||||
// Matches a wikiling that begins a string
|
// Matches a wikiling that begins a string
|
||||||
export const regexWikilink = /^!?\[\[(?<name>.+?)(\|(?<alias>.+?))?\]\]/
|
export const regexWikilink = /^!?\[\[(?<name>.+?)(\|(?<alias>.+?))?\]\]/
|
||||||
export const regexLineSplit = /\r?\n|\r|((\.|\?|!)( |\r?\n|\r))/g
|
export const regexLineSplit = /\r?\n|\r|((\.|\?|!)( |\r?\n|\r))/g
|
||||||
export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms
|
export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms
|
||||||
|
|
||||||
|
export const surroundLen = 180
|
||||||
|
|
||||||
export type SearchNote = {
|
export type SearchNote = {
|
||||||
path: string
|
path: string
|
||||||
basename: string
|
basename: string
|
||||||
|
|||||||
Reference in New Issue
Block a user