correct @ mentions when making posts

This commit is contained in:
hzrd149 2023-09-28 08:38:27 -05:00
parent d4ba16ef51
commit 676703fd9c
2 changed files with 16 additions and 3 deletions

View File

@ -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) {

View File

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