#452 - Actually let's remove all that code
There is certainly a reason it was so complicated, but it has always been buggy anyway.
This commit is contained in:
@@ -23,55 +23,13 @@ export class TextProcessor {
|
|||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// Text to highlight
|
return text.replace(
|
||||||
const smartMatches = new RegExp(
|
|
||||||
matches
|
|
||||||
.map(
|
|
||||||
// This regex will match the word (with \b word boundary)
|
|
||||||
// \b doesn't detect non-alphabetical character's word boundary, so we need to escape it
|
|
||||||
matchItem => {
|
|
||||||
const escaped = escapeRegExp(matchItem.match)
|
|
||||||
return `\\b${escaped}\\b${
|
|
||||||
!/[a-zA-Z]/.test(matchItem.match) ? `|${escaped}` : ''
|
|
||||||
}`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.join('|'),
|
|
||||||
'giu'
|
|
||||||
)
|
|
||||||
|
|
||||||
// Replacer function that will highlight the matches
|
|
||||||
const replacer = (match: string) => {
|
|
||||||
const matchInfo = matches.find(info =>
|
|
||||||
match.match(
|
|
||||||
new RegExp(
|
new RegExp(
|
||||||
`\\b${escapeRegExp(info.match)}\\b${
|
`(${matches.map(item => escapeRegExp(item.match)).join('|')})`,
|
||||||
!/[a-zA-Z]/.test(info.match)
|
|
||||||
? `|${escapeRegExp(info.match)}`
|
|
||||||
: ''
|
|
||||||
}`,
|
|
||||||
'giu'
|
'giu'
|
||||||
|
),
|
||||||
|
`<span class="${highlightClass}">$1</span>`
|
||||||
)
|
)
|
||||||
)
|
|
||||||
)
|
|
||||||
if (matchInfo) {
|
|
||||||
return `<span class="${highlightClass}">${match}</span>`
|
|
||||||
}
|
|
||||||
return match
|
|
||||||
}
|
|
||||||
|
|
||||||
// Effectively highlight the text
|
|
||||||
let newText = text.replace(smartMatches, replacer)
|
|
||||||
|
|
||||||
// If the text didn't change (= nothing to highlight), re-run the regex but just replace the matches without the word boundary
|
|
||||||
if (newText === text) {
|
|
||||||
const dumbMatches = new RegExp(
|
|
||||||
matches.map(matchItem => escapeRegExp(matchItem.match)).join('|'),
|
|
||||||
'giu'
|
|
||||||
)
|
|
||||||
newText = text.replace(dumbMatches, replacer)
|
|
||||||
}
|
|
||||||
return newText
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Omnisearch - Error in highlightText()', e)
|
console.error('Omnisearch - Error in highlightText()', e)
|
||||||
return text
|
return text
|
||||||
@@ -101,7 +59,11 @@ export class TextProcessor {
|
|||||||
* @param reg
|
* @param reg
|
||||||
* @param query
|
* @param query
|
||||||
*/
|
*/
|
||||||
public getMatches(text: string, words: string[], query?: Query): SearchMatch[] {
|
public getMatches(
|
||||||
|
text: string,
|
||||||
|
words: string[],
|
||||||
|
query?: Query
|
||||||
|
): SearchMatch[] {
|
||||||
words = words.map(escapeHTML)
|
words = words.map(escapeHTML)
|
||||||
const reg = this.stringsToRegex(words)
|
const reg = this.stringsToRegex(words)
|
||||||
const originalText = text
|
const originalText = text
|
||||||
|
|||||||
Reference in New Issue
Block a user