Skip to content

Commit e201416

Browse files
committed
fix(feishu): replace longer mention keys first
1 parent a96418c commit e201416

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Feishu mention normalization tests cover prefix collisions between non-zero-padded keys.
2+
import { describe, expect, it } from "vitest";
3+
import { normalizeMentions } from "./bot-content.js";
4+
5+
describe("normalizeMentions", () => {
6+
it("does not let a shorter mention key match inside a longer prefix-sharing key", () => {
7+
const result = normalizeMentions("Standup: @_user_1 @_user_10 @_user_11 done", [
8+
{ key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } },
9+
{ key: "@_user_10", name: "Judy", id: { open_id: "ou_judy" } },
10+
{ key: "@_user_11", name: "Kevin", id: { open_id: "ou_kevin" } },
11+
]);
12+
expect(result).toBe(
13+
'Standup: <at user_id="ou_alice">Alice</at> <at user_id="ou_judy">Judy</at> <at user_id="ou_kevin">Kevin</at> done',
14+
);
15+
});
16+
17+
it("keeps normalizing a listed mention when text follows without whitespace", () => {
18+
const result = normalizeMentions("@_user_1thanks", [
19+
{ key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } },
20+
]);
21+
expect(result).toBe('<at user_id="ou_alice">Alice</at>thanks');
22+
});
23+
});

‎extensions/feishu/src/bot-content.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ export function normalizeMentions(
278278
const escaped = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
279279
const escapeName = (value: string) => value.replace(/</g, "&lt;").replace(/>/g, "&gt;");
280280
let result = text;
281-
for (const mention of mentions) {
281+
// Feishu keys are not zero-padded, so @_user_1 is a prefix of @_user_10.
282+
// Replace longer keys first while still trusting the mentions array as the source of truth.
283+
const orderedMentions = mentions.toSorted((a, b) => b.key.length - a.key.length);
284+
for (const mention of orderedMentions) {
282285
const mentionId = mention.id.open_id;
283286
const replacement =
284287
botStripId && mentionId === botStripId

0 commit comments

Comments
 (0)