#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>`
}
export function highlighterGroups(...args: any[]) {
if (
args[1] !== null &&
args[1] !== undefined &&
args[2] !== null &&
args[2] !== undefined
)
return `<span>${args[1]}</span><span class="${highlightClass}">${args[2]}</span>`
export function highlighterGroups(substring: string, ...args: any[]) {
// args[0] is the single char preceding args[1], which is the word we want to highlight
if (!!args[1].trim())
return `<span>${args[0]}</span><span class="${highlightClass}">${args[1]}</span>`
return '&lt;no content&gt;'
}
@@ -91,15 +87,20 @@ export function getAllIndices(text: string, regex: RegExp): SearchMatch[] {
*/
export function stringsToRegex(strings: string[]): RegExp {
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 =
'(' +
// Default word split is not applied if the user uses the cm-chs-patch plugin
(getChsSegmenter()
? ''
: // 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
? `^|${SPACE_OR_PUNCTUATION.source}|[A-Z]`
: `^|${SPACE_OR_PUNCTUATION.source}`) +
? `^|${SPACE_OR_PUNCTUATION.source}|\-|[A-Z]`
: `^|${SPACE_OR_PUNCTUATION.source}|\-`) +
')' +
`(${strings.map(s => escapeRegex(s)).join('|')})`