diff options
| author | Adam Soltys <[email protected]> | 2026-12-05 10:19:56 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-12-05 10:19:56 -0700 |
| commit | e694a17af529614ba2cb477b01d0d5d12eb20072 (patch) | |
| tree | 64d2a31ca37d6482989dd865bfa50c1e179df76d /src | |
| parent | 1e5c781b85d7cf9922914c57ddbaf9acfd075042 (diff) | |
| download | coinosv3-e694a17af529614ba2cb477b01d0d5d12eb20072.tar.gz coinosv3-e694a17af529614ba2cb477b01d0d5d12eb20072.zip | |
A link becomes a card only on a line of its own. "maybe we should also detect if a link is in the middle of a sentence or has punctuation after it we shouldn't make it into a preview. only if it was on its own line?" (Adam, 2026-12-05; "can i also create a PR on <gitworkshop link>?" painted a big card between "on" and "?"). A page preview or PAFF player now needs its link alone on its line (spaces around it fine) with no punctuation after it (. , ; : ! ? quotes, or a ) the link didn't open); otherwise it stays a plain link, and its preview isn't even fetched. Pictures, videos and YouTube are unchanged. link-preview-test gains mid-sentence, trailing ? and bare-link-with-. cases; post-markdown, feed-long-note, feed-mention-tap and features-smoke pass.HEADmaster
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6UBzkUjWrGu31fqD5d
Diffstat (limited to 'src')
| -rw-r--r-- | src/features/messages.js | 40 |
1 files changed, 28 insertions, 12 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) |
