birdclaw 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (247) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +10 -8
  3. package/bin/birdclaw.mjs +4 -66
  4. package/dist/cli/birdclaw.js +28165 -0
  5. package/dist/client/assets/AvatarChip-DLoqM5xD.js +1 -0
  6. package/dist/client/assets/ProfilePreview-BejAuYsJ.js +1 -0
  7. package/dist/client/assets/SavedTimelineView-BIS7HKpp.js +1 -0
  8. package/dist/client/assets/SmartTimestamp-BR0-9se2.js +1 -0
  9. package/dist/client/assets/TimelineRouteFrame-CyGPj0eM.js +1 -0
  10. package/dist/client/assets/blocks-7UDKQp8H.js +1 -0
  11. package/dist/client/assets/bookmarks-CbBkMvXk.js +1 -0
  12. package/dist/client/assets/chunk-C0phanGV.js +1 -0
  13. package/dist/client/assets/circle-check-C_Vq3ElD.js +1 -0
  14. package/dist/client/assets/client-stream-contracts-ys3XhWG4.js +5 -0
  15. package/dist/client/assets/createLucideIcon-JV8isFFR.js +1 -0
  16. package/dist/client/assets/data-sources-Cm2bpNvg.js +1 -0
  17. package/dist/client/assets/discuss-DTjaOugw.js +1 -0
  18. package/dist/client/assets/dms-CGd6Eogq.js +1 -0
  19. package/dist/client/assets/inbox-BHlGuOy2.js +1 -0
  20. package/dist/client/assets/index-CEIG7S6s.js +31 -0
  21. package/dist/client/assets/index-bpFrQ5yG.css +1 -0
  22. package/dist/client/assets/likes-cLrcAQem.js +1 -0
  23. package/dist/client/assets/links-CM9QO1LC.js +1 -0
  24. package/dist/client/assets/mapbox-BxTlqXt9.js +2 -0
  25. package/dist/client/assets/mapbox-gl-dgf_hX3a.js +3215 -0
  26. package/dist/client/assets/mentions-CxKCgzIf.js +1 -0
  27. package/dist/client/assets/network-map-B5a3whqj.js +2 -0
  28. package/dist/client/assets/profile-analyze-CRtNEwlb.js +1 -0
  29. package/dist/client/assets/profile-hydration-client-YEtJsNTv.js +1 -0
  30. package/dist/client/assets/profiles._handle-C82_HoKX.js +1 -0
  31. package/dist/client/assets/query-client-fYu2rzRC.js +17 -0
  32. package/dist/client/assets/rate-limits-ScaEwdVK.js +1 -0
  33. package/dist/client/assets/react-dom-DHl6Ug7F.js +1 -0
  34. package/dist/client/assets/react-nBylq1U5.js +1 -0
  35. package/dist/client/assets/repeat-2-xCwHM9hD.js +1 -0
  36. package/dist/client/assets/routes-q7ZaW4En.js +1 -0
  37. package/dist/client/assets/schemas-I9r6F6ya.js +64 -0
  38. package/dist/client/assets/search-Dq5kYsxA.js +1 -0
  39. package/dist/client/assets/sparkles-CQVlvHsZ.js +1 -0
  40. package/dist/client/assets/styles-B8s3tt3r.css +2 -0
  41. package/dist/client/assets/today-CeG1O7Py.js +1 -0
  42. package/dist/client/assets/triangle-alert-CBL7s2pf.js +1 -0
  43. package/dist/client/assets/useTimelineRouteData-1HsqgvUi.js +1 -0
  44. package/dist/client/assets/users-CHbKlqUr.js +1 -0
  45. package/dist/server/assets/AvatarChip-zUDAEmjL.js +105 -0
  46. package/dist/server/assets/FeedState-ntW0Q2GB.js +214 -0
  47. package/dist/server/assets/ProfileAnalysisStream-BFvhVdhn.js +198 -0
  48. package/dist/server/assets/ProfilePreview-C6gAor_j.js +752 -0
  49. package/dist/server/assets/SavedTimelineView-BG1jnBp1.js +64 -0
  50. package/dist/server/assets/SmartTimestamp-DfwE1NGf.js +45 -0
  51. package/dist/server/assets/SyncNowButton-BOn_IBXS.js +76 -0
  52. package/dist/server/assets/TimelineRouteFrame-CU7ilM7U.js +85 -0
  53. package/dist/server/assets/TweetRichText-DhCVv1-6.js +74 -0
  54. package/dist/server/assets/_tanstack-start-manifest_v-C-ZRnG_n.js +148 -0
  55. package/dist/server/assets/account-selection-CTdFGWRk.js +44 -0
  56. package/dist/server/assets/blocks-C1mgGTB3.js +299 -0
  57. package/dist/server/assets/blocks-DSgQghqP.js +17 -0
  58. package/dist/server/assets/bookmarks-BJN8tykY.js +14 -0
  59. package/dist/server/assets/client-stream-contracts-C2xbWVjm.js +769 -0
  60. package/dist/server/assets/data-sources-BnA1Q94p.js +180 -0
  61. package/dist/server/assets/discuss-CJ1dau1j.js +15 -0
  62. package/dist/server/assets/discuss-QTo08NCN.js +338 -0
  63. package/dist/server/assets/dms-BuLj4bp8.js +368 -0
  64. package/dist/server/assets/dms-DAyQrVM2.js +240 -0
  65. package/dist/server/assets/empty-plugin-adapters-DzrEkdBF.js +5 -0
  66. package/dist/server/assets/inbox-CkGOaW1q.js +214 -0
  67. package/dist/server/assets/inbox-DpWd1EzC.js +146 -0
  68. package/dist/server/assets/likes-DBOAtsJU.js +14 -0
  69. package/dist/server/assets/links-DAhSjynu.js +240 -0
  70. package/dist/server/assets/links-DmH5lxHO.js +468 -0
  71. package/dist/server/assets/mentions-BH2MzKtB.js +26 -0
  72. package/dist/server/assets/network-map-D3CzRcC4.js +224 -0
  73. package/dist/server/assets/network-map-D5DoLOwR.js +582 -0
  74. package/dist/server/assets/profile-analyze-BP65PED0.js +90 -0
  75. package/dist/server/assets/profile-analyze-C1NoM0tV.js +9 -0
  76. package/dist/server/assets/profile-hydration-client-GJ4kwJS8.js +133 -0
  77. package/dist/server/assets/profiles._handle-DjwlN_ac.js +184 -0
  78. package/dist/server/assets/profiles._handle-DsLNj2Ca.js +12 -0
  79. package/dist/server/assets/query-client-BdauHBgd.js +923 -0
  80. package/dist/server/assets/rate-limits-VAr2qzYD.js +262 -0
  81. package/dist/server/assets/route-search-BUlQ5lGM.js +117 -0
  82. package/dist/server/assets/router-DqkwSPiM.js +17026 -0
  83. package/dist/server/assets/routes-A9c9ZhwN.js +26 -0
  84. package/dist/server/assets/start-FaDOTmto.js +4 -0
  85. package/dist/server/assets/today-B5hVM57Z.js +17 -0
  86. package/dist/server/assets/today-fvGiZ7ud.js +303 -0
  87. package/dist/server/assets/ui-qbmcMSSX.js +137 -0
  88. package/{src/components/useDebouncedValue.ts → dist/server/assets/useDebouncedValue-DOnA6Xcy.js} +4 -4
  89. package/dist/server/assets/useTimelineRouteData-27cxynxa.js +1101 -0
  90. package/dist/server/server.js +5520 -0
  91. package/package.json +27 -34
  92. package/playwright.config.ts +0 -35
  93. package/scripts/browser-perf.mjs +0 -427
  94. package/scripts/build-docs-site.mjs +0 -967
  95. package/scripts/docs-site-assets.mjs +0 -311
  96. package/scripts/run-vitest.mjs +0 -21
  97. package/scripts/sanitize-node-options.mjs +0 -23
  98. package/scripts/start-test-server.mjs +0 -42
  99. package/src/cli-moderation.ts +0 -210
  100. package/src/cli.ts +0 -2336
  101. package/src/components/AccountSwitcher.tsx +0 -186
  102. package/src/components/AppNav.tsx +0 -118
  103. package/src/components/AvatarChip.tsx +0 -58
  104. package/src/components/BrandMark.tsx +0 -67
  105. package/src/components/ConversationThread.tsx +0 -131
  106. package/src/components/DmWorkspace.tsx +0 -286
  107. package/src/components/EmbeddedTweetCard.tsx +0 -55
  108. package/src/components/FeedState.tsx +0 -147
  109. package/src/components/FloatingPreview.tsx +0 -382
  110. package/src/components/InboxCard.tsx +0 -152
  111. package/src/components/LinkPreviewCard.tsx +0 -292
  112. package/src/components/MarkdownViewer.tsx +0 -777
  113. package/src/components/ProfileAnalysisStream.tsx +0 -428
  114. package/src/components/ProfilePreview.tsx +0 -129
  115. package/src/components/SavedTimelineView.tsx +0 -155
  116. package/src/components/SmartTimestamp.tsx +0 -72
  117. package/src/components/SyncNowButton.tsx +0 -140
  118. package/src/components/ThemeSlider.tsx +0 -80
  119. package/src/components/TimelineCard.tsx +0 -533
  120. package/src/components/TimelineRouteFrame.tsx +0 -186
  121. package/src/components/TweetArticleCard.tsx +0 -66
  122. package/src/components/TweetMediaGrid.tsx +0 -185
  123. package/src/components/TweetRichText.tsx +0 -159
  124. package/src/components/account-selection.ts +0 -64
  125. package/src/components/useTimelineRouteData.ts +0 -323
  126. package/src/lib/account-sync-job.ts +0 -666
  127. package/src/lib/actions-transport.ts +0 -300
  128. package/src/lib/api-client.ts +0 -419
  129. package/src/lib/archive-finder.ts +0 -185
  130. package/src/lib/archive-import.ts +0 -2764
  131. package/src/lib/authored-live.ts +0 -1072
  132. package/src/lib/avatar-cache.ts +0 -349
  133. package/src/lib/backup.ts +0 -2741
  134. package/src/lib/bird-actions.ts +0 -294
  135. package/src/lib/bird-command.ts +0 -130
  136. package/src/lib/bird.ts +0 -1140
  137. package/src/lib/blocklist.ts +0 -136
  138. package/src/lib/blocks-write.ts +0 -167
  139. package/src/lib/blocks.ts +0 -389
  140. package/src/lib/bookmark-sync-job.ts +0 -536
  141. package/src/lib/client-cache.ts +0 -109
  142. package/src/lib/config.ts +0 -224
  143. package/src/lib/conversation-surface.ts +0 -205
  144. package/src/lib/data-sources.ts +0 -219
  145. package/src/lib/db.ts +0 -1135
  146. package/src/lib/dms-live.ts +0 -933
  147. package/src/lib/effect-runtime.ts +0 -45
  148. package/src/lib/follow-graph.ts +0 -1097
  149. package/src/lib/geocoding.ts +0 -296
  150. package/src/lib/http-effect.ts +0 -222
  151. package/src/lib/identity-search-index.ts +0 -369
  152. package/src/lib/inbox.ts +0 -241
  153. package/src/lib/link-index.ts +0 -641
  154. package/src/lib/link-insights.ts +0 -860
  155. package/src/lib/link-preview-metadata.ts +0 -754
  156. package/src/lib/location.ts +0 -137
  157. package/src/lib/media-fetch.ts +0 -860
  158. package/src/lib/media-includes.ts +0 -165
  159. package/src/lib/mention-threads-live.ts +0 -917
  160. package/src/lib/mentions-export.ts +0 -147
  161. package/src/lib/mentions-live.ts +0 -1238
  162. package/src/lib/moderation-target.ts +0 -216
  163. package/src/lib/moderation-write.ts +0 -131
  164. package/src/lib/mutes-write.ts +0 -167
  165. package/src/lib/mutes.ts +0 -84
  166. package/src/lib/ndjson-stream.ts +0 -106
  167. package/src/lib/network-map.ts +0 -382
  168. package/src/lib/openai.ts +0 -117
  169. package/src/lib/period-digest.ts +0 -1552
  170. package/src/lib/present.ts +0 -118
  171. package/src/lib/profile-affiliation-hydration.ts +0 -228
  172. package/src/lib/profile-affiliations.ts +0 -262
  173. package/src/lib/profile-analysis.ts +0 -1272
  174. package/src/lib/profile-bio-entities.ts +0 -318
  175. package/src/lib/profile-history.ts +0 -164
  176. package/src/lib/profile-hydration.ts +0 -180
  177. package/src/lib/profile-replies.ts +0 -77
  178. package/src/lib/profile-resolver.ts +0 -650
  179. package/src/lib/queries.ts +0 -2381
  180. package/src/lib/research.ts +0 -652
  181. package/src/lib/search-discussion.ts +0 -1016
  182. package/src/lib/seed.ts +0 -613
  183. package/src/lib/sqlite.ts +0 -147
  184. package/src/lib/sync-cache.ts +0 -65
  185. package/src/lib/theme-transition.ts +0 -113
  186. package/src/lib/theme.tsx +0 -165
  187. package/src/lib/timeline-collections-live.ts +0 -581
  188. package/src/lib/timeline-live.ts +0 -452
  189. package/src/lib/tweet-account-edges.ts +0 -48
  190. package/src/lib/tweet-lookup.ts +0 -46
  191. package/src/lib/tweet-render.ts +0 -430
  192. package/src/lib/tweet-search-live.ts +0 -566
  193. package/src/lib/types.ts +0 -723
  194. package/src/lib/ui.ts +0 -529
  195. package/src/lib/url-expansion-store.ts +0 -110
  196. package/src/lib/url-expansion.ts +0 -343
  197. package/src/lib/url-safety.ts +0 -220
  198. package/src/lib/web-sync.ts +0 -516
  199. package/src/lib/whois.ts +0 -1079
  200. package/src/lib/x-profile.ts +0 -365
  201. package/src/lib/x-web.ts +0 -199
  202. package/src/lib/xurl-rate-limits.ts +0 -267
  203. package/src/lib/xurl.ts +0 -1677
  204. package/src/routeTree.gen.ts +0 -723
  205. package/src/router.tsx +0 -20
  206. package/src/routes/__root.tsx +0 -79
  207. package/src/routes/api/action.tsx +0 -147
  208. package/src/routes/api/avatar.tsx +0 -50
  209. package/src/routes/api/blocks.tsx +0 -35
  210. package/src/routes/api/conversation.tsx +0 -45
  211. package/src/routes/api/data-sources.tsx +0 -24
  212. package/src/routes/api/inbox.tsx +0 -43
  213. package/src/routes/api/link-insights.tsx +0 -85
  214. package/src/routes/api/link-preview.tsx +0 -47
  215. package/src/routes/api/network-map.tsx +0 -55
  216. package/src/routes/api/period-digest.tsx +0 -101
  217. package/src/routes/api/profile-analysis.tsx +0 -97
  218. package/src/routes/api/profile-hydrate.tsx +0 -54
  219. package/src/routes/api/query.tsx +0 -115
  220. package/src/routes/api/search-discussion.tsx +0 -113
  221. package/src/routes/api/status.tsx +0 -28
  222. package/src/routes/api/sync.tsx +0 -105
  223. package/src/routes/api/xurl-rate-limits.tsx +0 -24
  224. package/src/routes/blocks.tsx +0 -377
  225. package/src/routes/bookmarks.tsx +0 -18
  226. package/src/routes/data-sources.tsx +0 -257
  227. package/src/routes/discuss.tsx +0 -419
  228. package/src/routes/dms.tsx +0 -432
  229. package/src/routes/inbox.tsx +0 -226
  230. package/src/routes/index.tsx +0 -32
  231. package/src/routes/likes.tsx +0 -18
  232. package/src/routes/links.tsx +0 -994
  233. package/src/routes/mentions.tsx +0 -32
  234. package/src/routes/network-map.tsx +0 -1035
  235. package/src/routes/profile-analyze.tsx +0 -112
  236. package/src/routes/profiles.$handle.tsx +0 -228
  237. package/src/routes/rate-limits.tsx +0 -309
  238. package/src/routes/today.tsx +0 -511
  239. package/src/styles.css +0 -257
  240. package/tsconfig.json +0 -29
  241. package/vite.config.ts +0 -29
  242. /package/{public → dist/client}/birdclaw-mark.png +0 -0
  243. /package/{public → dist/client}/favicon.ico +0 -0
  244. /package/{public → dist/client}/logo192.png +0 -0
  245. /package/{public → dist/client}/logo512.png +0 -0
  246. /package/{public → dist/client}/manifest.json +0 -0
  247. /package/{public → dist/client}/robots.txt +0 -0
@@ -1,777 +0,0 @@
1
- import { Fragment, type MouseEventHandler, type ReactNode } from "react";
2
- import { formatCompactNumber } from "#/lib/present";
3
- import type { PeriodDigestContext } from "#/lib/period-digest";
4
- import type { ProfileAnalysisContext } from "#/lib/profile-analysis";
5
- import { renderTweetPlainText } from "#/lib/tweet-render";
6
- import type { ProfileRecord } from "#/lib/types";
7
- import { cx, tweetLinkClass, tweetMentionClass } from "#/lib/ui";
8
- import { safeHttpUrl } from "#/lib/url-safety";
9
- import { AvatarChip } from "./AvatarChip";
10
- import { useFloatingPreview } from "./FloatingPreview";
11
- import { ProfilePreview } from "./ProfilePreview";
12
- import { SmartTimestamp } from "./SmartTimestamp";
13
-
14
- type CitationTweet = PeriodDigestContext["tweets"][number];
15
- type CitationContext = PeriodDigestContext | ProfileAnalysisContext;
16
- type InlineLookup = {
17
- tweetsById: Map<string, CitationTweet>;
18
- profilesByHandle: Map<string, ProfileRecord>;
19
- };
20
-
21
- function normalizeTweetReference(value: string) {
22
- return value
23
- .trim()
24
- .replace(/^\(/, "")
25
- .replace(/\)$/, "")
26
- .replace(/^tweet_/, "");
27
- }
28
-
29
- function isNumericTweetReference(value: string) {
30
- return /^\d{12,25}$/.test(normalizeTweetReference(value));
31
- }
32
-
33
- function tweetReferencesFromToken(token: string) {
34
- return Array.from(token.matchAll(/\b(?:tweet_)?[A-Za-z0-9_:-]{3,}\b/g))
35
- .map((match) => match[0])
36
- .filter((value) => value.startsWith("tweet_") || /^\d{12,25}$/.test(value));
37
- }
38
-
39
- function adjacentParenthesizedTweetReferences(value: string, cursor: number) {
40
- const references: string[] = [];
41
- let nextCursor = cursor;
42
- while (nextCursor < value.length) {
43
- const match =
44
- /^\s+\((?:\s*(?:tweet_[A-Za-z0-9_:-]+|\d{12,25})\s*,?)+\)/.exec(
45
- value.slice(nextCursor),
46
- );
47
- if (!match) break;
48
- references.push(...tweetReferencesFromToken(match[0]));
49
- nextCursor += match[0].length;
50
- }
51
- return { references, cursor: nextCursor };
52
- }
53
-
54
- function trailingReadableBounds(
55
- value: string,
56
- options: { preferClause?: boolean } = {},
57
- ) {
58
- let start = 0;
59
- for (const separator of [". ", "? ", "! ", "; ", ": "]) {
60
- const index = value.lastIndexOf(separator);
61
- if (index >= 0) start = Math.max(start, index + separator.length);
62
- }
63
-
64
- let end = value.length;
65
- while (start < end && /\s/.test(value[start] ?? "")) start += 1;
66
- while (end > start && /\s/.test(value[end - 1] ?? "")) end -= 1;
67
-
68
- let clauseStart = start;
69
- for (const separator of [", with ", ", while ", ", and "]) {
70
- const index = value.lastIndexOf(separator, end);
71
- if (index >= start) {
72
- clauseStart = index + 2;
73
- break;
74
- }
75
- }
76
-
77
- if (
78
- options.preferClause !== false &&
79
- (clauseStart > start || end - start > 140)
80
- ) {
81
- while (clauseStart < end && /\s/.test(value[clauseStart] ?? "")) {
82
- clauseStart += 1;
83
- }
84
- if (end > clauseStart) start = clauseStart;
85
- }
86
-
87
- return end > start ? { start, end } : null;
88
- }
89
-
90
- function trimBullet(value: string) {
91
- return value.replace(/^[-*]\s+/, "");
92
- }
93
-
94
- function skipRedundantSourceWords(value: string, cursor: number) {
95
- const match = /^((?:\s+source\b)+)(?=\s*(?:[.,;:!?)]|$))/i.exec(
96
- value.slice(cursor),
97
- );
98
- return match ? cursor + match[0].length : cursor;
99
- }
100
-
101
- function getTweetUrl(tweet: CitationTweet) {
102
- return tweet.url || `https://x.com/${tweet.author}/status/${tweet.id}`;
103
- }
104
-
105
- function getFallbackTweetUrl(tweetId: string) {
106
- return `https://x.com/i/status/${normalizeTweetReference(tweetId)}`;
107
- }
108
-
109
- function TweetSourceLink({
110
- children,
111
- href,
112
- onClick,
113
- describedBy,
114
- }: {
115
- children: ReactNode;
116
- href: string;
117
- onClick?: MouseEventHandler<HTMLAnchorElement>;
118
- describedBy?: string;
119
- }) {
120
- return (
121
- <a
122
- aria-describedby={describedBy}
123
- className="rounded-sm px-0.5 text-[var(--accent)] hover:bg-[var(--accent-soft)] hover:no-underline"
124
- href={href}
125
- onClick={onClick}
126
- rel="noreferrer"
127
- target="_blank"
128
- >
129
- {children}
130
- </a>
131
- );
132
- }
133
-
134
- function TweetPreviewToken({
135
- tweet,
136
- children,
137
- }: {
138
- tweet: CitationTweet;
139
- children: ReactNode;
140
- }) {
141
- const preview = useFloatingPreview();
142
-
143
- const article = tweet.entities?.article;
144
- const renderedText = renderTweetPlainText(tweet.text, tweet.entities ?? {});
145
- const previewText = article
146
- ? [article.title, article.previewText]
147
- .filter(
148
- (value, index, values): value is string =>
149
- Boolean(value) && values.indexOf(value) === index,
150
- )
151
- .join("\n\n")
152
- : renderedText;
153
-
154
- return (
155
- <span
156
- ref={preview.referenceRef}
157
- className="inline align-baseline"
158
- {...preview.referenceProps}
159
- >
160
- <TweetSourceLink
161
- describedBy={preview.open ? preview.floatingId : undefined}
162
- href={getTweetUrl(tweet)}
163
- onClick={(event) => {
164
- preview.closePreview();
165
- event.currentTarget.blur();
166
- }}
167
- >
168
- {children}
169
- </TweetSourceLink>
170
- {preview.open ? (
171
- <span
172
- id={preview.floatingId}
173
- ref={preview.floatingRef}
174
- className="fixed z-40 w-[360px] overflow-y-auto rounded-2xl border border-[var(--line)] bg-[var(--bg-elevated)] p-3 text-left text-[14px] leading-[1.4] text-[var(--ink)] shadow-[0_14px_40px_var(--shadow-strong)]"
175
- role="tooltip"
176
- style={preview.floatingStyle}
177
- {...preview.floatingProps}
178
- >
179
- <span className="block" data-floating-preview-content>
180
- <span className="mb-2 flex items-center gap-2">
181
- <AvatarChip
182
- avatarUrl={tweet.authorProfile.avatarUrl}
183
- hue={tweet.authorProfile.avatarHue}
184
- name={tweet.name}
185
- profileId={tweet.authorProfile.id}
186
- size="small"
187
- />
188
- <span className="min-w-0">
189
- <span className="block truncate font-bold">{tweet.name}</span>
190
- <span className="block truncate text-[12px] text-[var(--ink-soft)]">
191
- @{tweet.author} · <SmartTimestamp value={tweet.createdAt} />
192
- </span>
193
- </span>
194
- </span>
195
- <span className="line-clamp-6 whitespace-pre-wrap [overflow-wrap:anywhere]">
196
- {previewText}
197
- </span>
198
- <span className="mt-2 flex gap-3 text-[12px] text-[var(--ink-soft)]">
199
- <span>{tweet.source}</span>
200
- {tweet.likeCount > 0 ? (
201
- <span>{formatCompactNumber(tweet.likeCount)} likes</span>
202
- ) : null}
203
- {tweet.needsReply ? <span>reply open</span> : null}
204
- </span>
205
- </span>
206
- </span>
207
- ) : null}
208
- </span>
209
- );
210
- }
211
-
212
- function additionalDirectCitationLinks(references: string[], key: string) {
213
- return references.slice(1).flatMap((reference, index) => [
214
- index === 0 ? " " : ", ",
215
- <TweetSourceLink
216
- key={`${key}-direct-source-${String(index + 2)}`}
217
- href={getFallbackTweetUrl(reference)}
218
- >
219
- {`source ${String(index + 2)}`}
220
- </TweetSourceLink>,
221
- ]);
222
- }
223
-
224
- function splitReadableForSourceLinks(value: string, count: number) {
225
- if (count < 2) return null;
226
- const separators = Array.from(
227
- value.matchAll(/,\s+and\s+|,\s+or\s+|;\s+|,\s+|\s+and\s+|\s+or\s+/g),
228
- );
229
- if (separators.length < count - 1) return null;
230
-
231
- const selected = separators.slice(-(count - 1));
232
- const parts: Array<{ text: string; separatorAfter: string }> = [];
233
- let cursor = 0;
234
- for (const separator of selected) {
235
- const index = separator.index;
236
- if (index === undefined) return null;
237
- const text = value.slice(cursor, index);
238
- if (!text.trim()) return null;
239
- parts.push({ text, separatorAfter: separator[0] });
240
- cursor = index + separator[0].length;
241
- }
242
-
243
- const lastText = value.slice(cursor);
244
- if (!lastText.trim()) return null;
245
- parts.push({ text: lastText, separatorAfter: "" });
246
- return parts.length === count ? parts : null;
247
- }
248
-
249
- function linkedCitationParts(
250
- readable: string,
251
- tweets: CitationTweet[],
252
- key: string,
253
- ) {
254
- const parts = splitReadableForSourceLinks(readable, tweets.length);
255
- if (!parts) {
256
- const tweet = tweets[0];
257
- if (!tweet) return [];
258
- return [
259
- <TweetPreviewToken key={key} tweet={tweet}>
260
- {readable}
261
- </TweetPreviewToken>,
262
- ...additionalCitationLinks(tweets, key),
263
- ];
264
- }
265
- return parts.flatMap((part, index) => {
266
- const tweet = tweets[index];
267
- if (!tweet) return [];
268
- return [
269
- <TweetPreviewToken key={`${key}-part-${String(index)}`} tweet={tweet}>
270
- {part.text}
271
- </TweetPreviewToken>,
272
- part.separatorAfter,
273
- ];
274
- });
275
- }
276
-
277
- function linkedDirectCitationParts(
278
- readable: string,
279
- references: string[],
280
- key: string,
281
- ) {
282
- const parts = splitReadableForSourceLinks(readable, references.length);
283
- if (!parts) {
284
- const reference = references[0];
285
- if (!reference) return [];
286
- return [
287
- <TweetSourceLink key={key} href={getFallbackTweetUrl(reference)}>
288
- {readable}
289
- </TweetSourceLink>,
290
- ...additionalDirectCitationLinks(references, key),
291
- ];
292
- }
293
- return parts.flatMap((part, index) => {
294
- const reference = references[index];
295
- if (!reference) return [];
296
- return [
297
- <TweetSourceLink
298
- key={`${key}-direct-part-${String(index)}`}
299
- href={getFallbackTweetUrl(reference)}
300
- >
301
- {part.text}
302
- </TweetSourceLink>,
303
- part.separatorAfter,
304
- ];
305
- });
306
- }
307
-
308
- function additionalCitationLinks(tweets: CitationTweet[], key: string) {
309
- return tweets.slice(1).flatMap((tweet, index) => [
310
- index === 0 ? " " : ", ",
311
- <TweetPreviewToken key={`${key}-source-${String(index + 2)}`} tweet={tweet}>
312
- {`source ${String(index + 2)}`}
313
- </TweetPreviewToken>,
314
- ]);
315
- }
316
-
317
- function fallbackCitationLinks(tweets: CitationTweet[], key: string) {
318
- return tweets.flatMap((tweet, index) => [
319
- index === 0 ? "" : ", ",
320
- <TweetPreviewToken
321
- key={`${key}-fallback-${String(index + 1)}`}
322
- tweet={tweet}
323
- >
324
- {tweets.length === 1 ? "source" : `source ${String(index + 1)}`}
325
- </TweetPreviewToken>,
326
- ]);
327
- }
328
-
329
- function linkTrailingCitationText(
330
- nodes: ReactNode[],
331
- tweets: CitationTweet[],
332
- key: string,
333
- ) {
334
- const tweet = tweets[0];
335
- if (!tweet) return false;
336
- const last = nodes.at(-1);
337
- if (typeof last !== "string") return false;
338
-
339
- const match = /(["“][^"”]+["”])(\s*)$/.exec(last);
340
- if (match) {
341
- const quoted = match[1];
342
- const trailing = match[2] ?? "";
343
- const before = last.slice(0, match.index);
344
- nodes[nodes.length - 1] = before;
345
- nodes.push(
346
- <TweetPreviewToken key={key} tweet={tweet}>
347
- {quoted}
348
- </TweetPreviewToken>,
349
- ...additionalCitationLinks(tweets, key),
350
- /^\s*$/.test(trailing) ? "" : trailing,
351
- );
352
- return true;
353
- }
354
-
355
- const bounds = trailingReadableBounds(last, {
356
- preferClause: tweets.length === 1,
357
- });
358
- if (!bounds) return false;
359
-
360
- const before = last.slice(0, bounds.start);
361
- const readable = last.slice(bounds.start, bounds.end);
362
- const trailing = last.slice(bounds.end);
363
- nodes[nodes.length - 1] = before;
364
- nodes.push(
365
- ...linkedCitationParts(readable, tweets, key),
366
- /^\s*$/.test(trailing) ? "" : trailing,
367
- );
368
- return true;
369
- }
370
-
371
- function linkTrailingDirectCitationText(
372
- nodes: ReactNode[],
373
- references: string[],
374
- key: string,
375
- ) {
376
- const reference = references[0];
377
- if (!reference) return false;
378
- const last = nodes.at(-1);
379
- if (typeof last !== "string") return false;
380
- const bounds = trailingReadableBounds(last, {
381
- preferClause: references.length === 1,
382
- });
383
- if (!bounds) return false;
384
-
385
- const before = last.slice(0, bounds.start);
386
- const readable = last.slice(bounds.start, bounds.end);
387
- const trailing = last.slice(bounds.end);
388
- nodes[nodes.length - 1] = before;
389
- nodes.push(
390
- ...linkedDirectCitationParts(readable, references, key),
391
- /^\s*$/.test(trailing) ? "" : trailing,
392
- );
393
- return true;
394
- }
395
-
396
- function renderInline(text: string, lookup: InlineLookup) {
397
- const pattern =
398
- /(\[[^\]\n]+\]\s*\(https?:\/\/[^\s)]+\)|\*\*[^*]+\*\*|@[A-Za-z0-9_]{1,20}|\((?:\s*(?:tweet_[A-Za-z0-9_:-]+|\d{12,25})\s*,?)+\)|\btweet_[A-Za-z0-9_:-]+\b|\b\d{12,25}\b)/g;
399
- const nodes: ReactNode[] = [];
400
- let cursor = 0;
401
- let match: RegExpExecArray | null;
402
-
403
- while ((match = pattern.exec(text))) {
404
- const token = match[0];
405
- const tokenKey = `${token}-${String(match.index)}`;
406
- if (match.index > cursor) {
407
- nodes.push(text.slice(cursor, match.index));
408
- }
409
- cursor = match.index + token.length;
410
-
411
- if (token.startsWith("**") && token.endsWith("**")) {
412
- nodes.push(<strong key={tokenKey}>{token.slice(2, -2)}</strong>);
413
- continue;
414
- }
415
-
416
- const markdownLink = /^\[([^\]\n]+)\]\s*\((https?:\/\/[^\s)]+)\)$/.exec(
417
- token,
418
- );
419
- if (markdownLink) {
420
- const href = safeHttpUrl(markdownLink[2]);
421
- nodes.push(
422
- href ? (
423
- <a
424
- key={tokenKey}
425
- className={tweetLinkClass}
426
- href={href}
427
- rel="noreferrer"
428
- target="_blank"
429
- >
430
- {markdownLink[1]}
431
- </a>
432
- ) : (
433
- markdownLink[1]
434
- ),
435
- );
436
- continue;
437
- }
438
-
439
- if (token.startsWith("@")) {
440
- const profile = lookup.profilesByHandle.get(token.slice(1).toLowerCase());
441
- nodes.push(
442
- profile ? (
443
- <ProfilePreview key={tokenKey} profile={profile}>
444
- <span className={tweetMentionClass}>{token}</span>
445
- </ProfilePreview>
446
- ) : (
447
- <a
448
- key={tokenKey}
449
- className={tweetMentionClass}
450
- href={`/profiles/${encodeURIComponent(token.slice(1))}`}
451
- >
452
- {token}
453
- </a>
454
- ),
455
- );
456
- continue;
457
- }
458
-
459
- const isParenthesizedTweetRef =
460
- token.startsWith("(") && token.endsWith(")");
461
- let references = tweetReferencesFromToken(token);
462
- if (isParenthesizedTweetRef) {
463
- const adjacent = adjacentParenthesizedTweetReferences(text, cursor);
464
- const groupedReferences = [...references, ...adjacent.references];
465
- if (
466
- adjacent.references.length > 0 &&
467
- groupedReferences.every(isNumericTweetReference)
468
- ) {
469
- references = groupedReferences;
470
- cursor = adjacent.cursor;
471
- pattern.lastIndex = cursor;
472
- }
473
- }
474
- const resolvedTweets = references.map((reference) =>
475
- lookup.tweetsById.get(normalizeTweetReference(reference)),
476
- );
477
- const allReferencesResolved =
478
- references.length > 0 && resolvedTweets.every(Boolean);
479
- const tweets = resolvedTweets.filter((tweet): tweet is CitationTweet =>
480
- Boolean(tweet),
481
- );
482
- const tweet = tweets[0];
483
- if (
484
- isParenthesizedTweetRef &&
485
- references.length > 1 &&
486
- !allReferencesResolved
487
- ) {
488
- if (references.every(isNumericTweetReference)) {
489
- const cursorAfterSourceWords = skipRedundantSourceWords(text, cursor);
490
- if (linkTrailingDirectCitationText(nodes, references, tokenKey)) {
491
- cursor = cursorAfterSourceWords;
492
- continue;
493
- }
494
- nodes.push(
495
- ...references.flatMap((reference, index) => [
496
- index === 0 ? "" : ", ",
497
- <TweetSourceLink
498
- key={`${tokenKey}-direct-${String(index)}`}
499
- href={getFallbackTweetUrl(reference)}
500
- >
501
- {`source ${String(index + 1)}`}
502
- </TweetSourceLink>,
503
- ]),
504
- );
505
- cursor = cursorAfterSourceWords;
506
- continue;
507
- }
508
- nodes.push(token);
509
- continue;
510
- }
511
- if (
512
- tweet &&
513
- isParenthesizedTweetRef &&
514
- allReferencesResolved &&
515
- linkTrailingCitationText(nodes, tweets, tokenKey)
516
- ) {
517
- cursor = skipRedundantSourceWords(text, cursor);
518
- continue;
519
- }
520
- if (tweet && isParenthesizedTweetRef && allReferencesResolved) {
521
- nodes.push(...fallbackCitationLinks(tweets, tokenKey));
522
- cursor = skipRedundantSourceWords(text, cursor);
523
- continue;
524
- }
525
- if (tweet) {
526
- nodes.push(
527
- <TweetPreviewToken key={tokenKey} tweet={tweet}>
528
- {isParenthesizedTweetRef ? "source" : token}
529
- </TweetPreviewToken>,
530
- );
531
- } else if (
532
- isParenthesizedTweetRef &&
533
- references.length === 1 &&
534
- isNumericTweetReference(references[0] ?? "")
535
- ) {
536
- const cursorAfterSourceWords = skipRedundantSourceWords(text, cursor);
537
- if (linkTrailingDirectCitationText(nodes, references, tokenKey)) {
538
- cursor = cursorAfterSourceWords;
539
- continue;
540
- }
541
- nodes.push(
542
- <TweetSourceLink
543
- key={`${tokenKey}-direct`}
544
- href={getFallbackTweetUrl(references[0])}
545
- >
546
- source
547
- </TweetSourceLink>,
548
- );
549
- cursor = cursorAfterSourceWords;
550
- continue;
551
- } else {
552
- nodes.push(token);
553
- }
554
- }
555
-
556
- if (cursor < text.length) {
557
- nodes.push(text.slice(cursor));
558
- }
559
-
560
- return nodes.map((node, index) => (
561
- <Fragment
562
- key={typeof node === "string" ? `${node}-${String(index)}` : index}
563
- >
564
- {node}
565
- </Fragment>
566
- ));
567
- }
568
-
569
- function isProfileAnalysisContext(
570
- context: CitationContext,
571
- ): context is ProfileAnalysisContext {
572
- return "conversations" in context && "profile" in context;
573
- }
574
-
575
- function addLookupTweet(
576
- tweetsById: Map<string, CitationTweet>,
577
- profilesByHandle: Map<string, ProfileRecord>,
578
- tweet: CitationTweet,
579
- ) {
580
- const normalized = normalizeTweetReference(tweet.id);
581
- tweetsById.set(normalized, tweet);
582
- tweetsById.set(`tweet_${normalized}`, tweet);
583
- profilesByHandle.set(tweet.author.toLowerCase(), tweet.authorProfile);
584
- }
585
-
586
- function syntheticProfileForConversationTweet(
587
- tweet: ProfileAnalysisContext["conversations"][number],
588
- ): ProfileRecord {
589
- return {
590
- id: tweet.profileId,
591
- handle: tweet.author,
592
- displayName: tweet.name || tweet.author,
593
- bio: tweet.bio,
594
- followersCount: tweet.followersCount,
595
- avatarHue: 210,
596
- avatarUrl: tweet.avatarUrl,
597
- createdAt: tweet.createdAt,
598
- };
599
- }
600
-
601
- function profileAnalysisTweetToCitation(
602
- tweet: ProfileAnalysisContext["tweets"][number],
603
- profile: ProfileRecord,
604
- ): CitationTweet {
605
- return {
606
- id: tweet.id,
607
- url: tweet.url,
608
- source: "authored",
609
- author: profile.handle,
610
- name: profile.displayName,
611
- authorProfile: profile,
612
- createdAt: tweet.createdAt,
613
- text: tweet.text,
614
- entities: tweet.entities,
615
- likeCount: tweet.likeCount,
616
- liked: false,
617
- bookmarked: false,
618
- needsReply: false,
619
- };
620
- }
621
-
622
- function conversationTweetToCitation(
623
- tweet: ProfileAnalysisContext["conversations"][number],
624
- ): CitationTweet {
625
- const authorProfile = syntheticProfileForConversationTweet(tweet);
626
- return {
627
- id: tweet.id,
628
- url: tweet.url,
629
- source: "mentions",
630
- author: tweet.author,
631
- name: tweet.name || tweet.author,
632
- authorProfile,
633
- createdAt: tweet.createdAt,
634
- text: tweet.text,
635
- entities: tweet.entities,
636
- likeCount: tweet.likeCount,
637
- liked: false,
638
- bookmarked: false,
639
- needsReply: false,
640
- };
641
- }
642
-
643
- function buildLookup(context?: CitationContext | null): InlineLookup {
644
- const tweetsById = new Map<string, CitationTweet>();
645
- const profilesByHandle = new Map<string, ProfileRecord>();
646
- if (!context) {
647
- return { tweetsById, profilesByHandle };
648
- }
649
- if (isProfileAnalysisContext(context)) {
650
- profilesByHandle.set(context.profile.handle.toLowerCase(), context.profile);
651
- for (const profile of context.profiles ?? []) {
652
- profilesByHandle.set(profile.handle.toLowerCase(), profile);
653
- }
654
- for (const tweet of context.tweets) {
655
- addLookupTweet(
656
- tweetsById,
657
- profilesByHandle,
658
- profileAnalysisTweetToCitation(tweet, context.profile),
659
- );
660
- }
661
- for (const tweet of context.conversations) {
662
- addLookupTweet(
663
- tweetsById,
664
- profilesByHandle,
665
- conversationTweetToCitation(tweet),
666
- );
667
- }
668
- return { tweetsById, profilesByHandle };
669
- }
670
- for (const tweet of context.tweets) {
671
- addLookupTweet(tweetsById, profilesByHandle, tweet);
672
- }
673
- return { tweetsById, profilesByHandle };
674
- }
675
-
676
- export function MarkdownViewer({
677
- markdown,
678
- context,
679
- className,
680
- }: {
681
- markdown: string;
682
- context?: CitationContext | null;
683
- className?: string;
684
- }) {
685
- const lookup = buildLookup(context);
686
- const normalizedMarkdown = markdown.replace(
687
- /\]\s*\r?\n\s*\((https?:\/\/[^\s)]+)\)/g,
688
- "]($1)",
689
- );
690
- const lines = normalizedMarkdown.split(/\r?\n/);
691
- const nodes: ReactNode[] = [];
692
- let listItems: ReactNode[][] = [];
693
-
694
- const flushList = () => {
695
- if (listItems.length === 0) return;
696
- nodes.push(
697
- <ul
698
- className="my-2.5 flex list-disc flex-col gap-1.5 pl-5 first:mt-0 marker:text-[var(--ink-soft)]"
699
- key={`list-${String(nodes.length)}`}
700
- >
701
- {listItems.map((item, index) => (
702
- <li key={String(index)}>{item}</li>
703
- ))}
704
- </ul>,
705
- );
706
- listItems = [];
707
- };
708
-
709
- for (const line of lines) {
710
- const trimmed = line.trim();
711
- if (!trimmed) {
712
- flushList();
713
- continue;
714
- }
715
- if (trimmed.startsWith("### ")) {
716
- flushList();
717
- nodes.push(
718
- <h3
719
- className="mt-5 mb-1.5 text-[14px] font-bold uppercase tracking-wide text-[var(--ink-soft)] first:mt-0"
720
- key={`h3-${String(nodes.length)}`}
721
- >
722
- {renderInline(trimmed.slice(4), lookup)}
723
- </h3>,
724
- );
725
- continue;
726
- }
727
- if (trimmed.startsWith("## ")) {
728
- flushList();
729
- nodes.push(
730
- <h2
731
- className="mt-6 mb-2 text-[18px] font-bold text-[var(--ink)] first:mt-0"
732
- key={`h2-${String(nodes.length)}`}
733
- >
734
- {renderInline(trimmed.slice(3), lookup)}
735
- </h2>,
736
- );
737
- continue;
738
- }
739
- if (trimmed.startsWith("# ")) {
740
- flushList();
741
- nodes.push(
742
- <h1
743
- className="mt-0 mb-2.5 text-[20px] font-bold text-[var(--ink)]"
744
- key={`h1-${String(nodes.length)}`}
745
- >
746
- {renderInline(trimmed.slice(2), lookup)}
747
- </h1>,
748
- );
749
- continue;
750
- }
751
- if (/^[-*]\s+/.test(trimmed)) {
752
- listItems.push(renderInline(trimBullet(trimmed), lookup));
753
- continue;
754
- }
755
- flushList();
756
- nodes.push(
757
- <p
758
- className="my-2.5 whitespace-pre-wrap first:mt-0 [overflow-wrap:anywhere]"
759
- key={`p-${String(nodes.length)}`}
760
- >
761
- {renderInline(trimmed, lookup)}
762
- </p>,
763
- );
764
- }
765
- flushList();
766
-
767
- return (
768
- <article
769
- className={cx(
770
- "max-w-none px-4 py-3 text-[15px] leading-[1.55] text-[var(--ink)]",
771
- className,
772
- )}
773
- >
774
- {nodes}
775
- </article>
776
- );
777
- }