@@ -73,17 +73,30 @@ export default function rehypeSN (options = {}) {
73
73
}
74
74
}
75
75
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 ) {
82
93
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
86
96
}
97
+
98
+ node . tagName = 'autolink'
99
+ return
87
100
}
88
101
89
102
// if the link text is a URL, just show the URL
@@ -136,8 +149,7 @@ export default function rehypeSN (options = {}) {
136
149
}
137
150
}
138
151
139
- // Handle Nostr IDs
140
- if ( node . type === 'text' ) {
152
+ if ( node . type === 'text' && ! [ 'a' , 'autolink' ] . includes ( parent ?. tagName ) ) {
141
153
const newChildren = [ ]
142
154
let lastIndex = 0
143
155
let match
@@ -146,9 +158,7 @@ export default function rehypeSN (options = {}) {
146
158
if ( lastIndex < match . index ) {
147
159
newChildren . push ( { type : 'text' , value : node . value . slice ( lastIndex , match . index ) } )
148
160
}
149
-
150
161
newChildren . push ( replaceNostrId ( match [ 0 ] , match [ 0 ] ) )
151
-
152
162
lastIndex = nostrIdRegex . lastIndex
153
163
}
154
164
0 commit comments