Skip to content

Fix(npub): Do not parse npubs in links #2253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions lib/rehype-sn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down