#195 - Fixed highlighting for cyrillic chars

This commit is contained in:
Simon Cambier
2023-02-19 14:03:16 +01:00
parent ae08608e02
commit c70e446e84
3 changed files with 30 additions and 16 deletions

View File

@@ -1,6 +1,10 @@
<script lang="ts"> <script lang="ts">
import type { ResultNote } from '../globals' import type { ResultNote } from '../globals'
import { highlighter, makeExcerpt, stringsToRegex } from '../tools/utils' import {
highlighterGroups,
makeExcerpt,
stringsToRegex,
} from '../tools/utils'
import ResultItemContainer from './ResultItemContainer.svelte' import ResultItemContainer from './ResultItemContainer.svelte'
export let offset: number export let offset: number
@@ -12,8 +16,12 @@
$: cleanedContent = makeExcerpt(note?.content ?? '', offset) $: cleanedContent = makeExcerpt(note?.content ?? '', offset)
</script> </script>
<ResultItemContainer id={index.toString()} {selected} on:mousemove on:click> <ResultItemContainer
id="{index.toString()}"
selected="{selected}"
on:mousemove
on:click>
<div class="omnisearch-result__body"> <div class="omnisearch-result__body">
{@html cleanedContent.replace(reg, highlighter)} {@html cleanedContent.replace(reg, highlighterGroups)}
</div> </div>
</ResultItemContainer> </ResultItemContainer>

View File

@@ -3,18 +3,16 @@
import type { ResultNote } from '../globals' import type { ResultNote } from '../globals'
import { import {
getExtension, getExtension,
highlighter, highlighterGroups,
isFileCanvas, isFileCanvas,
isFileImage, isFileImage,
isFilePDF, isFilePDF,
isFilePlaintext,
makeExcerpt, makeExcerpt,
pathWithoutFilename, pathWithoutFilename,
removeDiacritics, removeDiacritics,
stringsToRegex, stringsToRegex,
} from '../tools/utils' } from '../tools/utils'
import ResultItemContainer from './ResultItemContainer.svelte' import ResultItemContainer from './ResultItemContainer.svelte'
import { onMount } from 'svelte'
import { setIcon } from 'obsidian' import { setIcon } from 'obsidian'
export let selected = false export let selected = false
@@ -54,7 +52,8 @@
if (elFilePathIcon) { if (elFilePathIcon) {
if (isFileImage(note.path)) setIcon(elFilePathIcon, 'image') if (isFileImage(note.path)) setIcon(elFilePathIcon, 'image')
else if (isFilePDF(note.path)) setIcon(elFilePathIcon, 'file-text') 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') else setIcon(elFilePathIcon, 'file')
} }
} }
@@ -70,7 +69,7 @@
<div class="omnisearch-result__title-container"> <div class="omnisearch-result__title-container">
<span class="omnisearch-result__title"> <span class="omnisearch-result__title">
<span bind:this="{elFilePathIcon}"></span> <span bind:this="{elFilePathIcon}"></span>
<span>{@html title.replace(reg, highlighter)}</span> <span>{@html title.replace(reg, highlighterGroups)}</span>
<span class="omnisearch-result__extension" <span class="omnisearch-result__extension"
>.{getExtension(note.path)}</span> >.{getExtension(note.path)}</span>
@@ -96,7 +95,7 @@
<div style="display: flex; flex-direction: row;"> <div style="display: flex; flex-direction: row;">
{#if $showExcerpt} {#if $showExcerpt}
<div class="omnisearch-result__body"> <div class="omnisearch-result__body">
{@html cleanedContent.replace(reg, highlighter)} {@html cleanedContent.replace(reg, highlighterGroups)}
</div> </div>
{/if} {/if}

View File

@@ -15,6 +15,7 @@ import {
regexLineSplit, regexLineSplit,
regexStripQuotes, regexStripQuotes,
regexYaml, regexYaml,
SPACE_OR_PUNCTUATION,
type SearchMatch, type SearchMatch,
} from '../globals' } from '../globals'
import { settings } from '../settings' import { settings } from '../settings'
@@ -25,6 +26,8 @@ export function highlighter(str: string): string {
return `<span class="${highlightClass}">${str}</span>` return `<span class="${highlightClass}">${str}</span>`
} }
export const highlighterGroups = `$1<span class="${highlightClass}">$2</span>`
export function escapeHTML(html: string): string { export function escapeHTML(html: string): string {
return html return html
.replaceAll('&', '&amp;') .replaceAll('&', '&amp;')
@@ -78,12 +81,16 @@ export function getAllIndices(text: string, regex: RegExp): SearchMatch[] {
*/ */
export function stringsToRegex(strings: string[]): RegExp { export function stringsToRegex(strings: string[]): RegExp {
if (!strings.length) return /^$/g if (!strings.length) return /^$/g
// \\b is "word boundary", and is not applied if the user uses the cm-chs-patch plugin // Default word split is not applied if the user uses the cm-chs-patch plugin
const joined = strings const joined =
.map(s => (getChsSegmenter() ? '' : '\\b') + escapeRegex(s)) '(' +
.join('|') (getChsSegmenter() ? '' : SPACE_OR_PUNCTUATION.source) +
const reg = new RegExp(`(${joined})`, 'gi') ')' +
// console.log(reg) '(' +
strings.map(s => escapeRegex(s)).join('|') +
')'
const reg = new RegExp(`${joined}`, 'giu')
return reg return reg
} }
@@ -256,7 +263,7 @@ export function isFileIndexable(path: string): boolean {
export function isFileImage(path: string): boolean { export function isFileImage(path: string): boolean {
const ext = getExtension(path) const ext = getExtension(path)
return (ext === 'png' || ext === 'jpg' || ext === 'jpeg') return ext === 'png' || ext === 'jpg' || ext === 'jpeg'
} }
export function isFilePDF(path: string): boolean { export function isFilePDF(path: string): boolean {