Merge pull request #172 from cloudflare/regex-refactor

simplify enrichStatus method by using regexes
pull/175/head
Sven Sauleau 2023-02-02 11:26:01 +00:00 zatwierdzone przez GitHub
commit a11567df8e
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 31 dodań i 59 usunięć

Wyświetl plik

@ -9,66 +9,38 @@ function tag(name: string, content: string, attrs: Record<string, string> = {}):
return `<${name}${htmlAttrs}>${content}</${name}>`
}
const mentionedEmailRegex = /\s@([^@\s]+@[^.\s]+\.[^.\s]+)\s/g
const linkRegex = /\s(https?:\/\/[^.\s]+\.[^.\s]+(?:\/[^.\s/]+)*)\s/g
/// Transform a text status into a HTML status; enriching it with links / mentions.
export function enrichStatus(status: string): string {
let out = ''
let state = 'normal'
let buffer = ''
const enrichedStatus = status
.replace(mentionedEmailRegex, (_, email: string) => ` ${getMentionSpan(email)} `)
.replace(linkRegex, (_, link: string) => ` ${getLinkAnchor(link)} `)
for (let i = 0, len = status.length; i < len; i++) {
const char = status[i]
if (char === '@') {
state = 'mention'
}
if (status.slice(i, i + 5) === 'http:' || status.slice(i, i + 6) === 'https:') {
state = 'link'
}
if (state === 'link') {
if (char === ' ') {
try {
const url = new URL(buffer)
buffer = ''
out += tag('a', url.hostname + url.pathname, { href: url.href })
} catch (err: unknown) {
console.warn('failed to parse link', err)
out += buffer
}
out += char
state = 'normal'
} else {
buffer += char
}
continue
}
if (state === 'mention') {
if (char === ' ') {
const handle = parseHandle(buffer)
buffer = ''
// TODO: the link to the profile is a guess, we could rely on
// the cached Actors to find the right link.
const linkToProfile = `https://${handle.domain}/@${handle.localPart}`
const mention = '@' + tag('span', handle.localPart)
out += tag('span', tag('a', mention, { href: linkToProfile, class: 'u-url mention' }), { class: 'h-card' })
out += char
state = 'normal'
} else {
buffer += char
}
continue
}
if (state === 'normal') {
out += char
continue
}
}
return tag('p', out)
return tag('p', enrichedStatus)
}
function getMentionSpan(mentionedEmail: string) {
const handle = parseHandle(mentionedEmail)
// TODO: the link to the profile is a guess, we could rely on
// the cached Actors to find the right link.
const linkToProfile = `https://${handle.domain}/@${handle.localPart}`
const mention = `@${tag('span', handle.localPart)}`
return tag('span', tag('a', mention, { href: linkToProfile, class: 'u-url mention' }), {
class: 'h-card',
})
}
function getLinkAnchor(link: string) {
try {
const url = new URL(link)
return tag('a', url.hostname + url.pathname, { href: url.href })
} catch (err: unknown) {
console.warn('failed to parse link', err)
return link
}
}