Skip to content

Commit 879f6b2

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

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

lib/rehype-sn.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const subGroup = '[A-Za-z][\\w_]+'
88

99
const mentionRegex = new RegExp('\\B@(' + userGroup + '(?:\\/' + userGroup + ')?)', 'gi')
1010
const subRegex = new RegExp('~(' + subGroup + '(?:\\/' + subGroup + ')?)', 'gi')
11-
const nostrIdRegex = /\b((npub1|nevent1|nprofile1|note1|naddr1)[02-9ac-hj-np-z]+)\b/g
11+
const nostrIdRegex = /\b((npub1|nevent1|nprofile1|note1|naddr1)[02-9ac-hj-np-z]+)\b/i
1212

1313
export default function rehypeSN (options = {}) {
1414
const { stylers = [] } = options
@@ -77,12 +77,20 @@ export default function rehypeSN (options = {}) {
7777
if (node.tagName === 'a' &&
7878
!parent.children.some(s => s.type === 'text' && s.value.trim()) &&
7979
toString(node) === node.properties.href) {
80-
const embed = parseEmbedUrl(node.properties.href)
81-
if (embed) {
82-
node.tagName = 'embed'
83-
node.properties = { ...embed, src: node.properties.href }
84-
} else {
80+
nostrIdRegex.lastIndex = 0
81+
const isNostrLink = nostrIdRegex.test(node.properties.href)
82+
nostrIdRegex.lastIndex = 0
83+
84+
if (isNostrLink) {
8585
node.tagName = 'autolink'
86+
} else {
87+
const embed = parseEmbedUrl(node.properties.href)
88+
if (embed) {
89+
node.tagName = 'embed'
90+
node.properties = { ...embed, src: node.properties.href }
91+
} else {
92+
node.tagName = 'autolink'
93+
}
8694
}
8795
}
8896

@@ -137,7 +145,7 @@ export default function rehypeSN (options = {}) {
137145
}
138146

139147
// Handle Nostr IDs
140-
if (node.type === 'text') {
148+
if (node.type === 'text' && !(parent && ['a', 'autolink'].includes(parent.tagName))) {
141149
const newChildren = []
142150
let lastIndex = 0
143151
let match

0 commit comments

Comments
 (0)