@standard-reader/renderer-core 0.1.0

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 (119) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +119 -0
  3. package/dist/atproto/blob.d.ts +31 -0
  4. package/dist/atproto/blob.d.ts.map +1 -0
  5. package/dist/atproto/blob.js +47 -0
  6. package/dist/atproto/blob.js.map +1 -0
  7. package/dist/build.d.ts +13 -0
  8. package/dist/build.d.ts.map +1 -0
  9. package/dist/build.js +674 -0
  10. package/dist/build.js.map +1 -0
  11. package/dist/document/content-formats.d.ts +18 -0
  12. package/dist/document/content-formats.d.ts.map +1 -0
  13. package/dist/document/content-formats.js +76 -0
  14. package/dist/document/content-formats.js.map +1 -0
  15. package/dist/document/structured-content/blocknote.d.ts +11 -0
  16. package/dist/document/structured-content/blocknote.d.ts.map +1 -0
  17. package/dist/document/structured-content/blocknote.js +169 -0
  18. package/dist/document/structured-content/blocknote.js.map +1 -0
  19. package/dist/document/structured-content/fables.d.ts +5 -0
  20. package/dist/document/structured-content/fables.d.ts.map +1 -0
  21. package/dist/document/structured-content/fables.js +61 -0
  22. package/dist/document/structured-content/fables.js.map +1 -0
  23. package/dist/document/structured-content/image.d.ts +22 -0
  24. package/dist/document/structured-content/image.d.ts.map +1 -0
  25. package/dist/document/structured-content/image.js +54 -0
  26. package/dist/document/structured-content/image.js.map +1 -0
  27. package/dist/document/structured-content/item-blocks.d.ts +12 -0
  28. package/dist/document/structured-content/item-blocks.d.ts.map +1 -0
  29. package/dist/document/structured-content/item-blocks.js +138 -0
  30. package/dist/document/structured-content/item-blocks.js.map +1 -0
  31. package/dist/document/structured-content/oxa.d.ts +5 -0
  32. package/dist/document/structured-content/oxa.d.ts.map +1 -0
  33. package/dist/document/structured-content/oxa.js +72 -0
  34. package/dist/document/structured-content/oxa.js.map +1 -0
  35. package/dist/document/structured-content/prosemirror.d.ts +10 -0
  36. package/dist/document/structured-content/prosemirror.d.ts.map +1 -0
  37. package/dist/document/structured-content/prosemirror.js +165 -0
  38. package/dist/document/structured-content/prosemirror.js.map +1 -0
  39. package/dist/document/structured-content/text-runs.d.ts +16 -0
  40. package/dist/document/structured-content/text-runs.d.ts.map +1 -0
  41. package/dist/document/structured-content/text-runs.js +64 -0
  42. package/dist/document/structured-content/text-runs.js.map +1 -0
  43. package/dist/document/structured-content/types.d.ts +131 -0
  44. package/dist/document/structured-content/types.d.ts.map +1 -0
  45. package/dist/document/structured-content/types.js +2 -0
  46. package/dist/document/structured-content/types.js.map +1 -0
  47. package/dist/facets.d.ts +16 -0
  48. package/dist/facets.d.ts.map +1 -0
  49. package/dist/facets.js +16 -0
  50. package/dist/facets.js.map +1 -0
  51. package/dist/image.d.ts +13 -0
  52. package/dist/image.d.ts.map +1 -0
  53. package/dist/image.js +32 -0
  54. package/dist/image.js.map +1 -0
  55. package/dist/index.d.ts +22 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +21 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/inline.d.ts +14 -0
  60. package/dist/inline.d.ts.map +1 -0
  61. package/dist/inline.js +78 -0
  62. package/dist/inline.js.map +1 -0
  63. package/dist/internal.d.ts +24 -0
  64. package/dist/internal.d.ts.map +1 -0
  65. package/dist/internal.js +44 -0
  66. package/dist/internal.js.map +1 -0
  67. package/dist/leaflet/blocks.d.ts +18 -0
  68. package/dist/leaflet/blocks.d.ts.map +1 -0
  69. package/dist/leaflet/blocks.js +437 -0
  70. package/dist/leaflet/blocks.js.map +1 -0
  71. package/dist/leaflet/facets.d.ts +10 -0
  72. package/dist/leaflet/facets.d.ts.map +1 -0
  73. package/dist/leaflet/facets.js +77 -0
  74. package/dist/leaflet/facets.js.map +1 -0
  75. package/dist/leaflet/footnotes.d.ts +29 -0
  76. package/dist/leaflet/footnotes.d.ts.map +1 -0
  77. package/dist/leaflet/footnotes.js +113 -0
  78. package/dist/leaflet/footnotes.js.map +1 -0
  79. package/dist/leaflet/image.d.ts +9 -0
  80. package/dist/leaflet/image.d.ts.map +1 -0
  81. package/dist/leaflet/image.js +16 -0
  82. package/dist/leaflet/image.js.map +1 -0
  83. package/dist/leaflet/types.d.ts +266 -0
  84. package/dist/leaflet/types.d.ts.map +1 -0
  85. package/dist/leaflet/types.js +46 -0
  86. package/dist/leaflet/types.js.map +1 -0
  87. package/dist/leaflet/utf8.d.ts +5 -0
  88. package/dist/leaflet/utf8.d.ts.map +1 -0
  89. package/dist/leaflet/utf8.js +12 -0
  90. package/dist/leaflet/utf8.js.map +1 -0
  91. package/dist/nodes.d.ts +200 -0
  92. package/dist/nodes.d.ts.map +1 -0
  93. package/dist/nodes.js +15 -0
  94. package/dist/nodes.js.map +1 -0
  95. package/dist/offprint/blocks.d.ts +3 -0
  96. package/dist/offprint/blocks.d.ts.map +1 -0
  97. package/dist/offprint/blocks.js +265 -0
  98. package/dist/offprint/blocks.js.map +1 -0
  99. package/dist/offprint/types.d.ts +25 -0
  100. package/dist/offprint/types.d.ts.map +1 -0
  101. package/dist/offprint/types.js +25 -0
  102. package/dist/offprint/types.js.map +1 -0
  103. package/dist/pckt/blocks.d.ts +19 -0
  104. package/dist/pckt/blocks.d.ts.map +1 -0
  105. package/dist/pckt/blocks.js +337 -0
  106. package/dist/pckt/blocks.js.map +1 -0
  107. package/dist/pckt/image.d.ts +11 -0
  108. package/dist/pckt/image.d.ts.map +1 -0
  109. package/dist/pckt/image.js +48 -0
  110. package/dist/pckt/image.js.map +1 -0
  111. package/dist/pckt/types.d.ts +220 -0
  112. package/dist/pckt/types.d.ts.map +1 -0
  113. package/dist/pckt/types.js +37 -0
  114. package/dist/pckt/types.js.map +1 -0
  115. package/dist/types.d.ts +38 -0
  116. package/dist/types.d.ts.map +1 -0
  117. package/dist/types.js +2 -0
  118. package/dist/types.js.map +1 -0
  119. package/package.json +39 -0
package/dist/build.js ADDED
@@ -0,0 +1,674 @@
1
+ import { LEAFLET_DOCUMENT_FORMAT, leafletDocumentContent, structuredFormatBlocks, } from "./document/content-formats";
2
+ import { structuredImageAspectRatio, structuredImageHasSource, } from "./document/structured-content/image";
3
+ import { defaultImageUrlResolver, resolveGridImages } from "./image";
4
+ import { externalHttpUrl, isRecord } from "./internal";
5
+ import { asTextBlock as leafletAsTextBlock, leafletBlocks, leafletWebsiteSrc, } from "./leaflet/blocks";
6
+ import { collectLeafletFootnotes } from "./leaflet/footnotes";
7
+ import { leafletImageAspectRatio } from "./leaflet/image";
8
+ import { LEAFLET_CONTENT } from "./leaflet/types";
9
+ import { offprintBlocks } from "./offprint/blocks";
10
+ import { OFFPRINT_CONTENT } from "./offprint/types";
11
+ import { asTextBlock as pcktAsTextBlock, pcktBlocks, pcktCodeLanguage, } from "./pckt/blocks";
12
+ import { pcktImageAlt, pcktImageAspectRatio } from "./pckt/image";
13
+ import { PCKT_BLOCK } from "./pckt/types";
14
+ function resolveFormat(doc) {
15
+ if (isRecord(doc.content) && typeof doc.content.$type === "string") {
16
+ return doc.content.$type;
17
+ }
18
+ return doc.contentFormat ?? null;
19
+ }
20
+ /** Parsers key off `content.$type`; stamp it on when only `contentFormat` is set. */
21
+ function ensureType(content, format) {
22
+ if (isRecord(content) && typeof content.$type !== "string") {
23
+ return { ...content, $type: format };
24
+ }
25
+ return content;
26
+ }
27
+ /**
28
+ * Normalize a Standard Site document into the framework-agnostic
29
+ * {@link DocumentTree}. Every supported content format is parsed and mapped
30
+ * onto the shared {@link BlockNode} vocabulary; image URLs are resolved, leading
31
+ * image/heading trimming and drop-cap selection are applied, and Leaflet
32
+ * footnotes are collected and numbered.
33
+ *
34
+ * Returns `null` when the format is unsupported or the body is empty.
35
+ */
36
+ export function buildRenderTree(doc, options) {
37
+ const format = resolveFormat(doc);
38
+ if (!format)
39
+ return null;
40
+ const ctx = {
41
+ authorDid: doc.authorDid,
42
+ resolveImageUrl: options?.resolveImageUrl ?? defaultImageUrlResolver,
43
+ };
44
+ const content = ensureType(doc.content, format);
45
+ let children;
46
+ let footnotes = [];
47
+ let footnoteNumbers = new Map();
48
+ if (format === LEAFLET_CONTENT || format === LEAFLET_DOCUMENT_FORMAT) {
49
+ const source = format === LEAFLET_DOCUMENT_FORMAT
50
+ ? leafletDocumentContent(content)
51
+ : content;
52
+ const blocks = leafletBlocks(source);
53
+ const collected = collectLeafletFootnotes(blocks);
54
+ footnoteNumbers = collected.numberById;
55
+ footnotes = collected.footnotes.map((fn) => ({
56
+ id: fn.id,
57
+ number: fn.number,
58
+ text: { plaintext: fn.contentPlaintext, facets: fn.contentFacets },
59
+ }));
60
+ children = blocks.flatMap((block) => {
61
+ const node = leafletToNode(block, ctx);
62
+ return node ? [node] : [];
63
+ });
64
+ }
65
+ else if (format === PCKT_CONTENT_FORMAT) {
66
+ children = pcktBlocks(content).flatMap((block) => {
67
+ const node = pcktToNode(block, ctx);
68
+ return node ? [node] : [];
69
+ });
70
+ }
71
+ else {
72
+ const structured = format === OFFPRINT_CONTENT
73
+ ? offprintBlocks(content)
74
+ : structuredFormatBlocks(content, format);
75
+ if (!structured)
76
+ return null;
77
+ children = structured.flatMap((block) => {
78
+ const node = structuredToNode(block, ctx);
79
+ return node ? [node] : [];
80
+ });
81
+ }
82
+ children = applyLeadingTrims(children, doc.description, options);
83
+ if (options?.dropCap)
84
+ markDropCap(children);
85
+ if (children.length === 0)
86
+ return null;
87
+ return { format, children, footnotes, footnoteNumbers };
88
+ }
89
+ const PCKT_CONTENT_FORMAT = "blog.pckt.content";
90
+ // ---------------------------------------------------------------------------
91
+ // Post-processing
92
+ // ---------------------------------------------------------------------------
93
+ function applyLeadingTrims(nodes, description, options) {
94
+ let result = nodes;
95
+ if (options?.skipLeadingImage && result[0]?.type === "image") {
96
+ result = result.slice(1);
97
+ }
98
+ const desc = description?.trim();
99
+ if (desc && result[0]?.type === "heading") {
100
+ const first = result[0];
101
+ if (first.text.plaintext.trim() === desc)
102
+ result = result.slice(1);
103
+ }
104
+ return result;
105
+ }
106
+ function markDropCap(nodes) {
107
+ const node = nodes.find((candidate) => candidate.type === "paragraph" &&
108
+ candidate.text.plaintext.trim().length > 0);
109
+ if (node && node.type === "paragraph")
110
+ node.dropCap = true;
111
+ }
112
+ // ---------------------------------------------------------------------------
113
+ // Leaflet
114
+ // ---------------------------------------------------------------------------
115
+ function leafletListItem(item) {
116
+ const text = leafletAsTextBlock(item.content);
117
+ const runs = text && text.plaintext.trim()
118
+ ? [{ plaintext: text.plaintext, facets: text.facets }]
119
+ : [];
120
+ const children = [];
121
+ if (item.children?.length) {
122
+ const nested = leafletListNode({ children: item.children }, "orderedList");
123
+ if (nested)
124
+ children.push(nested);
125
+ }
126
+ if (isRecord(item.unorderedListChildren) &&
127
+ Array.isArray(item.unorderedListChildren.children) &&
128
+ item.unorderedListChildren.children.length > 0) {
129
+ const nested = leafletListNode(item.unorderedListChildren, "bulletList");
130
+ if (nested)
131
+ children.push(nested);
132
+ }
133
+ if (isRecord(item.orderedListChildren) &&
134
+ Array.isArray(item.orderedListChildren.children) &&
135
+ item.orderedListChildren.children.length > 0) {
136
+ const nested = leafletListNode(item.orderedListChildren, "orderedList");
137
+ if (nested)
138
+ children.push(nested);
139
+ }
140
+ if (runs.length === 0 && children.length === 0)
141
+ return null;
142
+ return { runs, children };
143
+ }
144
+ function leafletListNode(block, kind) {
145
+ const items = (block.children ?? []).flatMap((item) => {
146
+ const parsed = leafletListItem(item);
147
+ return parsed ? [parsed] : [];
148
+ });
149
+ if (items.length === 0)
150
+ return null;
151
+ return kind === "orderedList"
152
+ ? { type: "orderedList", start: block.startIndex, items }
153
+ : { type: "bulletList", items };
154
+ }
155
+ function leafletToNode(block, ctx) {
156
+ switch (block.kind) {
157
+ case "text": {
158
+ return block.block.plaintext
159
+ ? {
160
+ type: "paragraph",
161
+ dropCap: false,
162
+ text: {
163
+ plaintext: block.block.plaintext,
164
+ facets: block.block.facets,
165
+ },
166
+ }
167
+ : null;
168
+ }
169
+ case "header": {
170
+ return block.block.plaintext
171
+ ? {
172
+ type: "heading",
173
+ level: block.block.level ?? 2,
174
+ text: {
175
+ plaintext: block.block.plaintext,
176
+ facets: block.block.facets,
177
+ },
178
+ }
179
+ : null;
180
+ }
181
+ case "blockquote": {
182
+ return {
183
+ type: "blockquote",
184
+ paragraphs: [
185
+ { plaintext: block.block.plaintext, facets: block.block.facets },
186
+ ],
187
+ };
188
+ }
189
+ case "horizontalRule": {
190
+ return { type: "horizontalRule" };
191
+ }
192
+ case "unorderedList": {
193
+ return leafletListNode(block.block, "bulletList");
194
+ }
195
+ case "orderedList": {
196
+ return leafletListNode(block.block, "orderedList");
197
+ }
198
+ case "bskyPost": {
199
+ const uri = block.block.postRef?.uri;
200
+ return uri ? { type: "blueskyEmbed", postUri: uri } : null;
201
+ }
202
+ case "image": {
203
+ const src = ctx.resolveImageUrl({
204
+ blob: block.block.image,
205
+ authorDid: ctx.authorDid,
206
+ });
207
+ if (!src)
208
+ return null;
209
+ return {
210
+ type: "image",
211
+ src,
212
+ alt: block.block.alt?.trim() || "",
213
+ aspectRatio: leafletImageAspectRatio(block.block),
214
+ fullBleed: block.block.fullBleed,
215
+ };
216
+ }
217
+ case "code": {
218
+ return {
219
+ type: "code",
220
+ code: block.block.plaintext,
221
+ language: block.block.language,
222
+ };
223
+ }
224
+ case "iframe": {
225
+ return {
226
+ type: "iframe",
227
+ url: block.block.url ?? "",
228
+ height: block.block.height,
229
+ aspectRatio: block.block.aspectRatio,
230
+ };
231
+ }
232
+ case "website": {
233
+ const src = leafletWebsiteSrc(block.block);
234
+ return src
235
+ ? {
236
+ type: "website",
237
+ src,
238
+ title: block.block.title,
239
+ description: block.block.description,
240
+ previewImage: block.block.previewImage,
241
+ }
242
+ : null;
243
+ }
244
+ case "math": {
245
+ const tex = block.block.tex?.trim();
246
+ return tex ? { type: "math", tex } : null;
247
+ }
248
+ case "button": {
249
+ const href = block.block.url?.trim();
250
+ const text = block.block.text?.trim();
251
+ return href && text ? { type: "button", href, text } : null;
252
+ }
253
+ case "poll": {
254
+ const uri = block.block.pollRef?.uri;
255
+ return uri ? { type: "leaflet.poll", pollUri: uri } : null;
256
+ }
257
+ case "separator": {
258
+ return { type: "leaflet.separator" };
259
+ }
260
+ case "standardSitePost": {
261
+ const uri = block.block.uri;
262
+ return uri ? { type: "leaflet.standardSitePost", uri } : null;
263
+ }
264
+ case "standardSitePublication": {
265
+ const uri = block.block.uri;
266
+ return uri
267
+ ? {
268
+ type: "leaflet.standardSitePublication",
269
+ uri,
270
+ cid: block.block.cid,
271
+ showPublicationTheme: block.block.showPublicationTheme,
272
+ }
273
+ : null;
274
+ }
275
+ case "imageGallery": {
276
+ return leafletGalleryNode(block.block, ctx);
277
+ }
278
+ case "signup": {
279
+ return { type: "leaflet.signup" };
280
+ }
281
+ case "pageEmbed": {
282
+ return {
283
+ type: "leaflet.pageEmbed",
284
+ pageId: block.pageId,
285
+ pageType: block.pageType,
286
+ children: block.blocks.flatMap((nested) => {
287
+ const node = leafletToNode(nested, ctx);
288
+ return node ? [node] : [];
289
+ }),
290
+ };
291
+ }
292
+ case "unknown": {
293
+ return { type: "unknown", blockType: block.blockType };
294
+ }
295
+ }
296
+ }
297
+ function leafletGalleryNode(block, ctx) {
298
+ const images = (block.images ?? []).flatMap((image) => {
299
+ const src = ctx.resolveImageUrl({
300
+ blob: image.image,
301
+ authorDid: ctx.authorDid,
302
+ });
303
+ if (!src)
304
+ return [];
305
+ return [
306
+ {
307
+ src,
308
+ alt: image.alt?.trim() || "",
309
+ aspectRatio: leafletImageAspectRatio(image),
310
+ },
311
+ ];
312
+ });
313
+ if (images.length === 0)
314
+ return null;
315
+ const layout = block.format?.trim() || "grid";
316
+ return layout === "carousel"
317
+ ? { type: "imageCarousel", images, layout }
318
+ : { type: "imageGrid", images, layout };
319
+ }
320
+ // ---------------------------------------------------------------------------
321
+ // pckt
322
+ // ---------------------------------------------------------------------------
323
+ function pcktImageSrc(block, ctx) {
324
+ const attrs = block.attrs;
325
+ if (!attrs)
326
+ return null;
327
+ const src = attrs.src;
328
+ if (externalHttpUrl(src)) {
329
+ return ctx.resolveImageUrl({ externalSrc: src, authorDid: ctx.authorDid });
330
+ }
331
+ let blob = attrs.blob;
332
+ if (blob == null && typeof src === "string" && src.startsWith("blob:")) {
333
+ const cid = src.slice("blob:".length);
334
+ if (cid)
335
+ blob = { ref: cid };
336
+ }
337
+ return ctx.resolveImageUrl({ blob, authorDid: ctx.authorDid });
338
+ }
339
+ function pcktRuns(content) {
340
+ const runs = [];
341
+ const children = [];
342
+ for (const entry of content ?? []) {
343
+ const text = pcktAsTextBlock(entry);
344
+ if (text?.plaintext.trim()) {
345
+ runs.push({ plaintext: text.plaintext, facets: text.facets });
346
+ continue;
347
+ }
348
+ if (!isRecord(entry))
349
+ continue;
350
+ if (entry.$type === PCKT_BLOCK.bulletList) {
351
+ const node = pcktListNode(entry, "bulletList");
352
+ if (node)
353
+ children.push(node);
354
+ }
355
+ else if (entry.$type === PCKT_BLOCK.orderedList) {
356
+ const node = pcktListNode(entry, "orderedList");
357
+ if (node)
358
+ children.push(node);
359
+ }
360
+ else if (entry.$type === PCKT_BLOCK.taskList) {
361
+ const node = pcktTaskListNode(entry);
362
+ if (node)
363
+ children.push(node);
364
+ }
365
+ }
366
+ return { runs, children };
367
+ }
368
+ function pcktListNode(block, kind) {
369
+ const items = (block.content ?? []).flatMap((child) => {
370
+ if (!isRecord(child))
371
+ return [];
372
+ const { runs, children } = pcktRuns(child.content);
373
+ if (runs.length === 0 && children.length === 0)
374
+ return [];
375
+ return [{ runs, children }];
376
+ });
377
+ if (items.length === 0)
378
+ return null;
379
+ return kind === "orderedList"
380
+ ? { type: "orderedList", start: block.start, items }
381
+ : { type: "bulletList", items };
382
+ }
383
+ function pcktTaskListNode(block) {
384
+ const items = (block.content ?? []).flatMap((child) => {
385
+ if (!isRecord(child))
386
+ return [];
387
+ const { runs } = pcktRuns(child.content);
388
+ if (runs.length === 0)
389
+ return [];
390
+ const checked = Boolean(isRecord(child.attrs) && child.attrs.checked === true);
391
+ return [{ checked, runs }];
392
+ });
393
+ return items.length > 0 ? { type: "taskList", items } : null;
394
+ }
395
+ function pcktTableNode(block) {
396
+ const rows = [];
397
+ for (const row of block.content ?? []) {
398
+ if (!isRecord(row))
399
+ continue;
400
+ const cells = row.content;
401
+ if (!cells?.length)
402
+ continue;
403
+ const rowCells = cells.flatMap((cell) => {
404
+ if (!isRecord(cell))
405
+ return [];
406
+ const header = cell.$type === PCKT_BLOCK.tableHeader;
407
+ const { runs } = pcktRuns(cell.content);
408
+ return [{ header, text: mergeRuns(runs) }];
409
+ });
410
+ if (rowCells.length > 0)
411
+ rows.push(rowCells);
412
+ }
413
+ return rows.length > 0 ? { type: "table", rows } : null;
414
+ }
415
+ /** Collapse multiple inline runs into one rich-text value (facets from first). */
416
+ function mergeRuns(runs) {
417
+ if (runs.length <= 1)
418
+ return runs[0] ?? { plaintext: "" };
419
+ return {
420
+ plaintext: runs.map((run) => run.plaintext).join(""),
421
+ facets: runs[0]?.facets,
422
+ };
423
+ }
424
+ function pcktToNode(block, ctx) {
425
+ switch (block.kind) {
426
+ case "text": {
427
+ return block.block.plaintext
428
+ ? {
429
+ type: "paragraph",
430
+ dropCap: false,
431
+ text: {
432
+ plaintext: block.block.plaintext,
433
+ facets: block.block.facets,
434
+ },
435
+ }
436
+ : null;
437
+ }
438
+ case "heading": {
439
+ return block.block.plaintext
440
+ ? {
441
+ type: "heading",
442
+ level: block.block.level ?? 2,
443
+ text: {
444
+ plaintext: block.block.plaintext,
445
+ facets: block.block.facets,
446
+ },
447
+ }
448
+ : null;
449
+ }
450
+ case "blockquote": {
451
+ const { runs } = pcktRuns(block.block.content);
452
+ return runs.length > 0 ? { type: "blockquote", paragraphs: runs } : null;
453
+ }
454
+ case "horizontalRule": {
455
+ return { type: "horizontalRule" };
456
+ }
457
+ case "bulletList": {
458
+ return pcktListNode(block.block, "bulletList");
459
+ }
460
+ case "orderedList": {
461
+ return pcktListNode(block.block, "orderedList");
462
+ }
463
+ case "taskList": {
464
+ return pcktTaskListNode(block.block);
465
+ }
466
+ case "blueskyEmbed": {
467
+ const uri = block.block.postRef?.uri;
468
+ return uri ? { type: "blueskyEmbed", postUri: uri } : null;
469
+ }
470
+ case "image": {
471
+ const src = pcktImageSrc(block.block, ctx);
472
+ if (!src)
473
+ return null;
474
+ return {
475
+ type: "image",
476
+ src,
477
+ alt: pcktImageAlt(block.block),
478
+ aspectRatio: pcktImageAspectRatio(block.block),
479
+ };
480
+ }
481
+ case "code": {
482
+ return {
483
+ type: "code",
484
+ code: block.block.plaintext,
485
+ language: pcktCodeLanguage(block.block),
486
+ };
487
+ }
488
+ case "iframe": {
489
+ return {
490
+ type: "iframe",
491
+ url: block.block.url ?? block.block.attrs?.url ?? "",
492
+ height: block.block.height,
493
+ };
494
+ }
495
+ case "table": {
496
+ return pcktTableNode(block.block);
497
+ }
498
+ case "website": {
499
+ return {
500
+ type: "website",
501
+ src: block.block.src ?? "",
502
+ title: block.block.title,
503
+ description: block.block.description,
504
+ previewImage: block.block.previewImage,
505
+ };
506
+ }
507
+ case "gallery": {
508
+ const ref = block.block.ref;
509
+ return ref ? { type: "pckt.gallery", ref } : null;
510
+ }
511
+ case "noteEmbed": {
512
+ return {
513
+ type: "pckt.noteEmbed",
514
+ uri: block.block.noteRef?.uri,
515
+ cid: block.block.noteRef?.cid,
516
+ };
517
+ }
518
+ case "unknown": {
519
+ return { type: "unknown", blockType: block.blockType };
520
+ }
521
+ }
522
+ }
523
+ // ---------------------------------------------------------------------------
524
+ // Structured (Offprint + third-party)
525
+ // ---------------------------------------------------------------------------
526
+ function structuredToNode(block, ctx) {
527
+ switch (block.kind) {
528
+ case "text": {
529
+ return block.text.plaintext
530
+ ? { type: "paragraph", dropCap: false, text: block.text }
531
+ : null;
532
+ }
533
+ case "heading": {
534
+ return block.text.plaintext
535
+ ? { type: "heading", level: block.level ?? 2, text: block.text }
536
+ : null;
537
+ }
538
+ case "blockquote": {
539
+ const paragraphs = block.blocks.flatMap((nested) => {
540
+ if (nested.kind === "text" || nested.kind === "heading") {
541
+ return nested.text.plaintext.trim() ? [nested.text] : [];
542
+ }
543
+ return [];
544
+ });
545
+ return paragraphs.length > 0 ? { type: "blockquote", paragraphs } : null;
546
+ }
547
+ case "callout": {
548
+ return {
549
+ type: "callout",
550
+ text: block.text,
551
+ emoji: block.emoji,
552
+ color: block.color,
553
+ };
554
+ }
555
+ case "horizontalRule": {
556
+ return { type: "horizontalRule" };
557
+ }
558
+ case "bulletList": {
559
+ return {
560
+ type: "bulletList",
561
+ items: block.items.map((text) => ({ runs: [text], children: [] })),
562
+ };
563
+ }
564
+ case "orderedList": {
565
+ return {
566
+ type: "orderedList",
567
+ start: block.start,
568
+ items: block.items.map((text) => ({ runs: [text], children: [] })),
569
+ };
570
+ }
571
+ case "taskList": {
572
+ return {
573
+ type: "taskList",
574
+ items: block.items.map((item) => ({
575
+ checked: item.checked === true,
576
+ runs: [item.text],
577
+ })),
578
+ };
579
+ }
580
+ case "blueskyEmbed": {
581
+ return { type: "blueskyEmbed", postUri: block.postUri };
582
+ }
583
+ case "image": {
584
+ if (!structuredImageHasSource(block))
585
+ return null;
586
+ const src = ctx.resolveImageUrl({
587
+ blob: block.blob,
588
+ externalSrc: block.externalSrc,
589
+ authorDid: ctx.authorDid,
590
+ });
591
+ if (!src)
592
+ return null;
593
+ return {
594
+ type: "image",
595
+ src,
596
+ alt: block.alt?.trim() || "",
597
+ aspectRatio: structuredImageAspectRatio(block),
598
+ };
599
+ }
600
+ case "code": {
601
+ return { type: "code", code: block.plaintext, language: block.language };
602
+ }
603
+ case "iframe": {
604
+ return { type: "iframe", url: block.url, height: block.height };
605
+ }
606
+ case "website": {
607
+ return {
608
+ type: "website",
609
+ src: block.src,
610
+ title: block.title,
611
+ description: block.description,
612
+ previewImage: block.previewImage,
613
+ };
614
+ }
615
+ case "table": {
616
+ return {
617
+ type: "table",
618
+ rows: block.rows.map((row) => row.map((cell) => ({
619
+ header: cell.isHeader === true,
620
+ text: cell.text,
621
+ }))),
622
+ };
623
+ }
624
+ case "gallery": {
625
+ return { type: "pckt.gallery", ref: block.ref };
626
+ }
627
+ case "button": {
628
+ return {
629
+ type: "button",
630
+ text: block.text,
631
+ href: block.href,
632
+ caption: block.caption,
633
+ alignment: block.alignment,
634
+ };
635
+ }
636
+ case "math": {
637
+ return { type: "math", tex: block.tex };
638
+ }
639
+ case "imageGrid": {
640
+ const images = resolveGridImages(block.images, ctx.resolveImageUrl, ctx.authorDid);
641
+ return images.length > 0
642
+ ? { type: "imageGrid", images, caption: block.caption }
643
+ : null;
644
+ }
645
+ case "imageCarousel": {
646
+ const images = resolveGridImages(block.images, ctx.resolveImageUrl, ctx.authorDid);
647
+ return images.length > 0
648
+ ? { type: "imageCarousel", images, caption: block.caption }
649
+ : null;
650
+ }
651
+ case "imageDiff": {
652
+ const images = resolveGridImages(block.images, ctx.resolveImageUrl, ctx.authorDid);
653
+ if (images.length !== 2)
654
+ return null;
655
+ const [before, after] = images;
656
+ if (!before || !after)
657
+ return null;
658
+ return {
659
+ type: "imageDiff",
660
+ before,
661
+ after,
662
+ caption: block.caption,
663
+ labels: block.labels,
664
+ };
665
+ }
666
+ case "offprintComponent": {
667
+ return { type: "offprint.component", componentUri: block.componentUri };
668
+ }
669
+ case "unknown": {
670
+ return { type: "unknown", blockType: block.blockType };
671
+ }
672
+ }
673
+ }
674
+ //# sourceMappingURL=build.js.map