Skip to content

Commit 44e0164

Browse files
committed
fix(platforms): ignore misaligned facets
1 parent 6ede14d commit 44e0164

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

packages/platforms/src/platforms/twitter.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,27 @@ function enrichText(raw?: RawText) {
2323
const end = offsets.index(facet.indices[1]);
2424
if (start < index || end <= start) continue;
2525
if (start < displayStart || displayEnd < end) continue;
26+
const facetText = raw.text.slice(start, end);
27+
if (!facetMatchesText(facet, facetText)) continue;
2628

2729
rendered += raw.text.slice(index, start);
2830
index = end;
2931

3032
if (facet.type === "url") {
31-
rendered += renderUrlFacet(facet, raw.text.slice(start, end));
33+
rendered += renderUrlFacet(facet, facetText);
3234
}
3335
if (facet.type === "hashtag") {
34-
rendered += renderHashtagFacet(facet, raw.text.slice(start, end));
36+
rendered += renderHashtagFacet(facet, facetText);
3537
}
3638
if (facet.type === "media") {
3739
continue;
3840
}
3941
if (facet.type === "mention") {
40-
rendered += renderMentionFacet(facet, raw.text.slice(start, end));
42+
rendered += renderMentionFacet(facet, facetText);
4143
}
4244

4345
if (!RENDERED_FACET_TYPES.includes(facet.type)) {
44-
rendered += raw.text.slice(start, end);
46+
rendered += facetText;
4547
}
4648
}
4749

@@ -90,15 +92,27 @@ function scoreOffsets(raw: RawText, offsets: { index(offset: number): number })
9092
const text = raw.text.slice(start, end);
9193

9294
if (end <= start) score -= 1;
93-
if (facet.type === "hashtag" && text === `#${facet.original}`) score += 3;
94-
if (facet.type === "mention" && text === `@${facet.original}`) score += 3;
95-
if (facet.type === "url" && /^https?:\/\//.test(text)) score += 2;
96-
if (facet.type === "media" && /^https?:\/\//.test(text)) score += 2;
95+
if (!facetMatchesText(facet, text)) continue;
96+
if (facet.type === "hashtag" || facet.type === "mention") score += 3;
97+
if (facet.type === "url" || facet.type === "media") score += 2;
9798
}
9899

99100
return score;
100101
}
101102

103+
function facetMatchesText(facet: RawText["facets"][number], text: string) {
104+
if (facet.type === "url" || facet.type === "media") {
105+
return facet.original ? text === facet.original : /^https?:\/\//.test(text);
106+
}
107+
if (facet.type === "hashtag") {
108+
return facet.original ? text === `#${facet.original}` : text.startsWith("#");
109+
}
110+
if (facet.type === "mention") {
111+
return facet.original ? text === `@${facet.original}` : text.startsWith("@");
112+
}
113+
return true;
114+
}
115+
102116
function renderUrlFacet(facet: RawText["facets"][number], text: string) {
103117
if (facet.replacement) return facet.replacement;
104118
if (!facet.display) return text;

0 commit comments

Comments
 (0)