diff --git a/src/tools/utils.ts b/src/tools/utils.ts index 719d308..19699d1 100644 --- a/src/tools/utils.ts +++ b/src/tools/utils.ts @@ -26,14 +26,10 @@ export function highlighter(str: string): string { return `${str}` } -export function highlighterGroups(...args: any[]) { - if ( - args[1] !== null && - args[1] !== undefined && - args[2] !== null && - args[2] !== undefined - ) - return `${args[1]}${args[2]}` +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 `${args[0]}${args[1]}` return '<no content>' } @@ -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('|')})`