kopia lustrzana https://github.com/cloudflare/wildebeest
Revert "simplify enrichStatus method by using regexes"
This reverts commit 4644405add
.
pull/196/head
rodzic
179988927d
commit
c9c87b4209
|
@ -9,38 +9,66 @@ 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 {
|
||||
const enrichedStatus = status
|
||||
.replace(mentionedEmailRegex, (_, email: string) => ` ${getMentionSpan(email)} `)
|
||||
.replace(linkRegex, (_, link: string) => ` ${getLinkAnchor(link)} `)
|
||||
let out = ''
|
||||
let state = 'normal'
|
||||
let buffer = ''
|
||||
|
||||
return tag('p', enrichedStatus)
|
||||
}
|
||||
for (let i = 0, len = status.length; i < len; i++) {
|
||||
const char = status[i]
|
||||
if (char === '@') {
|
||||
state = 'mention'
|
||||
}
|
||||
|
||||
function getMentionSpan(mentionedEmail: string) {
|
||||
const handle = parseHandle(mentionedEmail)
|
||||
if (status.slice(i, i + 5) === 'http:' || status.slice(i, i + 6) === 'https:') {
|
||||
state = 'link'
|
||||
}
|
||||
|
||||
// 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}`
|
||||
if (state === 'link') {
|
||||
if (char === ' ') {
|
||||
try {
|
||||
const url = new URL(buffer)
|
||||
buffer = ''
|
||||
|
||||
const mention = `@${tag('span', handle.localPart)}`
|
||||
return tag('span', tag('a', mention, { href: linkToProfile, class: 'u-url mention' }), {
|
||||
class: 'h-card',
|
||||
})
|
||||
}
|
||||
out += tag('a', url.hostname + url.pathname, { href: url.href })
|
||||
} catch (err: unknown) {
|
||||
console.warn('failed to parse link', err)
|
||||
out += buffer
|
||||
}
|
||||
|
||||
function getLinkAnchor(link: string) {
|
||||
try {
|
||||
const url = new URL(link)
|
||||
out += char
|
||||
state = 'normal'
|
||||
} else {
|
||||
buffer += char
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
return tag('a', url.hostname + url.pathname, { href: url.href })
|
||||
} catch (err: unknown) {
|
||||
console.warn('failed to parse link', err)
|
||||
return link
|
||||
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)
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue