From 9b73990083fe67ac270e484eb8bad9b3b5a4fe14 Mon Sep 17 00:00:00 2001 From: soxa <6390896+Soxasora@users.noreply.github.com> Date: Sat, 12 Apr 2025 21:20:01 +0200 Subject: [PATCH] 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 --- lib/rehype-sn.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/rehype-sn.js b/lib/rehype-sn.js index 98a17dbd..bed7e8f4 100644 --- a/lib/rehype-sn.js +++ b/lib/rehype-sn.js @@ -6,7 +6,7 @@ import { toString } from 'mdast-util-to-string' const userGroup = '[\\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 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 replacement = mentionMatch ? replaceMention(fullMatch, mentionMatch) : replaceSub(fullMatch, subMatch) + const replacement = mentionMatch ? replaceMention(fullMatch, mentionMatch) : replaceSub(fullMatch, subMatch) if (replacement) { newChildren.push(replacement) } else { @@ -238,10 +238,11 @@ export default function rehypeSN (options = {}) { } function replaceMention (value, username) { + // split the name by / to allow user paths and still show the user return { type: 'element', tagName: 'mention', - properties: { href: '/' + username, name: username }, + properties: { href: '/' + username, name: username.split('/')[0] }, children: [{ type: 'text', value }] } }