From 4299b15abd1976464dd3986d34455376b5f8fb8e Mon Sep 17 00:00:00 2001 From: Bojan Mojsilovic Date: Tue, 26 Mar 2024 13:11:46 +0100 Subject: [PATCH] Handle profile and note mentions in profile bio --- src/lib/notes.tsx | 30 +++++++++++++++++++++++++++++- src/pages/Profile.tsx | 4 ++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/lib/notes.tsx b/src/lib/notes.tsx index 0fa1cd0..811349d 100644 --- a/src/lib/notes.tsx +++ b/src/lib/notes.tsx @@ -1,8 +1,9 @@ // @ts-ignore Bad types in nostr-tools +import { A } from "@solidjs/router"; import { Relay } from "nostr-tools"; import { createStore } from "solid-js/store"; import LinkPreview from "../components/LinkPreview/LinkPreview"; -import { appleMusicRegex, emojiRegex, hashtagRegex, interpunctionRegex, Kind, linebreakRegex, mixCloudRegex, nostrNestsRegex, noteRegex, noteRegexLocal, profileRegex, soundCloudRegex, spotifyRegex, tagMentionRegex, twitchRegex, urlRegex, urlRegexG, wavlakeRegex, youtubeRegex } from "../constants"; +import { appleMusicRegex, emojiRegex, hashtagRegex, interpunctionRegex, Kind, linebreakRegex, mixCloudRegex, nostrNestsRegex, noteRegex, noteRegexLocal, profileRegex, profileRegexG, soundCloudRegex, spotifyRegex, tagMentionRegex, twitchRegex, urlRegex, urlRegexG, wavlakeRegex, youtubeRegex } from "../constants"; import { sendMessage, subscribeTo } from "../sockets"; import { MediaSize, NostrRelays, NostrRelaySignedEvent, PrimalNote, SendNoteResult } from "../types/primal"; import { logError, logInfo, logWarning } from "./logger"; @@ -75,6 +76,33 @@ export const isNostrNests = (url: string) => nostrNestsRegex.test(url); export const isWavelake = (url: string) => wavlakeRegex.test(url); +export const linkifyNostrProfileLink = (text: string) => { + + return text.replace(profileRegexG, (url) => { + if (isUserMention(url)) { + const npub = url.split('nostr:')[1]; + // @ts-ignore + return ({npub})?.innerHTML || url; + } + + return url; + + }); +} +export const linkifyNostrNoteLink = (text: string) => { + + return text.replace(noteRegex, (url) => { + if (isNoteMention(url)) { + const noteId = url.split('nostr:')[1]; + // @ts-ignore + return ({noteId})?.innerHTML || url; + } + + return url; + + }); +} + export const urlify = ( text: string, getMediaUrl: ((url: string | undefined, size?: MediaSize, animated?: boolean) => string | undefined) | undefined, diff --git a/src/pages/Profile.tsx b/src/pages/Profile.tsx index 1013e26..1fd1eb6 100644 --- a/src/pages/Profile.tsx +++ b/src/pages/Profile.tsx @@ -19,7 +19,7 @@ import { useProfileContext } from '../contexts/ProfileContext'; import { useAccountContext } from '../contexts/AccountContext'; import Wormhole from '../components/Wormhole/Wormhole'; import { useIntl } from '@cookbook/solid-intl'; -import { urlify, sanitize } from '../lib/notes'; +import { urlify, sanitize, linkifyNostrProfileLink, linkifyNostrNoteLink } from '../lib/notes'; import { shortDate } from '../lib/dates'; import styles from './Profile.module.scss'; @@ -439,7 +439,7 @@ const Profile: Component = () => { const [renderProfileAbout, setRenderProfileAbout] = createSignal(''); const getProfileAbout = (about: string) => { - const a = urlify(sanitize(about), () => '', false, false, true); + const a = linkifyNostrNoteLink(linkifyNostrProfileLink(urlify(sanitize(about), () => '', false, false, true))); setRenderProfileAbout(a) };