Non-word boundary Regex on user Mentions (#2096)

* fix: non-word boundary regex on user mentions; show user when mentioned with a path

* allow only a single slash after the user
This commit is contained in:
soxa 2025-04-12 21:20:01 +02:00 committed by GitHub
parent a3e0b6aa9c
commit 9b73990083
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,7 @@ import { toString } from 'mdast-util-to-string'
const userGroup = '[\\w_]+' const userGroup = '[\\w_]+'
const subGroup = '[A-Za-z][\\w_]+' const subGroup = '[A-Za-z][\\w_]+'
const mentionRegex = new RegExp('(?:^|\\s)@(' + userGroup + '(?:\\/' + userGroup + ')?)', 'gi') const mentionRegex = new RegExp('\\B@(' + userGroup + '(?:\\/' + userGroup + ')?)', 'gi')
const subRegex = new RegExp('~(' + subGroup + '(?:\\/' + subGroup + ')?)', 'gi') const subRegex = new RegExp('~(' + subGroup + '(?:\\/' + subGroup + ')?)', 'gi')
const nostrIdRegex = /\b((npub1|nevent1|nprofile1|note1|naddr1)[02-9ac-hj-np-z]+)\b/g const nostrIdRegex = /\b((npub1|nevent1|nprofile1|note1|naddr1)[02-9ac-hj-np-z]+)\b/g
@ -116,8 +116,8 @@ export default function rehypeSN (options = {}) {
} }
const [fullMatch, mentionMatch, subMatch] = match const [fullMatch, mentionMatch, subMatch] = match
const replacement = mentionMatch ? replaceMention(fullMatch, mentionMatch) : replaceSub(fullMatch, subMatch)
const replacement = mentionMatch ? replaceMention(fullMatch, mentionMatch) : replaceSub(fullMatch, subMatch)
if (replacement) { if (replacement) {
newChildren.push(replacement) newChildren.push(replacement)
} else { } else {
@ -238,10 +238,11 @@ export default function rehypeSN (options = {}) {
} }
function replaceMention (value, username) { function replaceMention (value, username) {
// split the name by / to allow user paths and still show the user
return { return {
type: 'element', type: 'element',
tagName: 'mention', tagName: 'mention',
properties: { href: '/' + username, name: username }, properties: { href: '/' + username, name: username.split('/')[0] },
children: [{ type: 'text', value }] children: [{ type: 'text', value }]
} }
} }