Skip to content

Commit 6382d1a

Browse files
committed
fix(whatsapp): honor escaped multiline code delimiter runs
1 parent 228234e commit 6382d1a

3 files changed

Lines changed: 36 additions & 9 deletions

File tree

‎extensions/whatsapp/src/inbound/outbound-mentions.test.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ describe("resolveWhatsAppOutboundMentions", () => {
1616
});
1717
});
1818

19-
it("rewrites phone-number tokens to LID mention text without device suffixes", () => {
19+
it.each([
20+
{ text: "ping @+5511976136970", expected: "ping @277038292303944" },
21+
{
22+
text: "literal \\` ping @+5511976136970",
23+
expected: "literal \\` ping @277038292303944",
24+
},
25+
{
26+
text: "literal \\\\\\` ping @+5511976136970",
27+
expected: "literal \\\\\\` ping @277038292303944",
28+
},
29+
{
30+
text: "inside ``notify ` @+5511976136970`` then @+5511976136970",
31+
expected: "inside ``notify ` @+5511976136970`` then @277038292303944",
32+
},
33+
])("rewrites visible phone-number mentions to LIDs: $text", ({ text, expected }) => {
2034
expect(
2135
resolveWhatsAppOutboundMentions({
2236
chatJid: "[email protected]",
23-
text: "ping @+5511976136970",
37+
text,
2438
participants: [
2539
{
2640
id: "277038292303944:2@lid",
@@ -29,7 +43,7 @@ describe("resolveWhatsAppOutboundMentions", () => {
2943
],
3044
}),
3145
).toEqual({
32-
text: "ping @277038292303944",
46+
text: expected,
3347
mentionedJids: ["277038292303944@lid"],
3448
});
3549
});
@@ -115,8 +129,14 @@ describe("resolveWhatsAppOutboundMentions", () => {
115129
});
116130
});
117131

118-
it.each(["Run `notify @+5511976136970", "Run `notify\n@+5511976136970"])(
119-
"does not rewrite or mention phone numbers inside unterminated inline code: %j",
132+
it.each([
133+
"Run `notify @+5511976136970",
134+
"Run `notify\n@+5511976136970",
135+
"Run ``notify ` @+5511976136970",
136+
"Run ``notify ` @+5511976136970``",
137+
"literal \\\\` inside @+5511976136970",
138+
])(
139+
"does not rewrite or mention phone numbers inside balanced or unterminated inline code: %j",
120140
(text) => {
121141
expect(
122142
resolveWhatsAppOutboundMentions({

‎extensions/whatsapp/src/inbound/outbound-mentions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export type WhatsAppOutboundMentionResolution = {
1616
};
1717

1818
const CODE_FENCE_RE = /```[\s\S]*?```/g;
19-
const INLINE_CODE_RE = /`[^`]+(?:`|$)/g;
19+
const INLINE_CODE_RE = /(?<=(?:^|[^\\])(?:\\\\)*)(`+)[\s\S]*?(?:(?<!`)\1(?!`)|$)/g;
2020
const OUTBOUND_MENTION_RE = /@(\+?\d+)/g;
2121
const KNOWN_USER_JID_RE = /^(\d+)(?::\d+)?@(s\.whatsapp\.net|hosted|lid|hosted\.lid|c\.us)$/i;
2222
const PHONE_JID_DOMAIN_RE = /^(s\.whatsapp\.net|hosted|c\.us)$/i;

‎extensions/whatsapp/src/inbound/send-api.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,15 @@ describe("createWebSendApi", () => {
330330
});
331331
});
332332

333-
it.each(["Run `notify @15551234567", "Run `notify\n@15551234567"])(
334-
"does not send native mentions for phone numbers inside unterminated inline code: %j",
335-
async (messageText) => {
333+
it.each([
334+
{ messageText: "Run `notify @15551234567" },
335+
{ messageText: "Run `notify\n@15551234567" },
336+
{ messageText: "Run ``notify ` @15551234567" },
337+
{ messageText: "literal \\` ping @15551234567", nativeMention: true },
338+
{ messageText: "literal \\\\` inside @15551234567" },
339+
])(
340+
"only sends native mentions for visible phone numbers outside inline code: $messageText",
341+
async ({ messageText, nativeMention }) => {
336342
api = createWebSendApi({
337343
sock: { sendMessage, sendPresenceUpdate },
338344
defaultAccountId: "main",
@@ -348,6 +354,7 @@ describe("createWebSendApi", () => {
348354

349355
expect(sendMessage).toHaveBeenCalledWith("[email protected]", {
350356
text: messageText,
357+
...(nativeMention ? { mentions: ["[email protected]"] } : {}),
351358
});
352359
},
353360
);

0 commit comments

Comments
 (0)