From f6cff356e146e9fd3899f61b334d2cbdef83bf13 Mon Sep 17 00:00:00 2001 From: brymut Date: Sat, 28 Jun 2025 17:30:25 +0300 Subject: [PATCH] Fix(npub): Do not parse npubs in links --- lib/rehype-sn.js | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/rehype-sn.js b/lib/rehype-sn.js index bed7e8f42..d30556d34 100644 --- a/lib/rehype-sn.js +++ b/lib/rehype-sn.js @@ -73,16 +73,38 @@ export default function rehypeSN (options = {}) { } } - // only show a link as an embed if it doesn't have text siblings - if (node.tagName === 'a' && - !parent.children.some(s => s.type === 'text' && s.value.trim()) && - toString(node) === node.properties.href) { - const embed = parseEmbedUrl(node.properties.href) - if (embed) { + if (node.tagName === 'a') { + const href = node.properties.href + const textOnly = toString(node) === href + const hasTextSibling = parent.children.some( + s => s.type === 'text' && s.value.trim() + ) + + let embed, host + try { + embed = parseEmbedUrl(href) + } catch { + embed = null + } + try { + host = new URL(href).hostname + } catch { + host = null + } + + if (embed?.provider === 'nostr' && host === 'njump.me') { + node.tagName = 'embed' + node.properties = { ...embed, src: href } + return + } + if (embed && embed.provider !== 'nostr' && textOnly && !hasTextSibling) { node.tagName = 'embed' - node.properties = { ...embed, src: node.properties.href } - } else { + node.properties = { ...embed, src: href } + return + } + if (textOnly && !hasTextSibling) { node.tagName = 'autolink' + return } } @@ -136,8 +158,7 @@ export default function rehypeSN (options = {}) { } } - // Handle Nostr IDs - if (node.type === 'text') { + if (node.type === 'text' && !['a', 'autolink'].includes(parent?.tagName)) { const newChildren = [] let lastIndex = 0 let match @@ -146,9 +167,7 @@ export default function rehypeSN (options = {}) { if (lastIndex < match.index) { newChildren.push({ type: 'text', value: node.value.slice(lastIndex, match.index) }) } - newChildren.push(replaceNostrId(match[0], match[0])) - lastIndex = nostrIdRegex.lastIndex }