From 006ff07a0c62be368af0e7f8b9653a8aea7c6496 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 18 Jul 2023 15:57:54 +0200 Subject: [PATCH] [mastodon-client] improve mention regexes --- packages/megalodon/src/misskey.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/megalodon/src/misskey.ts b/packages/megalodon/src/misskey.ts index ca418439e..5e24e49f8 100644 --- a/packages/megalodon/src/misskey.ts +++ b/packages/megalodon/src/misskey.ts @@ -1280,8 +1280,8 @@ export default class Misskey implements MegalodonInterface { status.mentions = (await this.getMentions(status.plain_content!, cache)).filter(p => p != null); for (const m of status.mentions.filter((value, index, array) => array.indexOf(value) === index)) { if (m.acct == m.username) - status.content = status.content.replace(`@${m.acct}@${this.baseUrlToHost(this.baseUrl)}`, `@${m.acct}`); - status.content = status.content.replace(`@${m.acct}`, `@${m.acct}`); + status.content = status.content.replace(new RegExp(`(?<=^|\\s|>)@${m.acct}@${this.baseUrlToHost(this.baseUrl)}(?=[^a-zA-Z0-9]|$)`, 'g'), `@${m.acct}`); + status.content = status.content.replace(new RegExp(`(?<=^|\\s|>)@${m.acct}(?=[^a-zA-Z0-9]|$)`, 'g'), `@${m.acct}`); } return status; } @@ -1292,7 +1292,7 @@ export default class Misskey implements MegalodonInterface { if (text == undefined) return mentions; - const mentionMatch = text.matchAll(/(?<=^|\s)@(?.*?)(?:@(?.*?)|)(?=\s|$)/g); + const mentionMatch = text.matchAll(/(?<=^|\s)@(?[a-zA-Z0-9_]+)(?:@(?[a-zA-Z0-9-.]+\.[a-zA-Z0-9-]+)|)(?=[^a-zA-Z0-9]|$)/g,); for (const m of mentionMatch) { try { @@ -2489,7 +2489,7 @@ export default class Misskey implements MegalodonInterface { } try { - const match = q.match(/^@(?.*?)(?:@(?.*?)|)$/); + const match = q.match(/^@(?[a-zA-Z0-9_]+)(?:@(?[a-zA-Z0-9-.]+\.[a-zA-Z0-9-]+)|)$/); if (match) { const lookupQuery = { username: match.groups?.user,