diff options
| -rw-r--r-- | src/features/messages.js | 40 | ||||
| -rw-r--r-- | tools/link-preview-test.js | 16 |
2 files changed, 40 insertions, 16 deletions
diff --git a/src/features/messages.js b/src/features/messages.js index dbbe05e..f2a3f8e 100644 --- a/src/features/messages.js +++ b/src/features/messages.js @@ -7514,11 +7514,12 @@ export function messagesFeature(ctx) { // the first two plain links of a post (a wall of links is a list, not cards) function notePreviewUrls(content) { const urls = []; - for (const part of String(content || '').split(NOTE_SPLIT)) { - if (!part || urls.length >= 2) continue; - const md = MD_PARTS.exec(part); - const url = md ? md[3] : partUrl(part); - if (url && !(md && md[1]) && previewable(url) && !PACK_LINK_RE.test(url)) urls.push(url); + const parts = String(content || '').split(NOTE_SPLIT); + for (let i = 1; i < parts.length && urls.length < 2; i += 2) { + const part = parts[i]; + if (!part || MD_PARTS.test(part) || !linkAlone(parts, i)) continue; // a [label](url) is a link by its author's choice + const url = partUrl(part); + if (url && previewable(url) && !PACK_LINK_RE.test(url)) urls.push(url); } return urls; } @@ -8514,6 +8515,18 @@ export function messagesFeature(ctx) { }; // the https URL a split part stands for, written in full or bare const partUrl = (part) => (/^https?:\/\//i.test(part || '') ? part : bareLink(part)?.url || null); + // A link becomes a card (a page preview, a PAFF player) only standing on a + // line of its own. One in the middle of a sentence, or with punctuation + // after it, belongs to the sentence and stays a link ("can i also create a + // PR on <link>?" painted a big card between "on" and "?", Adam 2026-12-05). + // `parts` is a NOTE_SPLIT result: text at even indexes, matches at odd. + const linkAlone = (parts, i) => { + const part = parts[i] || '', before = parts[i - 1] ?? '', after = parts[i + 1] ?? ''; + if (/^https?:\/\//i.test(part) ? /[.,;:!?'"]$|\)$(?<=^[^(]*)/.test(part) : bareLink(part)?.tail) return false; + if (before === '' ? i > 1 : !/(^|\n)[ \t]*$/.test(before)) return false; + if (after === '' ? i + 1 < parts.length - 1 : !/^[ \t]*(\n|$)/.test(after)) return false; + return true; + }; const NOTE_SPLIT = new RegExp('(' + MD_LINK + '|https?:\\/\\/[^\\s]+|wss?:\\/\\/[^\\s]+|nostr:(?:npub|nprofile|note|nevent|naddr)1[a-z0-9]+|' + BARE_KEY + '|' + MENTION + '|' + HASHTAG + '|' + BARE_URL + ')', 'gi'); const MD_PARTS = new RegExp('^(!?)\\[([^\\]\\n]{0,300})\\]\\(\\s*<?(https?:\\/\\/[^\\s>)]+)>?[^)\\n]{0,300}\\)$', 'i'); @@ -11050,7 +11063,8 @@ export function messagesFeature(ctx) { // A bio's links stay links: no page cards or PAFF players in a profile header. let bareLinks = false; const bareLinkBody = (text) => { bareLinks = true; try { return noteBody(text); } finally { bareLinks = false; } }; - function urlNode(url, { label = null, isImage = false, shown: written = null } = {}) { + function urlNode(url, { label = null, isImage = false, shown: written = null, card = true } = {}) { + if (bareLinks) card = false; if (isVideoUrl(url)) { return videoNode(url, { stable: !!feedPaint }); } @@ -11084,7 +11098,7 @@ export function messagesFeature(ctx) { }); } if (youtubeId(url)) return youtubeEmbed(url, youtubeId(url)); - if (!label && !bareLinks && paffMusicUrl(url)) { + if (!label && card && paffMusicUrl(url)) { let pv = previews.get(url) || null; if (feedPaint) { if (!feedPaint.media.has('pv:' + url)) feedPaint.media.set('pv:' + url, pv); @@ -11092,7 +11106,7 @@ export function messagesFeature(ctx) { } return paffSongCard(url, pv); } - if (!label && !bareLinks && paffEmbedUrl(url)) { + if (!label && card && paffEmbedUrl(url)) { let pv = previews.get(url) || null; if (feedPaint) { if (!feedPaint.media.has('pv:' + url)) feedPaint.media.set('pv:' + url, pv); @@ -11100,7 +11114,7 @@ export function messagesFeature(ctx) { } return paffEmbed(url, pv); } - if (!label && !bareLinks && previewable(url)) { + if (!label && card && previewable(url)) { // a frozen feed row keeps the card (or the bare link) it first painted let pv = previews.get(url) || null; if (feedPaint) { @@ -11447,7 +11461,9 @@ export function messagesFeature(ctx) { function noteBody(text, depth = 0, em = null) { const out = []; const emojiAt = em && em.size ? (c) => em.get(c) : null; - for (const part of String(text || '').split(NOTE_SPLIT)) { + const parts = String(text || '').split(NOTE_SPLIT); + for (let i = 0; i < parts.length; i++) { + const part = parts[i]; if (!part) continue; const md = MD_PARTS.exec(part); if (md) { @@ -11455,10 +11471,10 @@ export function messagesFeature(ctx) { out.push(urlNode(url, { label: label || null, isImage: !!bang })); } else if (/^https?:\/\//i.test(part)) { const pack = PACK_LINK_RE.exec(part); - out.push(pack ? packLinkNode(part, pack[1]) : urlNode(part)); + out.push(pack ? packLinkNode(part, pack[1]) : urlNode(part, { card: linkAlone(parts, i) })); } else if (bareLink(part)) { const { url, text, tail } = bareLink(part); - out.push(urlNode(url, { shown: text })); + out.push(urlNode(url, { shown: text, card: linkAlone(parts, i) })); if (tail) out.push(tail); } else if (/^wss?:\/\//i.test(part)) { // a relay named in a post opens what's on it (the raw firehose) diff --git a/tools/link-preview-test.js b/tools/link-preview-test.js index 2eeac7d..934c570 100644 --- a/tools/link-preview-test.js +++ b/tools/link-preview-test.js @@ -16,11 +16,14 @@ const AUTHOR = generateSecretKey(); const APK = getPublicKey(AUTHOR); const now = Math.floor(Date.now() / 1000); const PNG = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==', 'base64'); const POSTS = [ - finalizeEvent({ kind: 1, created_at: now - 30, tags: [], content: 'Well, yeah https://news.example/story/1' }, AUTHOR), - finalizeEvent({ kind: 1, created_at: now - 60, tags: [], content: 'nothing here https://blank.example/page' }, AUTHOR), + finalizeEvent({ kind: 1, created_at: now - 30, tags: [], content: 'Well, yeah\n https://news.example/story/1 \nso there' }, AUTHOR), + finalizeEvent({ kind: 1, created_at: now - 60, tags: [], content: 'nothing here\nhttps://blank.example/page' }, AUTHOR), finalizeEvent({ kind: 1, created_at: now - 90, tags: [], content: 'a picture http://localhost:5311/pic.png' }, AUTHOR), - finalizeEvent({ kind: 1, created_at: now - 120, tags: [], content: 'film night https://paff.club/movie/1382-hackers-1995/play?q=1080p&t=4910' }, AUTHOR), - finalizeEvent({ kind: 1, created_at: now - 150, tags: [], content: 'on repeat https://paff.club/song/abc123' }, AUTHOR), + finalizeEvent({ kind: 1, created_at: now - 120, tags: [], content: 'film night\nhttps://paff.club/movie/1382-hackers-1995/play?q=1080p&t=4910' }, AUTHOR), + finalizeEvent({ kind: 1, created_at: now - 160, tags: [], content: 'can i make a PR on https://news.example/story/1 too' }, AUTHOR), + finalizeEvent({ kind: 1, created_at: now - 170, tags: [], content: 'is this it?\nhttps://news.example/story/1?' }, AUTHOR), + finalizeEvent({ kind: 1, created_at: now - 180, tags: [], content: 'see news.example/story/1.' }, AUTHOR), + finalizeEvent({ kind: 1, created_at: now - 150, tags: [], content: 'on repeat\n\nhttps://paff.club/song/abc123' }, AUTHOR), ]; const PROFILE = finalizeEvent({ kind: 0, created_at: now - 100, tags: [], content: JSON.stringify({ name: 'Linker', picture: 'http://localhost:5311/face.png', about: 'Read me at https://news.example/story/1 or https://paff.club/movie/1382-hackers-1995/play?q=1080p&t=4910' }) }, AUTHOR); @@ -138,6 +141,11 @@ try { check('...with the page\'s title', story?.title === 'Lawmakers doubt the plan', story?.title); check('...and its picture, already decoded', !!story?.thumb && story.thumbShown, JSON.stringify({ thumb: story?.thumb, shown: story?.thumbShown })); check('a page with nothing to show stays a bare link', !!blank && blank.bareLink && !blank.card, JSON.stringify(blank)); + // a link inside a sentence, or with punctuation after it, stays a link (Adam, 2026-12-05) + for (const [name, re] of [['mid-sentence', /make a PR on/], ['with a ? after it', /is this it/], ['bare, with a . after it', /^Linker.*see news/]]) { + const r = rows.find((x) => re.test(x.text)); + check('a link ' + name + ' stays a link, no card', !!r && !r.card, JSON.stringify(r)); + } check('a picture link is still a picture, not a card', !!pic && pic.img && !pic.card, JSON.stringify(pic)); // a PAFF film link plays in the post, PAFF's bare player loaded only on a tap const paff = await page.evaluate(() => { |
