improve hashtag parser

This commit is contained in:
Ren Amamiya 2023-09-01 18:26:22 +07:00
parent e6d35bc635
commit 28939d1733
4 changed files with 30 additions and 11 deletions

View File

@ -7,8 +7,9 @@ export function Hashtag({ tag }: { tag: string }) {
const setWidget = useWidgets((state) => state.setWidget); const setWidget = useWidgets((state) => state.setWidget);
return ( return (
<button <span
type="button" role="button"
tabIndex={0}
onClick={() => onClick={() =>
setWidget(db, { setWidget(db, {
kind: WidgetKinds.global.hashtag, kind: WidgetKinds.global.hashtag,
@ -16,9 +17,16 @@ export function Hashtag({ tag }: { tag: string }) {
content: tag.replace('#', ''), content: tag.replace('#', ''),
}) })
} }
onKeyDown={() =>
setWidget(db, {
kind: WidgetKinds.global.hashtag,
title: tag,
content: tag.replace('#', ''),
})
}
className="break-words text-fuchsia-400 hover:text-fuchsia-500" className="break-words text-fuchsia-400 hover:text-fuchsia-500"
> >
{tag} {tag}
</button> </span>
); );
} }

View File

@ -66,10 +66,10 @@ export const MentionNote = memo(function MentionNote({ id }: { id: string }) {
onKeyDown={(e) => openThread(e, id)} onKeyDown={(e) => openThread(e, id)}
role="button" role="button"
tabIndex={0} tabIndex={0}
className="mb-2 mt-3 cursor-default rounded-lg bg-white/10 px-3 py-3 backdrop-blur-xl" className="cursor-default rounded-lg bg-white/10 px-3 py-3 backdrop-blur-xl"
> >
<User pubkey={data.pubkey} time={data.created_at} size="small" /> <User pubkey={data.pubkey} time={data.created_at} size="small" />
<div>{renderKind(data)}</div> <div className="mt-1">{renderKind(data)}</div>
</div> </div>
); );
}); });

View File

@ -12,8 +12,9 @@ export function MentionUser({ pubkey }: { pubkey: string }) {
const setWidget = useWidgets((state) => state.setWidget); const setWidget = useWidgets((state) => state.setWidget);
return ( return (
<button <span
type="button" role="button"
tabIndex={0}
onClick={() => onClick={() =>
setWidget(db, { setWidget(db, {
kind: WidgetKinds.local.user, kind: WidgetKinds.local.user,
@ -21,9 +22,16 @@ export function MentionUser({ pubkey }: { pubkey: string }) {
content: pubkey, content: pubkey,
}) })
} }
onKeyDown={() =>
setWidget(db, {
kind: WidgetKinds.local.user,
title: user?.name || user?.display_name,
content: pubkey,
})
}
className="break-words text-fuchsia-400 hover:text-fuchsia-500" className="break-words text-fuchsia-400 hover:text-fuchsia-500"
> >
{user?.name || user?.display_name || user?.username || displayNpub(pubkey, 16)} {user?.name || user?.display_name || user?.username || displayNpub(pubkey, 16)}
</button> </span>
); );
} }

View File

@ -62,9 +62,12 @@ export function parser(event: NDKEvent) {
// parse hashtag // parse hashtag
const hashtags = content.parsed.split(/\s/gm).filter((s) => s.startsWith('#')); const hashtags = content.parsed.split(/\s/gm).filter((s) => s.startsWith('#'));
hashtags?.forEach((tag) => { if (hashtags) {
content.parsed = content.parsed.replace(tag, ` ~tag-${tag}~ `); const uniqTags = new Set(hashtags);
}); uniqTags.forEach((tag) => {
content.parsed = content.parsed.replaceAll(tag, `~tag-${tag}~`);
});
}
return content; return content;
} }