Merge pull request #249 from cloudflare/improve-mentions

Improve mentions
pull/251/head
Sven Sauleau 2023-02-10 14:03:33 +00:00 zatwierdzone przez GitHub
commit e264982d27
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 36 dodań i 40 usunięć

Wyświetl plik

@ -10,7 +10,7 @@ function tag(name: string, content: string, attrs: Record<string, string> = {}):
}
const linkRegex = /(^|\s|\b)(https?:\/\/[-\w@:%._+~#=]{2,256}\.[a-z]{2,6}\b(?:[-\w@:%_+.~#?&/=]*))(\b|\s|$)/g
const mentionedEmailRegex = /(^|\s|\b)@(\w+(?:[.-]?\w+)+@\w+(?:[.-]?\w+)+(?:\.\w{2,3})+)(\b|\s|$)/g
const mentionedEmailRegex = /(^|\s|\b|\W)@(\w+(?:[.-]?\w+)+@\w+(?:[.-]?\w+)+(?:\.\w{2,63})+)(\b|\s|$)/g
/// Transform a text status into a HTML status; enriching it with links / mentions.
export function enrichStatus(status: string): string {

Wyświetl plik

@ -285,45 +285,41 @@ describe('Mastodon APIs', () => {
describe('Microformats', () => {
test('convert mentions to HTML', () => {
assert.equal(
enrichStatus('@sven2@example.com hein'),
'<p><span class="h-card"><a href="https://example.com/@sven2" class="u-url mention">@<span>sven2</span></a></span> hein</p>'
)
assert.equal(
enrichStatus(' @sven2@example.com hein'),
'<p> <span class="h-card"><a href="https://example.com/@sven2" class="u-url mention">@<span>sven2</span></a></span> hein</p>'
)
assert.equal(
enrichStatus('@sven2@example.com\n\thein'),
'<p><span class="h-card"><a href="https://example.com/@sven2" class="u-url mention">@<span>sven2</span></a></span>\n\thein</p>'
)
assert.equal(
enrichStatus('hey @test@example.com hi'),
'<p>hey <span class="h-card"><a href="https://example.com/@test" class="u-url mention">@<span>test</span></a></span> hi</p>'
)
assert.equal(
enrichStatus('hello @test@example.com'),
'<p>hello <span class="h-card"><a href="https://example.com/@test" class="u-url mention">@<span>test</span></a></span></p>'
)
assert.equal(
enrichStatus('hello @test@example.eng.com'),
'<p>hello <span class="h-card"><a href="https://example.eng.com/@test" class="u-url mention">@<span>test</span></a></span></p>'
)
assert.equal(
enrichStatus('hello @test@example.eng.com!!!'),
'<p>hello <span class="h-card"><a href="https://example.eng.com/@test" class="u-url mention">@<span>test</span></a></span>!!!</p>'
)
assert.equal(
enrichStatus('hi @test.a.b.c-d@example.eng.co.uk.....'),
'<p>hi <span class="h-card"><a href="https://example.eng.co.uk/@test.a.b.c-d" class="u-url mention">@<span>test.a.b.c-d</span></a></span>.....</p>'
)
const mentionsToTest = [
{
mention: '@sven2@example.com',
expectedMentionSpan:
'<span class="h-card"><a href="https://example.com/@sven2" class="u-url mention">@<span>sven2</span></a></span>',
},
{
mention: '@test@example.eng.com',
expectedMentionSpan:
'<span class="h-card"><a href="https://example.eng.com/@test" class="u-url mention">@<span>test</span></a></span>',
},
{
mention: '@test.a.b.c-d@example.eng.co.uk',
expectedMentionSpan:
'<span class="h-card"><a href="https://example.eng.co.uk/@test.a.b.c-d" class="u-url mention">@<span>test.a.b.c-d</span></a></span>',
},
{
mention: '@testey@123456.abcdef',
expectedMentionSpan:
'<span class="h-card"><a href="https://123456.abcdef/@testey" class="u-url mention">@<span>testey</span></a></span>',
},
{
mention: '@testey@123456.test.testey.abcdef',
expectedMentionSpan:
'<span class="h-card"><a href="https://123456.test.testey.abcdef/@testey" class="u-url mention">@<span>testey</span></a></span>',
},
]
mentionsToTest.forEach(({ mention, expectedMentionSpan }) => {
assert.equal(enrichStatus(`hey ${mention} hi`), `<p>hey ${expectedMentionSpan} hi</p>`)
assert.equal(enrichStatus(`${mention} hi`), `<p>${expectedMentionSpan} hi</p>`)
assert.equal(enrichStatus(`${mention}\n\thein`), `<p>${expectedMentionSpan}\n\thein</p>`)
assert.equal(enrichStatus(`hey ${mention}`), `<p>hey ${expectedMentionSpan}</p>`)
assert.equal(enrichStatus(`${mention}`), `<p>${expectedMentionSpan}</p>`)
assert.equal(enrichStatus(`@!@£${mention}!!!`), `<p>@!@£${expectedMentionSpan}!!!</p>`)
})
})
test('handle invalid mention', () => {