mirror of
https://github.com/dergigi/boris.git
synced 2025-12-17 22:54:30 +01:00
chore: remove debug console.log statements from nostrUriResolver
Removed all debug logging that was added for troubleshooting the link processing issue. The functionality remains intact, including the parser-based markdown link detection and HTTP URL protection.
This commit is contained in:
@@ -115,45 +115,19 @@ function replaceNostrUrisSafely(
|
|||||||
markdown: string,
|
markdown: string,
|
||||||
getReplacement: (encoded: string) => string
|
getReplacement: (encoded: string) => string
|
||||||
): string {
|
): string {
|
||||||
console.log('[nostrUriResolver] ===== STARTING MARKDOWN PROCESSING =====')
|
|
||||||
console.log('[nostrUriResolver] Input markdown length:', markdown.length)
|
|
||||||
console.log('[nostrUriResolver] Input markdown preview (first 500 chars):', markdown.slice(0, 500))
|
|
||||||
|
|
||||||
// Check if input already contains markdown links (could indicate double-processing)
|
|
||||||
const existingLinkCount = (markdown.match(/\]\(/g) || []).length
|
|
||||||
console.log('[nostrUriResolver] Existing markdown links in input:', existingLinkCount)
|
|
||||||
|
|
||||||
// Check if markdown already contains our internal routes (indicates it's been processed)
|
|
||||||
// This prevents double-processing which can cause nested links
|
|
||||||
const hasInternalRoutes = /\]\(\/(?:a|p)\/[a-z0-9]+1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,}\)/i.test(markdown)
|
|
||||||
if (hasInternalRoutes) {
|
|
||||||
// Count how many internal routes we see
|
|
||||||
const internalRouteMatches = markdown.match(/\]\(\/(?:a|p)\/[a-z0-9]+1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,}\)/gi) || []
|
|
||||||
console.log('[nostrUriResolver] ⚠️ WARNING: Markdown appears to already be processed (contains', internalRouteMatches.length, 'internal routes like /a/naddr1... or /p/npub1...)')
|
|
||||||
console.log('[nostrUriResolver] Skipping re-processing to avoid nested links')
|
|
||||||
// Return as-is - don't process again
|
|
||||||
return markdown
|
|
||||||
}
|
|
||||||
|
|
||||||
// Track positions where we're inside a markdown link URL
|
// Track positions where we're inside a markdown link URL
|
||||||
// Use a parser approach to correctly handle URLs with brackets/parentheses
|
// Use a parser approach to correctly handle URLs with brackets/parentheses
|
||||||
const linkRanges: Array<{ start: number, end: number }> = []
|
const linkRanges: Array<{ start: number, end: number }> = []
|
||||||
|
|
||||||
// Find all markdown link URLs by looking for ]( pattern and tracking to matching )
|
// Find all markdown link URLs by looking for ]( pattern and tracking to matching )
|
||||||
let i = 0
|
let i = 0
|
||||||
let linksFound = 0
|
|
||||||
while (i < markdown.length) {
|
while (i < markdown.length) {
|
||||||
// Look for ]( pattern that starts a markdown link URL
|
// Look for ]( pattern that starts a markdown link URL
|
||||||
const urlStartMatch = markdown.indexOf('](', i)
|
const urlStartMatch = markdown.indexOf('](', i)
|
||||||
if (urlStartMatch === -1) break
|
if (urlStartMatch === -1) break
|
||||||
|
|
||||||
linksFound++
|
|
||||||
const urlStart = urlStartMatch + 2 // Position after "]("
|
const urlStart = urlStartMatch + 2 // Position after "]("
|
||||||
|
|
||||||
// Check what comes before ]( to see if it's actually a markdown link
|
|
||||||
const beforeMatch = markdown.slice(Math.max(0, urlStartMatch - 50), urlStartMatch)
|
|
||||||
console.log('[nostrUriResolver] Found ]( at position', urlStartMatch, 'context before:', beforeMatch)
|
|
||||||
|
|
||||||
// Now find the matching closing parenthesis
|
// Now find the matching closing parenthesis
|
||||||
// We need to account for nested parentheses and escaped characters
|
// We need to account for nested parentheses and escaped characters
|
||||||
let pos = urlStart
|
let pos = urlStart
|
||||||
@@ -184,15 +158,6 @@ function replaceNostrUrisSafely(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (urlEnd !== -1) {
|
if (urlEnd !== -1) {
|
||||||
const urlContent = markdown.slice(urlStart, urlEnd)
|
|
||||||
console.log('[nostrUriResolver] Link #' + linksFound + ' - URL at', urlStart, '-', urlEnd, 'content:', urlContent.slice(0, 150))
|
|
||||||
|
|
||||||
// Check if this URL contains nostr URIs
|
|
||||||
const containsNostrUri = /(?:nostr:)?(npub|note|nprofile|nevent|naddr)1/i.test(urlContent)
|
|
||||||
if (containsNostrUri) {
|
|
||||||
console.warn('[nostrUriResolver] ⚠️ WARNING: Link URL contains nostr URI! This should be protected:', urlContent.slice(0, 200))
|
|
||||||
}
|
|
||||||
|
|
||||||
linkRanges.push({
|
linkRanges.push({
|
||||||
start: urlStart,
|
start: urlStart,
|
||||||
end: urlEnd
|
end: urlEnd
|
||||||
@@ -200,40 +165,21 @@ function replaceNostrUrisSafely(
|
|||||||
|
|
||||||
i = urlEnd + 1
|
i = urlEnd + 1
|
||||||
} else {
|
} else {
|
||||||
console.warn('[nostrUriResolver] ⚠️ Could not find matching ) for ]( at position', urlStartMatch)
|
|
||||||
// No matching closing paren found, skip this one
|
// No matching closing paren found, skip this one
|
||||||
i = urlStart + 1
|
i = urlStart + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[nostrUriResolver] Total markdown links found:', linksFound)
|
|
||||||
console.log('[nostrUriResolver] Total link URL ranges tracked:', linkRanges.length)
|
|
||||||
if (linkRanges.length > 0) {
|
|
||||||
console.log('[nostrUriResolver] Link ranges:', linkRanges.map(r => `${r.start}-${r.end}`).join(', '))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if a position is inside any markdown link URL
|
// Check if a position is inside any markdown link URL
|
||||||
const isInsideLinkUrl = (pos: number): boolean => {
|
const isInsideLinkUrl = (pos: number): boolean => {
|
||||||
const inside = linkRanges.some(range => pos >= range.start && pos < range.end)
|
return linkRanges.some(range => pos >= range.start && pos < range.end)
|
||||||
if (inside) {
|
|
||||||
const matchingRange = linkRanges.find(range => pos >= range.start && pos < range.end)
|
|
||||||
console.log('[nostrUriResolver] Position', pos, 'is inside link URL range', matchingRange)
|
|
||||||
}
|
|
||||||
return inside
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace nostr URIs, but skip those inside link URLs
|
// Replace nostr URIs, but skip those inside link URLs
|
||||||
// Also check if nostr URI is part of any URL pattern (http/https URLs)
|
// Also check if nostr URI is part of any URL pattern (http/https URLs)
|
||||||
// Callback params: (match, encoded, type, offset, string)
|
// Callback params: (match, encoded, type, offset, string)
|
||||||
let nostrMatchCount = 0
|
|
||||||
const result = markdown.replace(NOSTR_URI_REGEX, (match, encoded, _type, offset, fullString) => {
|
const result = markdown.replace(NOSTR_URI_REGEX, (match, encoded, _type, offset, fullString) => {
|
||||||
nostrMatchCount++
|
|
||||||
const matchEnd = offset + match.length
|
const matchEnd = offset + match.length
|
||||||
console.log('[nostrUriResolver] Found nostr URI match #' + nostrMatchCount)
|
|
||||||
console.log('[nostrUriResolver] - Match:', match)
|
|
||||||
console.log('[nostrUriResolver] - Encoded:', encoded)
|
|
||||||
console.log('[nostrUriResolver] - Position:', offset, 'to', matchEnd)
|
|
||||||
console.log('[nostrUriResolver] - Context around match:', fullString.slice(Math.max(0, offset - 50), matchEnd + 50))
|
|
||||||
|
|
||||||
// Check if this match is inside a markdown link URL
|
// Check if this match is inside a markdown link URL
|
||||||
// Check both start and end positions to ensure we catch the whole match
|
// Check both start and end positions to ensure we catch the whole match
|
||||||
@@ -241,14 +187,6 @@ function replaceNostrUrisSafely(
|
|||||||
const endInside = isInsideLinkUrl(matchEnd - 1) // Check end position
|
const endInside = isInsideLinkUrl(matchEnd - 1) // Check end position
|
||||||
|
|
||||||
if (startInside || endInside) {
|
if (startInside || endInside) {
|
||||||
const range = linkRanges.find(r =>
|
|
||||||
(offset >= r.start && offset < r.end) ||
|
|
||||||
(matchEnd - 1 >= r.start && matchEnd - 1 < r.end)
|
|
||||||
)
|
|
||||||
console.log('[nostrUriResolver] SKIPPING replacement - inside markdown link URL')
|
|
||||||
console.log('[nostrUriResolver] - Match range:', offset, 'to', matchEnd)
|
|
||||||
console.log('[nostrUriResolver] - Link URL range:', range)
|
|
||||||
console.log('[nostrUriResolver] - Link URL content:', range ? markdown.slice(range.start, range.end).slice(0, 200) : 'N/A')
|
|
||||||
// Don't replace - return original match
|
// Don't replace - return original match
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
@@ -266,32 +204,15 @@ function replaceNostrUrisSafely(
|
|||||||
const isValidUrlContinuation = !contextAfter.match(/^[\s)]/) // Not followed by space or closing paren
|
const isValidUrlContinuation = !contextAfter.match(/^[\s)]/) // Not followed by space or closing paren
|
||||||
|
|
||||||
if (isInHttpUrl && isValidUrlContinuation) {
|
if (isInHttpUrl && isValidUrlContinuation) {
|
||||||
console.log('[nostrUriResolver] SKIPPING replacement - appears to be part of HTTP URL')
|
|
||||||
console.log('[nostrUriResolver] - Context before:', contextBefore.slice(-80))
|
|
||||||
console.log('[nostrUriResolver] - Context after:', contextAfter)
|
|
||||||
// Don't replace - return original match
|
// Don't replace - return original match
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[nostrUriResolver] REPLACING nostr URI (NOT inside any link URL or HTTP URL)')
|
|
||||||
// encoded is already the NIP-19 identifier without nostr: prefix (from capture group)
|
// encoded is already the NIP-19 identifier without nostr: prefix (from capture group)
|
||||||
const replacement = getReplacement(encoded)
|
const replacement = getReplacement(encoded)
|
||||||
console.log('[nostrUriResolver] - Replacement:', replacement)
|
|
||||||
return replacement
|
return replacement
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('[nostrUriResolver] Processing complete. Total nostr matches:', nostrMatchCount)
|
|
||||||
console.log('[nostrUriResolver] Result length:', result.length)
|
|
||||||
console.log('[nostrUriResolver] Result preview:', result.slice(0, 500))
|
|
||||||
|
|
||||||
// Check if we created nested markdown links (this would indicate a problem)
|
|
||||||
const nestedLinkPattern = /\]\([^)]*\[[^\]]+\]\([^)]+\)[^)]*\)/g
|
|
||||||
const nestedLinks = result.match(nestedLinkPattern)
|
|
||||||
if (nestedLinks && nestedLinks.length > 0) {
|
|
||||||
console.error('[nostrUriResolver] ⚠️⚠️⚠️ ERROR: Created nested markdown links! This indicates a bug:', nestedLinks.slice(0, 3))
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('[nostrUriResolver] ===== MARKDOWN PROCESSING COMPLETE =====')
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,7 +221,6 @@ function replaceNostrUrisSafely(
|
|||||||
* This converts: nostr:npub1... to [label](link)
|
* This converts: nostr:npub1... to [label](link)
|
||||||
*/
|
*/
|
||||||
export function replaceNostrUrisInMarkdown(markdown: string): string {
|
export function replaceNostrUrisInMarkdown(markdown: string): string {
|
||||||
console.log('[nostrUriResolver] replaceNostrUrisInMarkdown called')
|
|
||||||
return replaceNostrUrisSafely(markdown, (encoded) => {
|
return replaceNostrUrisSafely(markdown, (encoded) => {
|
||||||
const link = createNostrLink(encoded)
|
const link = createNostrLink(encoded)
|
||||||
const label = getNostrUriLabel(encoded)
|
const label = getNostrUriLabel(encoded)
|
||||||
@@ -318,7 +238,6 @@ export function replaceNostrUrisInMarkdownWithTitles(
|
|||||||
markdown: string,
|
markdown: string,
|
||||||
articleTitles: Map<string, string>
|
articleTitles: Map<string, string>
|
||||||
): string {
|
): string {
|
||||||
console.log('[nostrUriResolver] replaceNostrUrisInMarkdownWithTitles called, articleTitles:', articleTitles.size)
|
|
||||||
return replaceNostrUrisSafely(markdown, (encoded) => {
|
return replaceNostrUrisSafely(markdown, (encoded) => {
|
||||||
const link = createNostrLink(encoded)
|
const link = createNostrLink(encoded)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user