2025-02-14 13:15:10 +01:00
|
|
|
import { NDKKind } from '@nostr-dev-kit/ndk'
|
|
|
|
import { ProfileLink } from 'components/ProfileSection'
|
|
|
|
import { nip19 } from 'nostr-tools'
|
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { Fragment } from 'react/jsx-runtime'
|
|
|
|
import { BlogPreview } from './internal/BlogPreview'
|
|
|
|
import { ModPreview } from './internal/ModPreview'
|
|
|
|
import { NoteWrapper } from './internal/NoteWrapper'
|
2025-02-21 14:20:18 +01:00
|
|
|
import { NIP05_REGEX } from 'nostr-tools/nip05'
|
2025-02-14 13:15:10 +01:00
|
|
|
|
|
|
|
interface NoteRenderProps {
|
|
|
|
content: string
|
|
|
|
}
|
|
|
|
const link =
|
|
|
|
/(?:https?:\/\/|www\.)(?:[a-zA-Z0-9.-]+\.[a-zA-Z]+(?::\d+)?)(?:[/?#][\p{L}\p{N}\p{M}&.-/?=#\-@%+_,:!~*]*)?/gu
|
|
|
|
const nostrMention =
|
|
|
|
/(?:nostr:|@)?(?:npub|note|nprofile|nevent|naddr)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,}/gi
|
|
|
|
const nostrEntity =
|
|
|
|
/(npub|note|nprofile|nevent|naddr)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,}/gi
|
2025-02-21 14:20:18 +01:00
|
|
|
const nostrNip5Mention = /(?:nostr:|@)([^\s]{1,64}@[^\s]+\.[^\s]{2,})/gi
|
2025-02-14 13:15:10 +01:00
|
|
|
|
|
|
|
export const NoteRender = ({ content }: NoteRenderProps) => {
|
|
|
|
const _content = useMemo(() => {
|
|
|
|
if (!content) return
|
|
|
|
|
|
|
|
const parts = content.split(
|
2025-02-21 14:20:18 +01:00
|
|
|
new RegExp(
|
|
|
|
`(${link.source})|(${nostrMention.source})|${nostrNip5Mention.source}`,
|
|
|
|
'gui'
|
|
|
|
)
|
2025-02-14 13:15:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const _parts = parts.map((part, index) => {
|
|
|
|
if (link.test(part)) {
|
|
|
|
const [href] = part.match(link) || []
|
|
|
|
return (
|
2025-02-19 19:24:23 +01:00
|
|
|
<a key={index} target='_blank' href={href}>
|
2025-02-14 13:15:10 +01:00
|
|
|
{href}
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
} else if (nostrMention.test(part)) {
|
|
|
|
const [encoded] = part.match(nostrEntity) || []
|
|
|
|
|
|
|
|
if (!encoded) return part
|
|
|
|
|
|
|
|
try {
|
|
|
|
const decoded = nip19.decode(encoded)
|
|
|
|
|
|
|
|
switch (decoded.type) {
|
|
|
|
case 'nprofile':
|
|
|
|
return <ProfileLink key={index} pubkey={decoded.data.pubkey} />
|
|
|
|
case 'npub':
|
|
|
|
return <ProfileLink key={index} pubkey={decoded.data} />
|
|
|
|
case 'note':
|
|
|
|
return <NoteWrapper key={index} noteEntity={encoded} />
|
|
|
|
case 'nevent':
|
|
|
|
return <NoteWrapper key={index} noteEntity={encoded} />
|
|
|
|
case 'naddr':
|
|
|
|
return (
|
|
|
|
<Fragment key={index}>
|
|
|
|
{handleNaddr(decoded.data, part)}
|
|
|
|
</Fragment>
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return part
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return part
|
|
|
|
}
|
2025-02-21 14:20:18 +01:00
|
|
|
} else if (NIP05_REGEX.test(part)) {
|
|
|
|
const [nip05] = part.match(NIP05_REGEX) || []
|
|
|
|
return <ProfileLink key={index} nip05={nip05} />
|
2025-02-14 13:15:10 +01:00
|
|
|
} else {
|
|
|
|
return part
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return _parts
|
|
|
|
}, [content])
|
|
|
|
|
|
|
|
return _content
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleNaddr(data: nip19.AddressPointer, original: string) {
|
|
|
|
const { kind } = data
|
|
|
|
|
|
|
|
if (kind === NDKKind.Article) {
|
|
|
|
return <BlogPreview {...data} original={original} />
|
|
|
|
} else if (kind === NDKKind.Classified) {
|
|
|
|
return <ModPreview {...data} original={original} />
|
|
|
|
} else {
|
|
|
|
return <>{original}</>
|
|
|
|
}
|
|
|
|
}
|