diff --git a/src/components/post-modal/index.tsx b/src/components/post-modal/index.tsx index 86eb505d8..e6e7e0225 100644 --- a/src/components/post-modal/index.tsx +++ b/src/components/post-modal/index.tsx @@ -29,7 +29,13 @@ import { ArrowDownSIcon, ArrowUpSIcon, ImageIcon } from "../icons"; import { NoteContents } from "../note/note-contents"; import { PublishDetails } from "../publish-details"; import { TrustProvider } from "../../providers/trust"; -import { createEmojiTags, ensureNotifyPubkeys, finalizeNote, getContentMentions } from "../../helpers/nostr/post"; +import { + correctContentMentions, + createEmojiTags, + ensureNotifyPubkeys, + finalizeNote, + getContentMentions, +} from "../../helpers/nostr/post"; import { UserAvatarStack } from "../compact-user-stack"; import MagicTextArea, { RefType } from "../magic-textarea"; import { useContextEmojis } from "../../providers/emoji-provider"; @@ -95,6 +101,8 @@ export default function PostModal({ created_at: dayjs().unix(), }); + updatedDraft.content = correctContentMentions(updatedDraft.content); + if (nsfw) { updatedDraft.tags.push(nsfwReason ? ["content-warning", nsfwReason] : ["content-warning"]); } @@ -119,7 +127,7 @@ export default function PostModal({ }); const canSubmit = getValues().content.length > 0; - const mentions = getContentMentions(getValues().content); + const mentions = getContentMentions(correctContentMentions(getValues().content)); const renderContent = () => { if (publishAction) { diff --git a/src/helpers/nostr/post.ts b/src/helpers/nostr/post.ts index 819137eaa..15c8f982e 100644 --- a/src/helpers/nostr/post.ts +++ b/src/helpers/nostr/post.ts @@ -61,6 +61,10 @@ export function ensureNotifyPubkeys(draft: DraftNostrEvent, pubkeys: string[]) { return updated; } +export function correctContentMentions(content: string) { + return content.replace(/(\s|^)(?:@)?(npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})/gi, "$1nostr:$2"); +} + export function getContentMentions(content: string) { const matched = content.matchAll(/nostr:(npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})/gi); return Array.from(matched) @@ -107,7 +111,8 @@ export function createEmojiTags(draft: DraftNostrEvent, emojis: Emoji[]) { } export function finalizeNote(draft: DraftNostrEvent) { - let updated = draft; + let updated: DraftNostrEvent = { ...draft, tags: Array.from(draft.tags) }; + updated.content = correctContentMentions(updated.content); updated = createHashtagTags(updated); return updated; }