-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathbluesky-thread.html
More file actions
1136 lines (1056 loc) · 38.5 KB
/
bluesky-thread.html
File metadata and controls
1136 lines (1056 loc) · 38.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bluesky Thread Viewer</title>
<style>
:root {
--depth-color-1: hsl(200, 70%, 50%);
--depth-color-2: hsl(30, 80%, 50%);
--depth-color-3: hsl(120, 60%, 40%);
--depth-color-4: hsl(0, 70%, 50%);
--depth-color-5: hsl(280, 60%, 50%);
--depth-color-6: hsl(20, 60%, 40%);
--depth-color-7: hsl(330, 60%, 50%);
--depth-color-8: hsl(0, 0%, 40%);
}
body {
font-family: sans-serif;
margin: 1em;
}
header {
max-width: 800px;
}
.controls {
margin-bottom: 1em;
}
form {
display: flex;
gap: 0.5em;
}
input[type="text"] {
flex: 1;
font-size: 1rem;
padding: 0.5em;
}
#urlForm button {
background-color: #007bff;
color: white;
border: none;
padding: 0.5em 1em;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
}
.copy-container {
display: none;
margin-bottom: 1em;
}
#copyBtn,
#copyJsonBtn {
background-color: #28a745;
color: white;
border: none;
padding: 0.5em 1em;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
margin-right: 0.5em;
}
.tabs {
display: none;
margin-bottom: 1em;
border-bottom: 2px solid #ddd;
}
.tab {
background: none;
border: none;
padding: 0.75em 1.5em;
font-size: 1rem;
cursor: pointer;
border-bottom: 3px solid transparent;
margin-bottom: -2px;
color: #666;
}
.tab:hover {
color: #333;
}
.tab.active {
color: #007bff;
border-bottom-color: #007bff;
font-weight: bold;
}
.reply-to {
font-size: 0.8rem;
color: #888;
margin-bottom: 0.25em;
}
.reply-to a {
color: #007bff;
text-decoration: none;
}
.reply-to a:hover {
text-decoration: underline;
}
.post.highlighted {
background-color: #fffde7;
transition: background-color 0.3s;
}
.post {
position: relative;
border: 1px solid #ccc;
padding: 0.75em 2px 0.75em 0.75em;
border-radius: 6px;
margin-top: 1em;
}
.post::before {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 4px;
background-color: var(--stripe-color, transparent);
border-top-left-radius: 6px;
border-bottom-left-radius: 6px;
}
.depth-1 { --stripe-color: var(--depth-color-1); }
/* Connect consecutive root-level posts by the thread author */
.post.depth-1.author-continuation {
margin-top: 0;
border-top: none;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
.post.depth-1.author-continuation::before {
border-top-left-radius: 0;
}
.post.depth-1:has(+ .post.depth-1.author-continuation) {
border-bottom: none;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
.post.depth-1:has(+ .post.depth-1.author-continuation)::before {
border-bottom-left-radius: 0;
}
.depth-2 { --stripe-color: var(--depth-color-2); }
.depth-3 { --stripe-color: var(--depth-color-3); }
.depth-4 { --stripe-color: var(--depth-color-4); }
.depth-5 { --stripe-color: var(--depth-color-5); }
.depth-6 { --stripe-color: var(--depth-color-6); }
.depth-7 { --stripe-color: var(--depth-color-7); }
.depth-8 { --stripe-color: var(--depth-color-8); }
.author {
font-weight: bold;
margin-bottom: 0.25em;
}
.meta {
color: #666;
font-size: 0.85rem;
margin-bottom: 0.5em;
display: flex;
gap: 0.5em;
align-items: center;
}
.meta a {
font-size: 0.85rem;
color: #007bff;
text-decoration: none;
}
.text {
white-space: pre-wrap;
font-size: 1rem;
line-height: 1.4;
}
.images {
display: flex;
flex-wrap: wrap;
gap: 0.5em;
margin-top: 0.5em;
}
.images img {
max-height: 150px;
border-radius: 8px;
cursor: pointer;
object-fit: cover;
}
.images img:hover {
opacity: 0.9;
}
#imageModal {
border: none;
background: transparent;
max-width: 100vw;
max-height: 100vh;
padding: 0;
}
#imageModal::backdrop {
background: rgba(0, 0, 0, 0.9);
}
#imageModal img {
max-width: 95vw;
max-height: 95vh;
object-fit: contain;
}
#imageModal:focus {
outline: none;
}
/* Video thumbnail styling */
.video-container {
position: relative;
display: inline-block;
margin-top: 0.5em;
cursor: pointer;
}
.video-container img {
max-height: 200px;
border-radius: 8px;
object-fit: cover;
}
.video-container:hover img {
opacity: 0.9;
}
.video-play-icon {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 60px;
height: 60px;
background: rgba(0, 0, 0, 0.7);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
pointer-events: none;
}
.video-play-icon::after {
content: '';
width: 0;
height: 0;
border-left: 20px solid white;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
margin-left: 5px;
}
/* Video modal styling */
#videoModal {
border: none;
background: transparent;
max-width: 100vw;
max-height: 100vh;
padding: 0;
}
#videoModal::backdrop {
background: rgba(0, 0, 0, 0.9);
}
#videoModal .video-wrapper {
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
#videoModal video {
max-width: 95vw;
max-height: 95vh;
object-fit: contain;
}
#videoModal:focus {
outline: none;
}
#videoModal .close-btn {
position: absolute;
top: -30px;
right: 0;
background: none;
border: none;
color: white;
font-size: 24px;
cursor: pointer;
padding: 5px 10px;
}
.quote-tweet {
border: 1px solid #d0d7de;
border-left: 3px solid #1185fe;
border-radius: 8px;
padding: 0.75em;
margin-top: 0.5em;
background: linear-gradient(to right, #f0f7ff, #f9f9f9);
position: relative;
}
.quote-tweet .qt-header {
display: flex;
align-items: center;
gap: 0.4em;
margin-bottom: 0.25em;
}
.quote-tweet .qt-icon {
width: 16px;
height: 16px;
color: #1185fe;
flex-shrink: 0;
}
.quote-tweet .qt-author {
font-weight: bold;
font-size: 0.9rem;
}
.quote-tweet .qt-meta {
font-size: 0.8rem;
margin-bottom: 0.25em;
display: flex;
gap: 0.5em;
}
.quote-tweet .qt-meta a {
color: #007bff;
text-decoration: none;
}
.quote-tweet .qt-meta a:hover {
text-decoration: underline;
}
.quote-tweet .qt-text {
font-size: 0.9rem;
white-space: pre-wrap;
line-height: 1.3;
}
.quote-tweet .images img {
max-height: 100px;
}
.external-link {
border: 1px solid #ccc;
border-radius: 8px;
margin-top: 0.5em;
overflow: hidden;
display: block;
text-decoration: none;
color: inherit;
}
.external-link:hover {
background: #f5f5f5;
}
.external-link .el-thumb {
width: 100%;
max-height: 200px;
object-fit: cover;
}
.external-link .el-content {
padding: 0.5em 0.75em;
}
.external-link .el-title {
font-weight: bold;
font-size: 0.9rem;
margin-bottom: 0.25em;
}
.external-link .el-desc {
font-size: 0.8rem;
color: #666;
}
.external-link .el-url {
font-size: 0.75rem;
color: #888;
margin-top: 0.25em;
}
/* Hide other replies toggle */
.hide-replies-toggle {
display: block;
margin-top: 0.75em;
padding: 0.4em 0.8em;
background: #f0f0f0;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 0.85rem;
color: #555;
cursor: pointer;
}
.hide-replies-toggle:hover {
background: #e5e5e5;
}
/* When hiding replies, only show depth-1 posts by the root author */
#threadContainer.hide-other-replies .post:not(.depth-1.root-author-post) {
display: none;
}
@media (max-width: 600px) {
.post { padding-left: 1em; }
}
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/hls.js@1"></script>
<dialog id="imageModal"><img src="" alt=""></dialog>
<dialog id="videoModal">
<div class="video-wrapper">
<button class="close-btn">×</button>
<video controls playsinline></video>
</div>
</dialog>
<header>
<h1>Bluesky Thread Viewer</h1>
<div class="controls">
<form id="urlForm">
<input type="text" id="postUrl" placeholder="Bluesky post URL" required />
<button type="submit">Fetch Thread</button>
</form>
</div>
<div class="copy-container">
<button id="copyBtn">Copy</button>
<button id="copyJsonBtn">Copy JSON</button>
</div>
<div class="tabs" id="viewTabs">
<button class="tab active" data-view="thread">Thread View</button>
<button class="tab" data-view="recent">Most Recent First</button>
<button class="tab" data-view="oldest">Oldest First</button>
</div>
</header>
<div id="threadContainer"></div>
<script>
(async () => {
const form = document.getElementById('urlForm');
const container = document.getElementById('threadContainer');
const copyBtn = document.getElementById('copyBtn');
const copyJsonBtn = document.getElementById('copyJsonBtn');
const copyContainer = document.querySelector('.copy-container');
const viewTabs = document.getElementById('viewTabs');
const postUrl = document.getElementById('postUrl');
let lastThread = null;
let allPosts = []; // Flat array of all posts with metadata
let currentView = 'thread';
let hideOtherReplies = false;
postUrl.addEventListener('keydown', (e) => {
if (e.key === 'Enter') { e.preventDefault(); form.requestSubmit(); }
});
// Extract post ID from URI for use as element ID
function getPostId(uri) {
return 'post-' + uri.split('/').pop();
}
// Flatten thread into array of posts with parent info
function flattenThread(item, parentUri = null, parentAuthor = null) {
const posts = [];
posts.push({
item,
parentUri,
parentAuthor,
uri: item.post.uri,
createdAt: new Date(item.post.record.createdAt)
});
if (item.replies && item.replies.length) {
const authorName = item.post.author.displayName || item.post.author.handle;
item.replies.forEach(reply => {
posts.push(...flattenThread(reply, item.post.uri, authorName));
});
}
return posts;
}
// Render text with facets (links, mentions, etc.)
function renderTextWithFacets(text, facets) {
if (!facets || !facets.length) {
return document.createTextNode(text);
}
// Convert string to bytes for accurate indexing
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const bytes = encoder.encode(text);
// Sort facets by start index
const sorted = [...facets].sort((a, b) => a.index.byteStart - b.index.byteStart);
const container = document.createDocumentFragment();
let lastEnd = 0;
for (const facet of sorted) {
const { byteStart, byteEnd } = facet.index;
// Add text before this facet
if (byteStart > lastEnd) {
const before = decoder.decode(bytes.slice(lastEnd, byteStart));
container.appendChild(document.createTextNode(before));
}
// Get the facet text
const facetText = decoder.decode(bytes.slice(byteStart, byteEnd));
// Check for link feature
const linkFeature = facet.features.find(f => f.$type === 'app.bsky.richtext.facet#link');
if (linkFeature) {
const link = document.createElement('a');
link.href = linkFeature.uri;
link.target = '_blank';
link.textContent = facetText;
container.appendChild(link);
} else {
// For other facet types (mentions, tags), just render as text for now
container.appendChild(document.createTextNode(facetText));
}
lastEnd = byteEnd;
}
// Add remaining text after last facet
if (lastEnd < bytes.length) {
const after = decoder.decode(bytes.slice(lastEnd));
container.appendChild(document.createTextNode(after));
}
return container;
}
// Image modal setup
const imageModal = document.getElementById('imageModal');
const modalImg = imageModal.querySelector('img');
imageModal.addEventListener('click', () => imageModal.close());
function openImageModal(src, alt) {
modalImg.src = src;
modalImg.alt = alt || '';
imageModal.showModal();
}
// Video modal setup
const videoModal = document.getElementById('videoModal');
const modalVideo = videoModal.querySelector('video');
const videoCloseBtn = videoModal.querySelector('.close-btn');
let hlsInstance = null;
function closeVideoModal() {
if (hlsInstance) {
hlsInstance.destroy();
hlsInstance = null;
}
modalVideo.pause();
modalVideo.src = '';
videoModal.close();
}
videoCloseBtn.addEventListener('click', closeVideoModal);
videoModal.addEventListener('click', (e) => {
// Close when clicking backdrop (outside video wrapper)
if (e.target === videoModal) {
closeVideoModal();
}
});
function openVideoModal(playlistUrl) {
if (Hls.isSupported()) {
hlsInstance = new Hls();
hlsInstance.loadSource(playlistUrl);
hlsInstance.attachMedia(modalVideo);
hlsInstance.on(Hls.Events.MANIFEST_PARSED, () => {
modalVideo.play();
});
} else if (modalVideo.canPlayType('application/vnd.apple.mpegurl')) {
// Native HLS support (Safari)
modalVideo.src = playlistUrl;
modalVideo.addEventListener('loadedmetadata', () => {
modalVideo.play();
}, { once: true });
}
videoModal.showModal();
}
// Render video thumbnail with play button
function renderVideo(videoEmbed) {
if (!videoEmbed) return null;
const container = document.createElement('div');
container.className = 'video-container';
if (videoEmbed.thumbnail) {
const thumb = document.createElement('img');
thumb.src = videoEmbed.thumbnail;
thumb.alt = videoEmbed.alt || 'Video thumbnail';
thumb.title = videoEmbed.alt || 'Click to play video';
container.appendChild(thumb);
}
// Play icon overlay
const playIcon = document.createElement('div');
playIcon.className = 'video-play-icon';
container.appendChild(playIcon);
container.addEventListener('click', () => {
openVideoModal(videoEmbed.playlist);
});
return container;
}
// Render images from an embed
function renderImages(images) {
if (!images || !images.length) return null;
const container = document.createElement('div');
container.className = 'images';
images.forEach(img => {
const imgEl = document.createElement('img');
imgEl.src = img.thumb;
imgEl.alt = img.alt || '';
imgEl.title = img.alt || '';
imgEl.addEventListener('click', () => openImageModal(img.fullsize, img.alt));
container.appendChild(imgEl);
});
return container;
}
// Render external link preview
function renderExternalLink(external) {
if (!external) return null;
const link = document.createElement('a');
link.className = 'external-link';
link.href = external.uri;
link.target = '_blank';
if (external.thumb) {
const thumb = document.createElement('img');
thumb.className = 'el-thumb';
thumb.src = external.thumb;
link.appendChild(thumb);
}
const content = document.createElement('div');
content.className = 'el-content';
if (external.title) {
const title = document.createElement('div');
title.className = 'el-title';
title.textContent = external.title;
content.appendChild(title);
}
if (external.description) {
const desc = document.createElement('div');
desc.className = 'el-desc';
desc.textContent = external.description;
content.appendChild(desc);
}
const url = document.createElement('div');
url.className = 'el-url';
url.textContent = new URL(external.uri).hostname;
content.appendChild(url);
link.appendChild(content);
return link;
}
// Render a quoted post
function renderQuoteTweet(record) {
if (!record || record.$type !== 'app.bsky.embed.record#viewRecord') return null;
const qt = document.createElement('div');
qt.className = 'quote-tweet';
// Header with quote icon and author
const header = document.createElement('div');
header.className = 'qt-header';
// Quote icon SVG
const icon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
icon.setAttribute('class', 'qt-icon');
icon.setAttribute('viewBox', '0 0 24 24');
icon.setAttribute('fill', 'currentColor');
icon.innerHTML = '<path d="M4.583 17.321C3.553 16.227 3 15 3 13.011c0-3.5 2.457-6.637 6.03-8.188l.893 1.378c-3.335 1.804-3.987 4.145-4.247 5.621.537-.278 1.24-.375 1.929-.311 1.804.167 3.226 1.648 3.226 3.489a3.5 3.5 0 01-3.5 3.5c-1.073 0-2.099-.49-2.748-1.179zm10 0C13.553 16.227 13 15 13 13.011c0-3.5 2.457-6.637 6.03-8.188l.893 1.378c-3.335 1.804-3.987 4.145-4.247 5.621.537-.278 1.24-.375 1.929-.311 1.804.167 3.226 1.648 3.226 3.489a3.5 3.5 0 01-3.5 3.5c-1.073 0-2.099-.49-2.748-1.179z"/>';
header.appendChild(icon);
const author = document.createElement('div');
author.className = 'qt-author';
author.textContent = `${record.author.displayName || record.author.handle} (@${record.author.handle})`;
header.appendChild(author);
qt.appendChild(header);
// Add meta with View links
const postId = record.uri.split('/').pop();
const bskyUrl = `https://bsky.app/profile/${record.author.handle}/post/${postId}`;
const viewerUrl = `?url=${encodeURIComponent(bskyUrl)}&view=thread`;
const meta = document.createElement('div');
meta.className = 'qt-meta';
const viewLink = document.createElement('a');
viewLink.href = viewerUrl;
viewLink.textContent = 'View';
const bskyLink = document.createElement('a');
bskyLink.href = bskyUrl;
bskyLink.target = '_blank';
bskyLink.textContent = 'View in Bluesky';
meta.appendChild(viewLink);
meta.appendChild(bskyLink);
qt.appendChild(meta);
if (record.value && record.value.text) {
const text = document.createElement('div');
text.className = 'qt-text';
text.textContent = record.value.text;
qt.appendChild(text);
}
// Handle embeds within the quoted post
if (record.embeds && record.embeds.length) {
record.embeds.forEach(embed => {
const embedEl = renderEmbed(embed);
if (embedEl) qt.appendChild(embedEl);
});
}
return qt;
}
// Render any embed type
function renderEmbed(embed) {
if (!embed) return null;
const type = embed.$type;
if (type === 'app.bsky.embed.images#view') {
return renderImages(embed.images);
}
if (type === 'app.bsky.embed.video#view') {
return renderVideo(embed);
}
if (type === 'app.bsky.embed.external#view') {
return renderExternalLink(embed.external);
}
if (type === 'app.bsky.embed.record#view') {
return renderQuoteTweet(embed.record);
}
if (type === 'app.bsky.embed.recordWithMedia#view') {
const container = document.createElement('div');
// Render the media (images or video)
if (embed.media) {
if (embed.media.$type === 'app.bsky.embed.images#view') {
const images = renderImages(embed.media.images);
if (images) container.appendChild(images);
} else if (embed.media.$type === 'app.bsky.embed.video#view') {
const video = renderVideo(embed.media);
if (video) container.appendChild(video);
}
}
// Render the quoted record
if (embed.record && embed.record.record) {
const qt = renderQuoteTweet(embed.record.record);
if (qt) container.appendChild(qt);
}
return container;
}
return null;
}
// Fixed function to generate thread text in a readable format
function generateThreadText(thread) {
const lines = [];
function processPost(item, prefix = '1') {
const author = item.post.author.displayName || item.post.author.handle;
const text = item.post.record.text.replace(/\n/g, ' ');
lines.push(`[${prefix}] ${author}: ${text}`);
if (item.replies && item.replies.length > 0) {
item.replies.forEach((reply, i) => {
processPost(reply, `${prefix}.${i+1}`);
});
}
}
processPost(thread);
return lines.join('\n\n');
}
// Display post in thread view (nested)
// rootAuthorDid: the DID of the root post author (for flattening self-replies)
// rootContainer: the top-level container element (for appending flattened self-replies)
// parentAuthorDid: the DID of the parent post's author (to detect self-replies)
// parentAtRootLevel: whether the parent post is at the root level (depth 1)
function displayPostThread(item, parent, depth = 1, rootAuthorDid = null, rootContainer = null, parentAuthorDid = null, parentAtRootLevel = true) {
const currentAuthorDid = item.post.author.did;
// Initialize rootAuthorDid and rootContainer on first call (root post)
const isRootPost = rootAuthorDid === null;
if (isRootPost) {
rootAuthorDid = currentAuthorDid;
rootContainer = parent;
}
// Flatten only when root author replies to their own root-level post
const isRootAuthorPost = currentAuthorDid === rootAuthorDid;
const isSelfReplyAtRoot = isRootAuthorPost && parentAuthorDid === rootAuthorDid && parentAtRootLevel && !isRootPost;
const shouldFlatten = isSelfReplyAtRoot;
const effectiveParent = shouldFlatten ? rootContainer : parent;
const effectiveDepth = shouldFlatten ? 1 : depth;
const thisPostAtRootLevel = effectiveDepth === 1;
const el = document.createElement('div');
const classes = [`post`, `depth-${Math.min(effectiveDepth, 8)}`];
if (shouldFlatten) classes.push('author-continuation');
if (isRootAuthorPost) classes.push('root-author-post');
el.className = classes.join(' ');
el.id = getPostId(item.post.uri);
const authorEl = document.createElement('div');
authorEl.className = 'author';
authorEl.textContent = `${item.post.author.displayName} (@${item.post.author.handle})`;
const metaEl = document.createElement('div');
metaEl.className = 'meta';
metaEl.textContent = new Date(item.post.record.createdAt).toLocaleString();
const link = document.createElement('a');
link.href = `https://bsky.app/profile/${item.post.author.handle}/post/${item.post.uri.split('/').pop()}`;
link.textContent = 'View in Bluesky';
link.target = '_blank';
metaEl.appendChild(link);
const textEl = document.createElement('div');
textEl.className = 'text';
textEl.appendChild(renderTextWithFacets(item.post.record.text, item.post.record.facets));
el.append(authorEl, metaEl, textEl);
// Render embed if present
if (item.post.embed) {
const embedEl = renderEmbed(item.post.embed);
if (embedEl) el.appendChild(embedEl);
}
effectiveParent.appendChild(el);
if (item.replies && item.replies.length) {
item.replies.forEach(reply => {
// If this post was flattened to root, replies to it should be at depth 2
const nextDepth = shouldFlatten ? 2 : depth + 1;
displayPostThread(reply, el, nextDepth, rootAuthorDid, rootContainer, currentAuthorDid, thisPostAtRootLevel);
});
}
}
// Display posts in chronological order (most recent first)
function displayPostsChronological() {
container.innerHTML = '';
const sorted = [...allPosts].sort((a, b) => b.createdAt - a.createdAt);
sorted.forEach(postData => {
const item = postData.item;
const el = document.createElement('div');
el.className = 'post depth-1';
el.id = getPostId(item.post.uri);
// Add "in reply to" if this is a reply
if (postData.parentUri) {
const replyToEl = document.createElement('div');
replyToEl.className = 'reply-to';
const replyLink = document.createElement('a');
replyLink.href = '#' + getPostId(postData.parentUri);
replyLink.textContent = `in reply to ${postData.parentAuthor}`;
replyLink.addEventListener('click', (e) => {
e.preventDefault();
const targetEl = document.getElementById(getPostId(postData.parentUri));
if (targetEl) {
targetEl.scrollIntoView({ behavior: 'smooth', block: 'center' });
targetEl.classList.add('highlighted');
setTimeout(() => targetEl.classList.remove('highlighted'), 2000);
}
});
replyToEl.appendChild(replyLink);
el.appendChild(replyToEl);
}
const authorEl = document.createElement('div');
authorEl.className = 'author';
authorEl.textContent = `${item.post.author.displayName} (@${item.post.author.handle})`;
const metaEl = document.createElement('div');
metaEl.className = 'meta';
metaEl.textContent = new Date(item.post.record.createdAt).toLocaleString();
const link = document.createElement('a');
link.href = `https://bsky.app/profile/${item.post.author.handle}/post/${item.post.uri.split('/').pop()}`;
link.textContent = 'View in Bluesky';
link.target = '_blank';
metaEl.appendChild(link);
const textEl = document.createElement('div');
textEl.className = 'text';
textEl.appendChild(renderTextWithFacets(item.post.record.text, item.post.record.facets));
el.append(authorEl, metaEl, textEl);
// Render embed if present
if (item.post.embed) {
const embedEl = renderEmbed(item.post.embed);
if (embedEl) el.appendChild(embedEl);
}
container.appendChild(el);
});
}
// Display posts in chronological order (oldest first)
function displayPostsOldestFirst() {
container.innerHTML = '';
const sorted = [...allPosts].sort((a, b) => a.createdAt - b.createdAt);
sorted.forEach(postData => {
const item = postData.item;
const el = document.createElement('div');
el.className = 'post depth-1';
el.id = getPostId(item.post.uri);
// Add "in reply to" if this is a reply
if (postData.parentUri) {
const replyToEl = document.createElement('div');
replyToEl.className = 'reply-to';
const replyLink = document.createElement('a');
replyLink.href = '#' + getPostId(postData.parentUri);
replyLink.textContent = `in reply to ${postData.parentAuthor}`;
replyLink.addEventListener('click', (e) => {
e.preventDefault();
const targetEl = document.getElementById(getPostId(postData.parentUri));
if (targetEl) {
targetEl.scrollIntoView({ behavior: 'smooth', block: 'center' });
targetEl.classList.add('highlighted');
setTimeout(() => targetEl.classList.remove('highlighted'), 2000);
}
});
replyToEl.appendChild(replyLink);
el.appendChild(replyToEl);
}
const authorEl = document.createElement('div');
authorEl.className = 'author';
authorEl.textContent = `${item.post.author.displayName} (@${item.post.author.handle})`;
const metaEl = document.createElement('div');
metaEl.className = 'meta';
metaEl.textContent = new Date(item.post.record.createdAt).toLocaleString();
const link = document.createElement('a');
link.href = `https://bsky.app/profile/${item.post.author.handle}/post/${item.post.uri.split('/').pop()}`;
link.textContent = 'View in Bluesky';
link.target = '_blank';
metaEl.appendChild(link);
const textEl = document.createElement('div');
textEl.className = 'text';
textEl.appendChild(renderTextWithFacets(item.post.record.text, item.post.record.facets));
el.append(authorEl, metaEl, textEl);
// Render embed if present
if (item.post.embed) {
const embedEl = renderEmbed(item.post.embed);
if (embedEl) el.appendChild(embedEl);
}
container.appendChild(el);
});
}
// Display thread view
function displayThreadView() {
container.innerHTML = '';
if (lastThread) {
displayPostThread(lastThread, container);
}
}
// Render current view
function renderCurrentView() {
if (currentView === 'thread') {
displayThreadView();
} else if (currentView === 'oldest') {
displayPostsOldestFirst();
} else {
displayPostsChronological();
}
setupHideRepliesToggle();
}
// Setup the "Hide other replies" toggle if conditions are met
function setupHideRepliesToggle() {
// Remove any existing toggle
const existingToggle = container.querySelector('.hide-replies-toggle');
if (existingToggle) existingToggle.remove();
// Only show in thread view
if (currentView !== 'thread') {
container.classList.remove('hide-other-replies');
return;
}
const rootAuthorPosts = container.querySelectorAll('.post.root-author-post');
const otherPosts = container.querySelectorAll('.post:not(.root-author-post)');
// Only show toggle if there are multiple root author posts AND other replies exist
if (rootAuthorPosts.length < 2 || otherPosts.length === 0) {
container.classList.remove('hide-other-replies');
return;
}
// Apply current hide state
if (hideOtherReplies) {
container.classList.add('hide-other-replies');
} else {
container.classList.remove('hide-other-replies');
}
// Create toggle button
const toggle = document.createElement('button');
toggle.className = 'hide-replies-toggle';
updateToggleText(toggle, otherPosts.length);
toggle.addEventListener('click', () => {
hideOtherReplies = !hideOtherReplies;
container.classList.toggle('hide-other-replies', hideOtherReplies);
updateToggleText(toggle, otherPosts.length);
// Update URL
const newUrl = new URL(window.location);
if (hideOtherReplies) {
newUrl.searchParams.set('hideReplies', '1');
} else {
newUrl.searchParams.delete('hideReplies');
}
history.replaceState(null, '', newUrl);
});
// Insert toggle in the first root post, before any nested replies
const firstRootPost = rootAuthorPosts[0];
const firstNestedReply = firstRootPost.querySelector('.post');
if (firstNestedReply) {
firstRootPost.insertBefore(toggle, firstNestedReply);
} else {
firstRootPost.appendChild(toggle);
}
}