@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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andrew Lisowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # @standard-reader/renderer-core
2
+
3
+ The **framework-agnostic core** behind the Standard Reader renderers. It parses a
4
+ Standard Site document — Leaflet, pckt, Offprint, and every third-party block
5
+ format Standard Reader understands — and normalizes it into a single render tree
6
+ that any UI framework can walk.
7
+
8
+ You usually don't depend on this directly: pick a framework renderer instead
9
+ (see the [renderers overview](../README.md)). Reach for the core when you want to
10
+ build a renderer for another framework, or to inspect/transform a document
11
+ without rendering it.
12
+
13
+ ## The render tree
14
+
15
+ Fetch a document with the typed Standard Reader API client
16
+ ([`@standard-reader/lexicons`](https://www.npmjs.com/package/@standard-reader/lexicons) and [`@atproto/lex-client`](https://www.npmjs.com/package/@atproto/lex-client)) — a
17
+ single `getDocument` call returns the card metadata **and** the renderable body,
18
+ whose fields map straight onto a `StandardSiteDocument`:
19
+
20
+ ```ts
21
+ import { Client } from "@atproto/lex-client";
22
+ import {
23
+ standardReader,
24
+ STANDARD_READER_SERVICE,
25
+ } from "@standard-reader/lexicons";
26
+ import { buildRenderTree, segmentInline } from "@standard-reader/renderer-core";
27
+
28
+ const client = new Client(STANDARD_READER_SERVICE);
29
+ const doc = await client.call(standardReader.getDocument, {
30
+ document: "at://did:plc:…/site.standard.document/…",
31
+ });
32
+
33
+ const tree = buildRenderTree(
34
+ {
35
+ content: doc.content,
36
+ contentFormat: doc.contentFormat,
37
+ authorDid: doc.did,
38
+ description: doc.description,
39
+ }, // a StandardSiteDocument
40
+ { dropCap: true, skipLeadingImage: false },
41
+ );
42
+ // tree: { format, children: BlockNode[], footnotes, footnoteNumbers } | null
43
+ ```
44
+
45
+ `buildRenderTree` does all the format-specific work once:
46
+
47
+ - detects the content format from `content.$type` (or `contentFormat`),
48
+ - parses it to that format's blocks,
49
+ - maps every block onto the shared [`BlockNode`](./src/nodes.ts) vocabulary
50
+ (`paragraph`, `heading`, `image`, `code`, `bulletList`, `table`, `callout`,
51
+ the platform blocks `leaflet.poll` / `pckt.gallery` / `offprint.component`, …),
52
+ - resolves blob images to URLs (override via `options.resolveImageUrl`),
53
+ - trims a leading hero image / duplicate heading and marks the drop-cap
54
+ paragraph,
55
+ - collects and numbers Leaflet footnotes.
56
+
57
+ Inline rich text is carried on nodes as `RichText` (`plaintext` + byte-indexed
58
+ facets). Turn it into an `InlineNode` tree of marks/links/mentions/footnote
59
+ references with `segmentInline(richText, footnoteNumbers)`.
60
+
61
+ ## Writing a framework renderer
62
+
63
+ A renderer is a walk over the tree that maps each `BlockNode` / `InlineNode`
64
+ type to a component or template in your framework:
65
+
66
+ ```ts
67
+ import { buildRenderTree, segmentInline } from "@standard-reader/renderer-core";
68
+
69
+ function render(doc) {
70
+ const tree = buildRenderTree(doc);
71
+ if (!tree) return null;
72
+ return tree.children.map(renderBlock);
73
+ }
74
+
75
+ function renderBlock(node) {
76
+ switch (node.type) {
77
+ case "paragraph":
78
+ return myParagraph(renderInline(node.text));
79
+ case "heading":
80
+ return myHeading(node.level, renderInline(node.text));
81
+ case "image":
82
+ return myImage(node.src, node.alt);
83
+ // …one case per BlockNode type
84
+ }
85
+ }
86
+
87
+ function renderInline(text) {
88
+ return segmentInline(text).map((n) => {
89
+ switch (n.type) {
90
+ case "text":
91
+ return n.value;
92
+ case "mark":
93
+ return myMark(n.mark, renderInline(n.children));
94
+ case "link":
95
+ return myLink(n.href, n.children);
96
+ case "mention":
97
+ return myMention(n, n.children);
98
+ case "footnoteRef":
99
+ return myFootnoteRef(n);
100
+ }
101
+ });
102
+ }
103
+ ```
104
+
105
+ Every framework renderer is exactly this walk with a per-framework
106
+ component/template registry layered on top; the [renderers
107
+ overview](../README.md) links the reference implementations.
108
+
109
+ ## Also exported
110
+
111
+ The raw per-format parsers and vocabulary types are available too:
112
+ `leafletBlocks`, `pcktBlocks`, `offprintBlocks`, `structuredFormatBlocks`,
113
+ `collectLeafletFootnotes`, `segmentFacetedText`, `defaultImageUrlResolver`,
114
+ `blobCid` / `cdnImageUrl`, and the `LeafletRenderableBlock` /
115
+ `StructuredRenderableBlock` / `PcktRenderableBlock` types.
116
+
117
+ ## License
118
+
119
+ MIT
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Vendored, dependency-free copy of the app's blob helpers. Standard Reader
3
+ * images are AT-Proto blobs referenced by CID; the Bluesky CDN serves any PDS
4
+ * blob by `(did, cid)`, so these two helpers are all the renderer needs to turn
5
+ * a blob ref into an `<img src>`.
6
+ */
7
+ /** A blob ref as it appears in a decoded record. */
8
+ export interface BlobRef {
9
+ ref?: string | {
10
+ $link?: string;
11
+ ["/"]?: string;
12
+ toString?: () => string;
13
+ } | null;
14
+ mimeType?: string;
15
+ size?: number;
16
+ }
17
+ /**
18
+ * Pull the CID string out of a blob ref. Handles the shapes a `ref` can take
19
+ * depending on how the record was decoded: a bare CID string, a `{$link: cid}`
20
+ * object (plain dag-json), a `{"/": cid}` object (dag-json link form), or a
21
+ * multiformats `CID` instance (stringified).
22
+ */
23
+ export declare function blobCid(blob: BlobRef | undefined | null): string | null;
24
+ /**
25
+ * Build a Bluesky CDN image URL for a PDS blob. The CDN transcodes to the
26
+ * requested format (`@jpeg` for photos, `@png` when alpha must be preserved)
27
+ * and serves it with long cache headers + inline disposition — suitable for a
28
+ * browser `<img src>`.
29
+ */
30
+ export declare function cdnImageUrl(did: string, cid: string, format?: "jpeg" | "png"): string;
31
+ //# sourceMappingURL=blob.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blob.d.ts","sourceRoot":"","sources":["../../src/atproto/blob.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,oDAAoD;AACpD,MAAM,WAAW,OAAO;IACtB,GAAG,CAAC,EACA,MAAM,GACN;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,MAAM,CAAA;KAAE,GAC3D,IAAI,CAAC;IACT,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAsBvE;AAKD;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,MAAM,GAAE,MAAM,GAAG,KAAc,GAC9B,MAAM,CAIR"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Vendored, dependency-free copy of the app's blob helpers. Standard Reader
3
+ * images are AT-Proto blobs referenced by CID; the Bluesky CDN serves any PDS
4
+ * blob by `(did, cid)`, so these two helpers are all the renderer needs to turn
5
+ * a blob ref into an `<img src>`.
6
+ */
7
+ /**
8
+ * Pull the CID string out of a blob ref. Handles the shapes a `ref` can take
9
+ * depending on how the record was decoded: a bare CID string, a `{$link: cid}`
10
+ * object (plain dag-json), a `{"/": cid}` object (dag-json link form), or a
11
+ * multiformats `CID` instance (stringified).
12
+ */
13
+ export function blobCid(blob) {
14
+ if (!blob || !blob.ref) {
15
+ return null;
16
+ }
17
+ const ref = blob.ref;
18
+ if (typeof ref === "string") {
19
+ return ref;
20
+ }
21
+ if (typeof ref.$link === "string") {
22
+ return ref.$link;
23
+ }
24
+ const slashLink = ref["/"];
25
+ if (typeof slashLink === "string") {
26
+ return slashLink;
27
+ }
28
+ if (typeof ref.toString === "function") {
29
+ const cid = ref.toString();
30
+ if (cid && cid !== "[object Object]") {
31
+ return cid;
32
+ }
33
+ }
34
+ return null;
35
+ }
36
+ /** The Bluesky CDN base. It serves *any* PDS blob by `(did, cid)`. */
37
+ const BSKY_CDN_BASE = "https://cdn.bsky.app/img";
38
+ /**
39
+ * Build a Bluesky CDN image URL for a PDS blob. The CDN transcodes to the
40
+ * requested format (`@jpeg` for photos, `@png` when alpha must be preserved)
41
+ * and serves it with long cache headers + inline disposition — suitable for a
42
+ * browser `<img src>`.
43
+ */
44
+ export function cdnImageUrl(did, cid, format = "jpeg") {
45
+ return `${BSKY_CDN_BASE}/feed_fullsize/plain/${encodeURIComponent(did)}/${encodeURIComponent(cid)}@${format}`;
46
+ }
47
+ //# sourceMappingURL=blob.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blob.js","sourceRoot":"","sources":["../../src/atproto/blob.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAYH;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,IAAgC;IACtD,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC;IACrB,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,OAAO,GAAG,CAAC;IACb,CAAC;IACD,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;QAClC,OAAO,GAAG,CAAC,KAAK,CAAC;IACnB,CAAC;IACD,MAAM,SAAS,GAAI,GAA+B,CAAC,GAAG,CAAC,CAAC;IACxD,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QAClC,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,IAAI,OAAO,GAAG,CAAC,QAAQ,KAAK,UAAU,EAAE,CAAC;QACvC,MAAM,GAAG,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAC;QAC3B,IAAI,GAAG,IAAI,GAAG,KAAK,iBAAiB,EAAE,CAAC;YACrC,OAAO,GAAG,CAAC;QACb,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,sEAAsE;AACtE,MAAM,aAAa,GAAG,0BAA0B,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACzB,GAAW,EACX,GAAW,EACX,SAAyB,MAAM;IAE/B,OAAO,GAAG,aAAa,wBAAwB,kBAAkB,CAC/D,GAAG,CACJ,IAAI,kBAAkB,CAAC,GAAG,CAAC,IAAI,MAAM,EAAE,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { DocumentTree } from "./nodes";
2
+ import type { RendererOptions, StandardSiteDocument } from "./types";
3
+ /**
4
+ * Normalize a Standard Site document into the framework-agnostic
5
+ * {@link DocumentTree}. Every supported content format is parsed and mapped
6
+ * onto the shared {@link BlockNode} vocabulary; image URLs are resolved, leading
7
+ * image/heading trimming and drop-cap selection are applied, and Leaflet
8
+ * footnotes are collected and numbered.
9
+ *
10
+ * Returns `null` when the format is unsupported or the body is empty.
11
+ */
12
+ export declare function buildRenderTree(doc: StandardSiteDocument, options?: RendererOptions): DocumentTree | null;
13
+ //# sourceMappingURL=build.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAEV,YAAY,EAKb,MAAM,SAAS,CAAC;AAiBjB,OAAO,KAAK,EAEV,eAAe,EACf,oBAAoB,EACrB,MAAM,SAAS,CAAC;AAsBjB;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,oBAAoB,EACzB,OAAO,CAAC,EAAE,eAAe,GACxB,YAAY,GAAG,IAAI,CAqDrB"}