Handle profile and note mentions in profile bio

This commit is contained in:
Bojan Mojsilovic 2024-03-26 13:11:46 +01:00
parent 377569ce79
commit 4299b15abd
2 changed files with 31 additions and 3 deletions

View File

@ -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 (<span><A href={`/p/${npub}`}>{npub}</A></span>)?.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 (<span><A href={`/e/${noteId}`}>{noteId}</A></span>)?.innerHTML || url;
}
return url;
});
}
export const urlify = (
text: string,
getMediaUrl: ((url: string | undefined, size?: MediaSize, animated?: boolean) => string | undefined) | undefined,

View File

@ -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)
};