From 8c3b448291cf19eea0b8e549db573b3c3b286515 Mon Sep 17 00:00:00 2001 From: rosethornbush <31735267+rosethornbush@users.noreply.github.com> Date: Mon, 29 Jun 2026 01:25:58 -0700 Subject: [PATCH] fix: render twitter facets by index --- packages/platforms/src/platforms/twitter.ts | 37 +++++++++++---------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/packages/platforms/src/platforms/twitter.ts b/packages/platforms/src/platforms/twitter.ts index 94812a9..aad8cee 100644 --- a/packages/platforms/src/platforms/twitter.ts +++ b/packages/platforms/src/platforms/twitter.ts @@ -10,32 +10,35 @@ const MAX_CONTEXT_DEPTH = 1; function enrichText(raw?: RawText) { if (!raw) return undefined; - let text = raw.text; - for (const facet of raw.facets) { + let text = ""; + let ind = 0; + + for (const facet of raw.facets.toSorted((a, b) => a.indices[0] - b.indices[0])) { + const [start, end] = facet.indices; + if (start < ind) continue; + + text += raw.text.slice(ind, start); + ind = end; + if (facet.type === "url") { - const source = - facet.original && text.includes(facet.original) ? facet.original : facet.display; - if (source && facet.replacement) { - text = text.replace(source, facet.replacement); - } + text += facet.replacement ?? raw.text.slice(start, end); } if (facet.type === "hashtag") { - text = text.replace( - `#${facet.original}`, - `[#${facet.original}](https://x.com/hashtag/${facet.original})`, - ); + text += `[#${facet.original}](https://x.com/hashtag/${facet.original})`; } if (facet.type === "media") { - text = text.replace(facet.original!, ""); + continue; } if (facet.type === "mention") { - text = text.replace( - new RegExp(`@${facet.original}`, "i"), - `[@${facet.original}](https://x.com/${facet.original})`, - ); + text += `[@${facet.original}](https://x.com/${facet.original})`; + } + + if (!["url", "hashtag", "media", "mention"].includes(facet.type)) { + text += raw.text.slice(start, end); } } - return he.decode(text); + + return he.decode(text + raw.text.slice(ind)); } function resolveMediaUrl(media: { type: string; url?: string }) {