Files
PeerTube/server/core/helpers/mentions.ts
T
Chocobozzz 09fabc2a3c Fix mentions extractions with punctuations
Also don't consume spaces in regex, resulting in broken mention
extractions
2026-07-13 14:42:23 +02:00

19 lines
737 B
TypeScript

import { uniqify } from '@peertube/peertube-core-utils'
import { WEBSERVER } from '@server/initializers/constants.js'
import { actorNameAlphabet } from './custom-validators/activitypub/actor.js'
import { regexpCapture } from './regexp.js'
export function extractLocalMentions (text: string, fromLocalEntity: boolean) {
const setHostOptionalChar = fromLocalEntity
? '?'
: ''
// FIXME: Use RegExp.escape() when NodeJS 24 is the minimum version required by peertube
const mentionRegex = new RegExp(`(?<=^|\\s)@(${actorNameAlphabet}+)(?:@${WEBSERVER.HOST})${setHostOptionalChar}(?=[\\s.,!?)]|$)`, 'gmu')
const results = regexpCapture(text, mentionRegex)
.map(([ , username ]) => username)
return uniqify(results)
}