build regexp only once

This commit is contained in:
Athou
2025-03-02 15:16:53 +01:00
parent 1dcf76fc0a
commit 937e7353ce

View File

@@ -67,16 +67,15 @@ const transform: TransformCallback = node => {
} }
class HighlightMatcher extends Matcher { class HighlightMatcher extends Matcher {
private readonly search: string private readonly regexp: RegExp
constructor(search: string) { constructor(search: string) {
super("highlight") super("highlight")
this.search = escapeStringRegexp(search) this.regexp = new RegExp(escapeStringRegexp(search).split(" ").join("|"), "i")
} }
match(string: string): MatchResponse<unknown> | null { match(string: string): MatchResponse<unknown> | null {
const pattern = this.search.split(" ").join("|") return this.doMatch(string, this.regexp, () => ({}))
return this.doMatch(string, new RegExp(pattern, "i"), () => ({}))
} }
replaceWith(children: ChildrenNode): Node { replaceWith(children: ChildrenNode): Node {