refactor: post editor

This commit is contained in:
codytseng 2025-05-23 22:47:31 +08:00
parent 3d06421acb
commit 78725d1e88
31 changed files with 1603 additions and 766 deletions

40
src/lib/tiptap.ts Normal file
View file

@ -0,0 +1,40 @@
import { JSONContent } from '@tiptap/react'
import { nip19 } from 'nostr-tools'
export function parseEditorJsonToText(node?: JSONContent) {
const text = _parseEditorJsonToText(node).trim()
const regex = /(?<=^|\s)(nevent|naddr|nprofile|npub)[a-zA-Z0-9]+/g
return text.replace(regex, (match) => {
try {
nip19.decode(match)
return `nostr:${match}`
} catch {
return match
}
})
}
function _parseEditorJsonToText(node?: JSONContent): string {
if (!node) return ''
if (typeof node === 'string') return node
if (node.type === 'text') {
return node.text || ''
}
if (Array.isArray(node.content)) {
return (
node.content.map(_parseEditorJsonToText).join('') + (node.type === 'paragraph' ? '\n' : '')
)
}
switch (node.type) {
case 'paragraph':
return '\n'
case 'mention':
return node.attrs ? `nostr:${node.attrs.id}` : ''
default:
return ''
}
}