Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep a URL with _@digits or (@digits intact

Yudhi Armyndharisa867a7a7f9c8
diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.ts
index 43e988444..a73271608 100644
--- a/dashboard/src/utils/chatMessages.test.ts
+++ b/dashboard/src/utils/chatMessages.test.ts
@@ -609,3 +609,12 @@ test('a push name carrying the span delimiters cannot close its own mention earl
]);
assert.equal(resolveMentions('hi @6281112345', names), `hi ${wrap('@Xbit.ly/free')}`);
});
+
+test('resolveMentions does not fire after "_" or "(" inside a URL, only when the opener run follows whitespace', () => {
+ const names = buildMentionNameMap([msg({ author: '[email protected]', chatName: 'Ann' })]);
+ for (const body of ['https://x.example/p_@6281234567/doc', 'https://x.example/wiki/A_(@6281234567)']) {
+ assert.equal(resolveMentions(body, names), body);
+ }
+ assert.equal(resolveMentions('hi _(@6281234567)_', names), `hi _(${wrap('@Ann')})_`);
+ assert.equal(resolveMentions('`x`*@6281234567*', names), `\`x\`*${wrap('@Ann')}*`);
+});
diff --git a/dashboard/src/utils/chatMessages.ts b/dashboard/src/utils/chatMessages.ts
index 00e4eb678..8de53ba03 100644
--- a/dashboard/src/utils/chatMessages.ts
+++ b/dashboard/src/utils/chatMessages.ts
@@ -147,9 +147,10 @@ function blankMentionName(name: string): boolean {
return name.replace(INVISIBLE, '').trim() === '';
}
-// The left boundary is the start of the text, whitespace, opening punctuation, or a format opener
-// (`*_~`), so `*@digits*` resolves into a bold mention the way WhatsApp renders it.
-const MENTION_TOKEN = /(^|[\s([{"'*_~])@(\d{7,})/gu;
+// The left boundary is the start of the text or whitespace, optionally followed by a run of opening
+// punctuation or format openers (`*_~`), so `*@digits*` resolves into a bold mention the way
+// WhatsApp renders it while a `_@digits` or `(@digits` inside a URL does not.
+const MENTION_TOKEN = /((?:^|\s)[([{"'*_~]*)@(\d{7,})/gu;
// parseMessageBody peels code after this runs, so a mention inside a code span or block is left as
// digits here: rewriting it would split the span and render its backticks literally.
@@ -169,10 +170,10 @@ const CODE_SEGMENT = /(```[\s\S]*?```|`[^`]*`)/;
* clickable link (character-stripping alone does not stop linkify-react auto-linking a bare word
* like "localhost").
*
- * The left boundary only fires at the start of the text, after whitespace, or after opening
- * punctuation — not after `/` or a backtick — so a URL path segment or an inline-code span with
- * the same digits is left untouched (this runs on the raw body, before parseMessageBody splits
- * out code spans).
+ * The left boundary only fires at the start of the text or after whitespace, optionally through
+ * a run of opening punctuation or format markers. It never fires after `/`, a backtick, or a word,
+ * so a URL or an inline-code span with the same digits is left untouched (this runs on the raw
+ * body, before parseMessageBody splits out code spans).
*/
export function resolveMentions(text: string, names: Map<string, string>): string {
if (names.size === 0 || !text.includes('@')) return text;