From 08a8f5623ae8e8947994cc41a17edd8359f7c67d Mon Sep 17 00:00:00 2001 From: Gigi Date: Sat, 1 Nov 2025 00:19:43 +0100 Subject: [PATCH] debug: add comprehensive logging to diagnose timing issue with link processing Added extensive debug logs to track: - Input markdown preview and existing link count - Each markdown link found with context and content - Warnings when link URLs contain nostr URIs (should be protected) - Detailed position information for each nostr URI match - Whether matches are correctly identified as inside/outside link URLs - Detection of nested markdown links in result (indicates bug) This will help diagnose the timing issue where processing sometimes works and sometimes doesn't. --- src/utils/nostrUriResolver.tsx | 63 +++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/src/utils/nostrUriResolver.tsx b/src/utils/nostrUriResolver.tsx index 0163b515..64b002ee 100644 --- a/src/utils/nostrUriResolver.tsx +++ b/src/utils/nostrUriResolver.tsx @@ -115,7 +115,13 @@ function replaceNostrUrisSafely( markdown: string, getReplacement: (encoded: string) => string ): string { - console.log('[nostrUriResolver] Starting markdown processing, length:', markdown.length) + 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) // Track positions where we're inside a markdown link URL // Use a parser approach to correctly handle URLs with brackets/parentheses @@ -123,13 +129,19 @@ function replaceNostrUrisSafely( // Find all markdown link URLs by looking for ]( pattern and tracking to matching ) let i = 0 + let linksFound = 0 while (i < markdown.length) { // Look for ]( pattern that starts a markdown link URL const urlStartMatch = markdown.indexOf('](', i) if (urlStartMatch === -1) break + linksFound++ 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 // We need to account for nested parentheses and escaped characters let pos = urlStart @@ -161,7 +173,13 @@ function replaceNostrUrisSafely( if (urlEnd !== -1) { const urlContent = markdown.slice(urlStart, urlEnd) - console.log('[nostrUriResolver] Found markdown link URL at', urlStart, '-', urlEnd, 'content:', urlContent.slice(0, 100)) + 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({ start: urlStart, @@ -170,12 +188,17 @@ function replaceNostrUrisSafely( i = urlEnd + 1 } else { + console.warn('[nostrUriResolver] ⚠️ Could not find matching ) for ]( at position', urlStartMatch) // No matching closing paren found, skip this one i = urlStart + 1 } } - console.log('[nostrUriResolver] Total link ranges tracked:', linkRanges.length) + 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 const isInsideLinkUrl = (pos: number): boolean => { @@ -190,21 +213,37 @@ function replaceNostrUrisSafely( // Replace nostr URIs, but skip those inside link URLs // Callback params: (match, encoded, type, offset, string) let nostrMatchCount = 0 - const result = markdown.replace(NOSTR_URI_REGEX, (match, encoded, _type, offset) => { + const result = markdown.replace(NOSTR_URI_REGEX, (match, encoded, _type, offset, fullString) => { nostrMatchCount++ - console.log('[nostrUriResolver] Found nostr URI match #' + nostrMatchCount + ' at offset', offset + ':', match, 'encoded:', encoded) + 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 - 30), matchEnd + 30)) // Check if this match is inside a markdown link URL - if (isInsideLinkUrl(offset)) { + // Check both start and end positions to ensure we catch the whole match + const startInside = isInsideLinkUrl(offset) + const endInside = isInsideLinkUrl(matchEnd - 1) // Check end position + + 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 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 return match } - console.log('[nostrUriResolver] REPLACING nostr URI') + console.log('[nostrUriResolver] REPLACING nostr URI (NOT inside any link URL)') // encoded is already the NIP-19 identifier without nostr: prefix (from capture group) const replacement = getReplacement(encoded) - console.log('[nostrUriResolver] Replacement:', replacement) + console.log('[nostrUriResolver] - Replacement:', replacement) return replacement }) @@ -212,6 +251,14 @@ function replaceNostrUrisSafely( 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 }