From 7c3353e90ea73d40cbb868c64d9072476cfee21a Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 6 Feb 2023 23:16:38 +0000 Subject: [PATCH] handle sub-domains correctly in enrichStatus --- backend/src/mastodon/microformats.ts | 2 +- backend/test/mastodon.spec.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/src/mastodon/microformats.ts b/backend/src/mastodon/microformats.ts index 4c40254..e80ffc8 100644 --- a/backend/src/mastodon/microformats.ts +++ b/backend/src/mastodon/microformats.ts @@ -10,7 +10,7 @@ function tag(name: string, content: string, attrs: Record = {}): } const linkRegex = /(^|\s|\b)(https?:\/\/[^.\s]+\.[^.\s]+(?:\/[^.\s/]+)*)(\b|\s|$)/g -const mentionedEmailRegex = /(^|\s|\b)@([^@\s\W]+@[^.\W\s]+\.[^.\s]+)(\b|\s|$)/g +const mentionedEmailRegex = /(^|\s|\b)@(\w+(?:[.-]?\w+)+@\w+(?:[.-]?\w+)+(?:\.\w{2,3})+)(\b|\s|$)/g /// Transform a text status into a HTML status; enriching it with links / mentions. export function enrichStatus(status: string): string { diff --git a/backend/test/mastodon.spec.ts b/backend/test/mastodon.spec.ts index ea43261..b20afe5 100644 --- a/backend/test/mastodon.spec.ts +++ b/backend/test/mastodon.spec.ts @@ -309,6 +309,21 @@ describe('Mastodon APIs', () => { enrichStatus('hello @test@example.com'), '

hello @test

' ) + + assert.equal( + enrichStatus('hello @test@example.eng.com'), + '

hello @test

' + ) + + assert.equal( + enrichStatus('hello @test@example.eng.com!!!'), + '

hello @test!!!

' + ) + + assert.equal( + enrichStatus('hi @test.a.b.c-d@example.eng.co.uk.....'), + '

hi @test.a.b.c-d.....

' + ) }) test('handle invalid mention', () => {