fix: preserve linebreaks between URLs and Nostr references in editor (#657)
Co-authored-by: The Daniel <dmnyc@users.noreply.github.com>
This commit is contained in:
@@ -4,12 +4,13 @@ import { JSONContent } from '@tiptap/react'
|
|||||||
import { nip19 } from 'nostr-tools'
|
import { nip19 } from 'nostr-tools'
|
||||||
|
|
||||||
export function parseEditorJsonToText(node?: JSONContent) {
|
export function parseEditorJsonToText(node?: JSONContent) {
|
||||||
const text = _parseEditorJsonToText(node).trim()
|
const text = _parseEditorJsonToText(node)
|
||||||
const regex = /(?:^|\s|@)(nostr:)?(nevent|naddr|nprofile|npub)1[a-zA-Z0-9]+/g
|
const regex = /(^|\s+|@)(nostr:)?(nevent|naddr|nprofile|npub)1[a-zA-Z0-9]+/g
|
||||||
|
|
||||||
return text.replace(regex, (match) => {
|
return text.replace(regex, (match, leadingWhitespace) => {
|
||||||
let bech32 = match.trim()
|
let bech32 = match.trim()
|
||||||
const leadingSpace = match.startsWith(' ') ? ' ' : ''
|
const whitespace = leadingWhitespace || ''
|
||||||
|
|
||||||
if (bech32.startsWith('@nostr:')) {
|
if (bech32.startsWith('@nostr:')) {
|
||||||
bech32 = bech32.slice(7)
|
bech32 = bech32.slice(7)
|
||||||
} else if (bech32.startsWith('@')) {
|
} else if (bech32.startsWith('@')) {
|
||||||
@@ -20,11 +21,11 @@ export function parseEditorJsonToText(node?: JSONContent) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
nip19.decode(bech32)
|
nip19.decode(bech32)
|
||||||
return `${leadingSpace}nostr:${bech32}`
|
return `${whitespace}nostr:${bech32}`
|
||||||
} catch {
|
} catch {
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
})
|
}).trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
function _parseEditorJsonToText(node?: JSONContent): string {
|
function _parseEditorJsonToText(node?: JSONContent): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user