Skip to content

Commit f6cff35

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

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

lib/rehype-sn.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,38 @@ 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+
83+
let embed, host
84+
try {
85+
embed = parseEmbedUrl(href)
86+
} catch {
87+
embed = null
88+
}
89+
try {
90+
host = new URL(href).hostname
91+
} catch {
92+
host = null
93+
}
94+
95+
if (embed?.provider === 'nostr' && host === 'njump.me') {
96+
node.tagName = 'embed'
97+
node.properties = { ...embed, src: href }
98+
return
99+
}
100+
if (embed && embed.provider !== 'nostr' && textOnly && !hasTextSibling) {
82101
node.tagName = 'embed'
83-
node.properties = { ...embed, src: node.properties.href }
84-
} else {
102+
node.properties = { ...embed, src: href }
103+
return
104+
}
105+
if (textOnly && !hasTextSibling) {
85106
node.tagName = 'autolink'
107+
return
86108
}
87109
}
88110

@@ -136,8 +158,7 @@ export default function rehypeSN (options = {}) {
136158
}
137159
}
138160

139-
// Handle Nostr IDs
140-
if (node.type === 'text') {
161+
if (node.type === 'text' && !['a', 'autolink'].includes(parent?.tagName)) {
141162
const newChildren = []
142163
let lastIndex = 0
143164
let match
@@ -146,9 +167,7 @@ export default function rehypeSN (options = {}) {
146167
if (lastIndex < match.index) {
147168
newChildren.push({ type: 'text', value: node.value.slice(lastIndex, match.index) })
148169
}
149-
150170
newChildren.push(replaceNostrId(match[0], match[0]))
151-
152171
lastIndex = nostrIdRegex.lastIndex
153172
}
154173

0 commit comments

Comments
 (0)