diff --git a/src/components/ResultItemInFile.svelte b/src/components/ResultItemInFile.svelte index 3f35c51..3dcba2e 100644 --- a/src/components/ResultItemInFile.svelte +++ b/src/components/ResultItemInFile.svelte @@ -1,6 +1,10 @@ - +
- {@html cleanedContent.replace(reg, highlighter)} + {@html cleanedContent.replace(reg, highlighterGroups)}
diff --git a/src/components/ResultItemVault.svelte b/src/components/ResultItemVault.svelte index afa5e6e..13a287b 100644 --- a/src/components/ResultItemVault.svelte +++ b/src/components/ResultItemVault.svelte @@ -3,18 +3,16 @@ import type { ResultNote } from '../globals' import { getExtension, - highlighter, + highlighterGroups, isFileCanvas, isFileImage, isFilePDF, - isFilePlaintext, makeExcerpt, pathWithoutFilename, removeDiacritics, stringsToRegex, } from '../tools/utils' import ResultItemContainer from './ResultItemContainer.svelte' - import { onMount } from 'svelte' import { setIcon } from 'obsidian' export let selected = false @@ -54,7 +52,8 @@ if (elFilePathIcon) { if (isFileImage(note.path)) setIcon(elFilePathIcon, 'image') else if (isFilePDF(note.path)) setIcon(elFilePathIcon, 'file-text') - else if (isFileCanvas(note.path)) setIcon(elFilePathIcon, 'layout-dashboard') + else if (isFileCanvas(note.path)) + setIcon(elFilePathIcon, 'layout-dashboard') else setIcon(elFilePathIcon, 'file') } } @@ -70,7 +69,7 @@
- {@html title.replace(reg, highlighter)} + {@html title.replace(reg, highlighterGroups)} .{getExtension(note.path)} @@ -96,7 +95,7 @@
{#if $showExcerpt}
- {@html cleanedContent.replace(reg, highlighter)} + {@html cleanedContent.replace(reg, highlighterGroups)}
{/if} diff --git a/src/tools/utils.ts b/src/tools/utils.ts index 82470bd..00e0666 100644 --- a/src/tools/utils.ts +++ b/src/tools/utils.ts @@ -15,6 +15,7 @@ import { regexLineSplit, regexStripQuotes, regexYaml, + SPACE_OR_PUNCTUATION, type SearchMatch, } from '../globals' import { settings } from '../settings' @@ -25,6 +26,8 @@ export function highlighter(str: string): string { return `${str}` } +export const highlighterGroups = `$1$2` + export function escapeHTML(html: string): string { return html .replaceAll('&', '&') @@ -78,12 +81,16 @@ export function getAllIndices(text: string, regex: RegExp): SearchMatch[] { */ export function stringsToRegex(strings: string[]): RegExp { if (!strings.length) return /^$/g - // \\b is "word boundary", and is not applied if the user uses the cm-chs-patch plugin - const joined = strings - .map(s => (getChsSegmenter() ? '' : '\\b') + escapeRegex(s)) - .join('|') - const reg = new RegExp(`(${joined})`, 'gi') - // console.log(reg) + // Default word split is not applied if the user uses the cm-chs-patch plugin + const joined = + '(' + + (getChsSegmenter() ? '' : SPACE_OR_PUNCTUATION.source) + + ')' + + '(' + + strings.map(s => escapeRegex(s)).join('|') + + ')' + + const reg = new RegExp(`${joined}`, 'giu') return reg } @@ -256,7 +263,7 @@ export function isFileIndexable(path: string): boolean { export function isFileImage(path: string): boolean { const ext = getExtension(path) - return (ext === 'png' || ext === 'jpg' || ext === 'jpeg') + return ext === 'png' || ext === 'jpg' || ext === 'jpeg' } export function isFilePDF(path: string): boolean {