Skip to content

Commit 82ba456

Browse files
committed
Fix(npub): Do not parse npubs in links
1 parent 8337aad commit 82ba456

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

lib/rehype-sn.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,30 @@ export default function rehypeSN (options = {}) {
7373
}
7474
}
7575

76-
// only show a link as an embed if it doesn't have text siblings
77-
if (node.tagName === 'a' &&
78-
!parent.children.some(s => s.type === 'text' && s.value.trim()) &&
79-
toString(node) === node.properties.href) {
80-
const embed = parseEmbedUrl(node.properties.href)
81-
if (embed) {
76+
if (node.tagName === 'a') {
77+
const href = node.properties.href
78+
const textOnly = toString(node) === href
79+
const hasTextSibling = parent.children.some(
80+
s => s.type === 'text' && s.value.trim()
81+
)
82+
let embed
83+
try {
84+
embed = parseEmbedUrl(href)
85+
} catch { /* invalid URL */ }
86+
87+
if (embed?.provider === 'nostr' && new URL(href).hostname === 'njump.me') {
88+
node.tagName = 'embed'
89+
node.properties = { ...embed, src: href }
90+
return
91+
}
92+
if (embed && embed.provider !== 'nostr' && textOnly && !hasTextSibling) {
8293
node.tagName = 'embed'
83-
node.properties = { ...embed, src: node.properties.href }
84-
} else {
85-
node.tagName = 'autolink'
94+
node.properties = { ...embed, src: href }
95+
return
8696
}
97+
98+
node.tagName = 'autolink'
99+
return
87100
}
88101

89102
// if the link text is a URL, just show the URL
@@ -136,8 +149,7 @@ export default function rehypeSN (options = {}) {
136149
}
137150
}
138151

139-
// Handle Nostr IDs
140-
if (node.type === 'text') {
152+
if (node.type === 'text' && !['a', 'autolink'].includes(parent?.tagName)) {
141153
const newChildren = []
142154
let lastIndex = 0
143155
let match
@@ -146,9 +158,7 @@ export default function rehypeSN (options = {}) {
146158
if (lastIndex < match.index) {
147159
newChildren.push({ type: 'text', value: node.value.slice(lastIndex, match.index) })
148160
}
149-
150161
newChildren.push(replaceNostrId(match[0], match[0]))
151-
152162
lastIndex = nostrIdRegex.lastIndex
153163
}
154164

0 commit comments

Comments
 (0)