diff --git a/src/shared/notes/preview/link.tsx b/src/shared/notes/preview/link.tsx
index ea7942fe..9d7bbdb6 100644
--- a/src/shared/notes/preview/link.tsx
+++ b/src/shared/notes/preview/link.tsx
@@ -7,7 +7,7 @@ export function LinkPreview({ urls }: { urls: string[] }) {
const domain = new URL(urls[0]);
return (
-
+
{status === 'loading' ? (
diff --git a/src/shared/notes/preview/video.tsx b/src/shared/notes/preview/video.tsx
index 320cd412..a03cb621 100644
--- a/src/shared/notes/preview/video.tsx
+++ b/src/shared/notes/preview/video.tsx
@@ -2,7 +2,7 @@ import ReactPlayer from 'react-player/es6';
export function VideoPreview({ urls }: { urls: string[] }) {
return (
-
+
diff --git a/src/shared/notes/replies/sub.tsx b/src/shared/notes/replies/sub.tsx
index b2633482..004aa474 100644
--- a/src/shared/notes/replies/sub.tsx
+++ b/src/shared/notes/replies/sub.tsx
@@ -10,7 +10,7 @@ export function SubReply({ event }: { event: NDKEvent }) {
diff --git a/src/shared/widgets/global/hashtag.tsx b/src/shared/widgets/global/hashtag.tsx
index df9d6ab2..13693946 100644
--- a/src/shared/widgets/global/hashtag.tsx
+++ b/src/shared/widgets/global/hashtag.tsx
@@ -58,7 +58,7 @@ export function GlobalHashtagWidget({ params }: { params: Widget }) {
ref={virtualizer.measureElement}
>
-
+
);
diff --git a/src/shared/widgets/local/feeds.tsx b/src/shared/widgets/local/feeds.tsx
index ae11a61a..fcd29983 100644
--- a/src/shared/widgets/local/feeds.tsx
+++ b/src/shared/widgets/local/feeds.tsx
@@ -60,7 +60,7 @@ export function LocalFeedsWidget({ params }: { params: Widget }) {
ref={virtualizer.measureElement}
>
-
+
);
diff --git a/src/shared/widgets/local/network.tsx b/src/shared/widgets/local/network.tsx
index 7ebdd8a8..9ef29d39 100644
--- a/src/shared/widgets/local/network.tsx
+++ b/src/shared/widgets/local/network.tsx
@@ -62,7 +62,7 @@ export function LocalNetworkWidget() {
ref={virtualizer.measureElement}
>
-
+
);
@@ -122,7 +122,7 @@ export function LocalNetworkWidget() {
useEffect(() => {
if (db.account && db.account.network) {
const filter: NDKFilter = {
- kinds: [1, 6],
+ kinds: [NDKKind.Text, NDKKind.Repost],
authors: db.account.network,
since: db.account.last_login_at ?? Math.floor(Date.now() / 1000),
};
diff --git a/src/shared/widgets/local/thread.tsx b/src/shared/widgets/local/thread.tsx
index 4fda8166..6087e129 100644
--- a/src/shared/widgets/local/thread.tsx
+++ b/src/shared/widgets/local/thread.tsx
@@ -28,7 +28,7 @@ export function LocalThreadWidget({ params }: { params: Widget }) {
(event: NDKEvent) => {
switch (event.kind) {
case NDKKind.Text:
- return
;
+ return
;
case NDKKind.Article:
return
;
case 1063:
diff --git a/src/shared/widgets/local/user.tsx b/src/shared/widgets/local/user.tsx
index 703adf5a..0759beee 100644
--- a/src/shared/widgets/local/user.tsx
+++ b/src/shared/widgets/local/user.tsx
@@ -64,7 +64,7 @@ export function LocalUserWidget({ params }: { params: Widget }) {
ref={virtualizer.measureElement}
>
-
+
);
diff --git a/src/shared/widgets/nostrBand/trendingNotes.tsx b/src/shared/widgets/nostrBand/trendingNotes.tsx
index 84caf6bc..321b2b88 100644
--- a/src/shared/widgets/nostrBand/trendingNotes.tsx
+++ b/src/shared/widgets/nostrBand/trendingNotes.tsx
@@ -52,7 +52,7 @@ export function TrendingNotesWidget({ params }: { params: Widget }) {
{data.map((item) => (
-
+
))}
diff --git a/src/utils/hooks/useEvent.ts b/src/utils/hooks/useEvent.ts
index e76f8b7e..70074be8 100644
--- a/src/utils/hooks/useEvent.ts
+++ b/src/utils/hooks/useEvent.ts
@@ -23,7 +23,7 @@ export function useEvent(id: string, embed?: string) {
// get event from relay if event in db not present
const event = await ndk.fetchEvent(id);
- if (!event) throw new Error(`Event not found: ${id}`);
+ if (!event) throw new Error(`Event not found: ${id.toString()}`);
let root: string;
let reply: string;
@@ -50,10 +50,8 @@ export function useEvent(id: string, embed?: string) {
},
{
enabled: !!ndk,
- staleTime: Infinity,
refetchOnMount: false,
refetchOnWindowFocus: false,
- refetchOnReconnect: false,
}
);
diff --git a/src/utils/parser.ts b/src/utils/parser.ts
index 4bc330cc..762eb68d 100644
--- a/src/utils/parser.ts
+++ b/src/utils/parser.ts
@@ -1,73 +1,87 @@
-import { NDKEvent } from '@nostr-dev-kit/ndk';
-import getUrls from 'get-urls';
-import { Event, parseReferences } from 'nostr-tools';
+import { nip19 } from 'nostr-tools';
+import { EventPointer } from 'nostr-tools/lib/nip19';
import { RichContent } from '@utils/types';
-export function parser(event: NDKEvent) {
- const references = parseReferences(event as unknown as Event);
- const urls = getUrls(event.content as unknown as string);
-
- const content: RichContent = {
- parsed: event.content as unknown as string,
- notes: [],
- images: [],
- videos: [],
- links: [],
- };
-
- // parse nostr references
- references?.forEach((item) => {
- const profile = item.profile;
- const event = item.event;
- const addr = item.address;
- if (event) {
- content.notes.push(event.id);
- content.parsed = content.parsed.replace(item.text, '');
- }
- if (profile) {
- content.parsed = content.parsed.replace(item.text, `~pub-${item.profile.pubkey}~`);
- }
- if (addr) {
- content.notes.push(addr.identifier);
- content.parsed = content.parsed.replace(item.text, '');
- }
- });
-
- // parse urls
- urls?.forEach((url: string) => {
- if (url.match(/\.(jpg|jpeg|gif|png|webp|avif)$/)) {
- // image url
- content.images.push(url);
- // remove url from original content
- content.parsed = content.parsed.replace(url, '');
- }
-
- if (url.match(/\.(mp4|mov|webm|wmv|flv|mts|avi|ogv|mkv|mp3|m3u8)$/)) {
- // video
- content.videos.push(url);
- // remove url from original content
- content.parsed = content.parsed.replace(url, '');
- }
-
- /*
- if (content.links.length < 1) {
- // push to store
- content.links.push(url);
- // remove url from original content
- content.parsed = content.parsed.replace(url, '');
- }
- */
- });
-
- // parse hashtag
- const hashtags = content.parsed.split(/\s/gm).filter((s) => s.startsWith('#'));
- if (hashtags) {
- const uniqTags = new Set(hashtags);
- uniqTags.forEach((tag) => {
- content.parsed = content.parsed.replaceAll(tag, `~tag-${tag}~`);
- });
- }
-
- return content;
+function isURL(str: string) {
+ const pattern = new RegExp(
+ '^(https?:\\/\\/)?' + // protocol
+ '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
+ '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
+ '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
+ '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
+ '(\\#[-a-z\\d_]*)?$', // fragment locator
+ 'i'
+ );
+ return !!pattern.test(str);
+}
+
+export function parser(eventContent: string) {
+ try {
+ const content: RichContent = {
+ parsed: null,
+ images: [],
+ videos: [],
+ links: [],
+ notes: [],
+ };
+
+ const parse = eventContent.split(/\s/gm).map((word) => {
+ // url
+ if (isURL(word)) {
+ const url = new URL(word);
+ url.search = '';
+
+ if (url.toString().match(/\.(jpg|jpeg|gif|png|webp|avif)$/)) {
+ // image url
+ content.images.push(word);
+ // remove url from original content
+ return word.replace(word, '');
+ }
+
+ if (url.toString().match(/\.(mp4|mov|webm|wmv|flv|mts|avi|ogv|mkv|mp3|m3u8)$/)) {
+ // video
+ content.videos.push(word);
+ // remove url from original content
+ word = word.replace(word, '');
+ }
+ }
+
+ // hashtag
+ if (word.startsWith('#') && word.length > 1) {
+ return word.replace(word, `~tag-${word}~`);
+ }
+
+ // nostr account references
+ if (word.startsWith('nostr:npub1')) {
+ const npub = word.replace('nostr:', '').replace(/[^a-zA-Z0-9 ]/g, '');
+ return word.replace(word, `~pub-${nip19.decode(npub).data}~`);
+ }
+
+ // nostr account references
+ if (word.startsWith('nostr:note1')) {
+ const note = word.replace('nostr:', '').replace(/[^a-zA-Z0-9 ]/g, '');
+ content.notes.push(nip19.decode(note).data as string);
+ return word.replace(word, '');
+ }
+
+ // nostr event references
+ if (word.startsWith('nostr:nevent1')) {
+ const nevent = word.replace('nostr:', '').replace(/[^a-zA-Z0-9 ]/g, '');
+ const decoded = nip19.decode(nevent).data as EventPointer;
+ content.notes.push(decoded.id);
+ return word.replace(word, '');
+ }
+
+ // normal word
+ return word;
+ });
+
+ // update content with parsed version
+ content.parsed = parse.join(' ');
+
+ return content;
+ } catch (e) {
+ console.error('cannot parse content, error: ', e);
+ }
}
diff --git a/src/utils/types.d.ts b/src/utils/types.d.ts
index b9e1b66b..bab45a61 100644
--- a/src/utils/types.d.ts
+++ b/src/utils/types.d.ts
@@ -2,10 +2,10 @@ import { NDKEvent, NDKUserProfile } from '@nostr-dev-kit/ndk';
export interface RichContent {
parsed: string;
- notes: string[];
images: string[];
videos: string[];
links: string[];
+ notes: string[];
}
export interface DBEvent {