Removed duplicated code
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { createEventDispatcher } from "svelte"
|
import { createEventDispatcher } from "svelte"
|
||||||
import { excerptAfter, excerptBefore, type ResultNote } from "./globals"
|
import type { ResultNote } from "./globals"
|
||||||
import { escapeHTML, highlighter, stringsToRegex } from "./utils"
|
import { highlighter, makeExcerpt, stringsToRegex } from "./utils"
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
export let offset: number
|
export let offset: number
|
||||||
@@ -10,19 +10,6 @@ export let index = 0
|
|||||||
export let selected = false
|
export let selected = false
|
||||||
|
|
||||||
$: reg = stringsToRegex(note.foundWords)
|
$: reg = stringsToRegex(note.foundWords)
|
||||||
|
|
||||||
function cleanContent(content: string): string {
|
|
||||||
const pos = offset ?? -1
|
|
||||||
if (pos > -1) {
|
|
||||||
const from = Math.max(0, pos - excerptBefore)
|
|
||||||
const to = Math.min(content.length - 1, pos + excerptAfter)
|
|
||||||
content =
|
|
||||||
(from > 0 ? "…" : "") +
|
|
||||||
content.slice(from, to).trim() +
|
|
||||||
(to < content.length - 1 ? "…" : "")
|
|
||||||
}
|
|
||||||
return escapeHTML(content)
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
@@ -33,6 +20,6 @@ function cleanContent(content: string): string {
|
|||||||
on:click={(e) => dispatch("click")}
|
on:click={(e) => dispatch("click")}
|
||||||
>
|
>
|
||||||
<div class="omnisearch-result__body">
|
<div class="omnisearch-result__body">
|
||||||
{@html cleanContent(note?.content ?? "").replace(reg, highlighter)}
|
{@html makeExcerpt(note?.content ?? "", offset).replace(reg, highlighter)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { createEventDispatcher } from "svelte"
|
import { createEventDispatcher } from "svelte"
|
||||||
import { excerptAfter, excerptBefore, type ResultNote } from "./globals"
|
import type { ResultNote } from "./globals"
|
||||||
import { getMatches } from "./search"
|
import { getMatches } from "./search"
|
||||||
import { escapeHTML, highlighter, stringsToRegex } from "./utils"
|
import { highlighter, makeExcerpt, stringsToRegex } from "./utils"
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
export let selected = false
|
export let selected = false
|
||||||
@@ -10,20 +10,7 @@ export let note: ResultNote
|
|||||||
|
|
||||||
$: reg = stringsToRegex(note.foundWords)
|
$: reg = stringsToRegex(note.foundWords)
|
||||||
$: matches = getMatches(note.content, reg)
|
$: matches = getMatches(note.content, reg)
|
||||||
$: cleanedContent = cleanContent(note.content)
|
$: cleanedContent = makeExcerpt(note.content, note.matches[0]?.offset ?? -1)
|
||||||
|
|
||||||
function cleanContent(content: string): string {
|
|
||||||
const pos = note.matches[0]?.offset ?? -1
|
|
||||||
if (pos > -1) {
|
|
||||||
const from = Math.max(0, pos - excerptBefore)
|
|
||||||
const to = Math.min(content.length - 1, pos + excerptAfter)
|
|
||||||
content =
|
|
||||||
(from > 0 ? "…" : "") +
|
|
||||||
content.slice(from, to).trim() +
|
|
||||||
(to < content.length - 1 ? "…" : "")
|
|
||||||
}
|
|
||||||
return escapeHTML(content)
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
15
src/utils.ts
15
src/utils.ts
@@ -1,5 +1,7 @@
|
|||||||
import type { CachedMetadata } from 'obsidian'
|
import type { CachedMetadata } from 'obsidian'
|
||||||
import {
|
import {
|
||||||
|
excerptAfter,
|
||||||
|
excerptBefore,
|
||||||
highlightClass,
|
highlightClass,
|
||||||
isSearchMatch,
|
isSearchMatch,
|
||||||
regexLineSplit,
|
regexLineSplit,
|
||||||
@@ -82,3 +84,16 @@ export function extractHeadingsFromCache(
|
|||||||
export function loopIndex(index: number, nbItems: number): number {
|
export function loopIndex(index: number, nbItems: number): number {
|
||||||
return (index + nbItems) % nbItems
|
return (index + nbItems) % nbItems
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function makeExcerpt(content: string, offset: number): string {
|
||||||
|
const pos = offset ?? -1
|
||||||
|
if (pos > -1) {
|
||||||
|
const from = Math.max(0, pos - excerptBefore)
|
||||||
|
const to = Math.min(content.length - 1, pos + excerptAfter)
|
||||||
|
content =
|
||||||
|
(from > 0 ? '…' : '') +
|
||||||
|
content.slice(from, to).trim() +
|
||||||
|
(to < content.length - 1 ? '…' : '')
|
||||||
|
}
|
||||||
|
return escapeHTML(content)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user