refactor(feishu): dedupe mention regex escaping

This commit is contained in:
Peter Steinberger
2026-02-19 15:04:40 +01:00
parent b54ba3391b
commit f4b288b8f7
3 changed files with 58 additions and 12 deletions

View File

@@ -1,5 +1,12 @@
import type { FeishuMessageEvent } from "./bot.js";
/**
* Escape regex metacharacters so user-controlled mention fields are treated literally.
*/
export function escapeRegExp(input: string): string {
return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
/**
* Mention target user info
*/
@@ -67,7 +74,7 @@ export function extractMessageBody(text: string, allMentionKeys: string[]): stri
// Remove all @ placeholders
for (const key of allMentionKeys) {
result = result.replace(new RegExp(key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"), "");
result = result.replace(new RegExp(escapeRegExp(key), "g"), "");
}
return result.replace(/\s+/g, " ").trim();