#225 - improved highlighting

This commit is contained in:
Simon Cambier
2023-06-17 19:18:30 +02:00
parent 887dc669f3
commit 22fa98a44b

View File

@@ -26,14 +26,10 @@ export function highlighter(str: string): string {
return `<span class="${highlightClass}">${str}</span>` return `<span class="${highlightClass}">${str}</span>`
} }
export function highlighterGroups(...args: any[]) { export function highlighterGroups(substring: string, ...args: any[]) {
if ( // args[0] is the single char preceding args[1], which is the word we want to highlight
args[1] !== null && if (!!args[1].trim())
args[1] !== undefined && return `<span>${args[0]}</span><span class="${highlightClass}">${args[1]}</span>`
args[2] !== null &&
args[2] !== undefined
)
return `<span>${args[1]}</span><span class="${highlightClass}">${args[2]}</span>`
return '&lt;no content&gt;' return '&lt;no content&gt;'
} }
@@ -91,15 +87,20 @@ 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
// sort strings by decreasing length, so that longer strings are matched first
strings.sort((a, b) => b.length - a.length)
const joined = const joined =
'(' + '(' +
// Default word split 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
(getChsSegmenter() (getChsSegmenter()
? '' ? ''
: // Split on start of line, spaces, punctuation, or capital letters (for camelCase) : // Split on start of line, spaces, punctuation, or capital letters (for camelCase)
// We also add the hyphen to the list of characters that can split words
settings.splitCamelCase settings.splitCamelCase
? `^|${SPACE_OR_PUNCTUATION.source}|[A-Z]` ? `^|${SPACE_OR_PUNCTUATION.source}|\-|[A-Z]`
: `^|${SPACE_OR_PUNCTUATION.source}`) + : `^|${SPACE_OR_PUNCTUATION.source}|\-`) +
')' + ')' +
`(${strings.map(s => escapeRegex(s)).join('|')})` `(${strings.map(s => escapeRegex(s)).join('|')})`