@bettercms-ai/richtext 0.14.0 → 0.16.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.
package/src/ingest.ts CHANGED
@@ -20,7 +20,9 @@ import { EMBED_TITLE_MAX, isEmbedAspect, nearestEmbedAspect, resolveEmbed } from
20
20
  import { cellTypeOf, columnOf, designOf, isColumnType, isValueType, jsonLdOf, PT_TABLE_LIMITS, typedCellText } from "./table";
21
21
  import { PT_KEY_TAKEAWAYS_LIMITS } from "./key-takeaways";
22
22
  import { PT_GALLERY_LIMITS } from "./gallery";
23
- import { PT_TOC_LIMITS } from "./toc";
23
+ import { PT_TOC_LIMITS, isHeadingAnchor, outlineOf } from "./toc";
24
+ import { PT_RELATED_LIMITS } from "./references";
25
+ import { readImageFrame } from "./image-layout";
24
26
  import type { PtSchema } from "./schema";
25
27
 
26
28
  /**
@@ -63,6 +65,14 @@ export interface IngestOptions {
63
65
  * the allowlist grows from.
64
66
  */
65
67
  onReceipt?: (line: string) => void;
68
+ /**
69
+ * RT9: keep a top-level h2/h3's own `id` as the block's `anchor` (only when it differs from the
70
+ * id its words make). OFF by default, because a value stored before anchors existed was read
71
+ * without them: a comparison of such a value with its page must read the page the same way, or
72
+ * an unedited page reads as changed (R15). An import turns it on; a comparison turns it on only
73
+ * when the stored value has an anchor (`hasHeadingAnchors`).
74
+ */
75
+ headingAnchors?: boolean;
66
76
  }
67
77
 
68
78
  /**
@@ -608,6 +618,58 @@ function checklistItemRule(schema: PtSchema | undefined): DeserializerRule {
608
618
  };
609
619
  }
610
620
 
621
+ /**
622
+ * RT8: `<aside data-rt="related">` → `relatedPosts` and `<aside data-rt="author">` → `authorBio`, the
623
+ * inverses of their renderers and of V2's markup. Only what is stored is read: the title (≤ 40),
624
+ * the mode (V2's "auto" is "topic"; else "newest" or "manual"), the count (V2 clamps it to 1–6,
625
+ * default 3) and the picks (`data-posts`); the author's id (`data-author`). The listed posts and
626
+ * the author's name, avatar and bio are resolved at render and ignored.
627
+ */
628
+ function referenceRule(key: KeyGenerator, schema: PtSchema | undefined): DeserializerRule {
629
+ const related = allowsBlockObject(schema, PT_TYPES.relatedPosts);
630
+ const author = allowsBlockObject(schema, PT_TYPES.authorBio);
631
+ return {
632
+ deserialize(el: Node, _next, createBlock) {
633
+ if (!isElement(el) || el.nodeName.toLowerCase() !== "aside") return undefined;
634
+ const rt = el.getAttribute("data-rt");
635
+ if (rt === "related" && related) {
636
+ // Our placeholder (no `resolved`) carries the title as an attribute; a filled aside, and V2's, as `[data-title]`.
637
+ const title = (el.getAttribute("data-title") ?? el.querySelector("[data-title]")?.textContent ?? "").replace(/\s+/g, " ").trim().slice(0, PT_RELATED_LIMITS.titleChars);
638
+ const m = el.getAttribute("data-mode");
639
+ const mode = m === "manual" || m === "newest" ? m : "topic";
640
+ const count = Math.min(PT_RELATED_LIMITS.maxCount, Math.max(PT_RELATED_LIMITS.minCount, Math.trunc(Number(el.getAttribute("data-count"))) || 3));
641
+ const posts = (el.getAttribute("data-posts") ?? "").split(",").map((id) => id.trim()).filter(Boolean).map((_ref) => ({ _key: key(), _ref }));
642
+ return createBlock({ _type: PT_TYPES.relatedPosts, _key: key(), ...(title ? { title } : {}), mode, count, posts });
643
+ }
644
+ if (rt === "author" && author) {
645
+ const id = (el.getAttribute("data-author") ?? "").trim();
646
+ return createBlock({ _type: PT_TYPES.authorBio, _key: key(), ...(id ? { author: { _ref: id } } : {}) });
647
+ }
648
+ return undefined;
649
+ },
650
+ };
651
+ }
652
+
653
+ /**
654
+ * RT7: `<div data-rt="columns" data-gap>` → the `columns` block, the inverse of `columnsHtml` and
655
+ * of V2's own markup: one column per `[data-col]` child, its html read with the same options as
656
+ * the body (the field's schema too), and the gap (s, m, l; else m). The count is derived. Nothing
657
+ * is dropped here: a column holding more than text, or a count outside 2 to 4, is the write check's
658
+ * to refuse by name.
659
+ */
660
+ function columnsRule(key: KeyGenerator, inner: (html: string) => PortableText): DeserializerRule {
661
+ return {
662
+ deserialize(el: Node, _next, createBlock) {
663
+ if (!isElement(el) || el.nodeName.toLowerCase() !== "div" || el.getAttribute("data-rt") !== "columns") return undefined;
664
+ const g = el.getAttribute("data-gap");
665
+ const columns = Array.from(el.children)
666
+ .filter((c) => c.hasAttribute("data-col"))
667
+ .map((c) => ({ _key: key(), content: inner(c.innerHTML) }));
668
+ return createBlock({ _type: PT_TYPES.columns, _key: key(), gap: g === "s" || g === "l" ? g : "m", columns });
669
+ },
670
+ };
671
+ }
672
+
611
673
  /**
612
674
  * RT5: `<nav data-rt="toc" data-depth>` → the `tableOfContents` block, the inverse of `tocHtml`
613
675
  * and of V2's own markup. Only what is stored is read: the title (`[data-title]`, ≤ 40, none when
@@ -637,9 +699,17 @@ function galleryFigureRule(key: KeyGenerator): DeserializerRule {
637
699
  const images = Array.from(el.querySelectorAll("li")).map((li) => {
638
700
  const img = li.querySelector("img");
639
701
  const caption = (li.querySelector("figcaption")?.textContent ?? "").trim().slice(0, PT_GALLERY_LIMITS.captionChars);
702
+ const frame = img ? readImageFrame(img) : null;
640
703
  return {
641
704
  _key: key(),
642
- image: img ? { src: (img.getAttribute("src") ?? "").trim(), alt: img.getAttribute("alt") ?? "" } : null,
705
+ image: img
706
+ ? {
707
+ src: frame?.src ?? (img.getAttribute("src") ?? "").trim(),
708
+ alt: img.getAttribute("alt") ?? "",
709
+ ...(frame?.crop ? { crop: frame.crop } : {}),
710
+ ...(frame?.focal ? { focal: frame.focal } : {}),
711
+ }
712
+ : null,
643
713
  ...(caption ? { caption } : {}),
644
714
  };
645
715
  });
@@ -852,14 +922,18 @@ function mediaRule(key: KeyGenerator, schema: PtSchema | undefined, onReceipt?:
852
922
  fields: { url: src, ...(title ? { title } : {}) },
853
923
  });
854
924
  }
925
+ // RT9: an image we drew with a crop or focal point carries what was stored back.
926
+ const frame = readImageFrame(el);
855
927
  return createBlock({
856
928
  _type: PT_TYPES.block,
857
929
  _key: key(),
858
930
  schemaKey: BUILTIN_IMAGE_KEY,
859
931
  fields: {
860
- src,
932
+ src: frame.src ?? src,
861
933
  alt: (el.getAttribute("alt") ?? "").trim(),
862
934
  ...(caption ? { caption } : {}),
935
+ ...(frame.crop ? { crop: frame.crop } : {}),
936
+ ...(frame.focal ? { focal: frame.focal } : {}),
863
937
  },
864
938
  });
865
939
  },
@@ -1001,6 +1075,104 @@ const allowsStyled = (schema: PtSchema | undefined): boolean =>
1001
1075
  const allowsBlockObject = (schema: PtSchema | undefined, name: string): boolean =>
1002
1076
  !schema || schema.blockObjects.some((o) => o.name === name);
1003
1077
 
1078
+ const words = (s: string): string => s.replace(/\u200b/g, "").replace(/\s+/g, " ").trim();
1079
+
1080
+ /**
1081
+ * RT9: each top-level h2/h3 of the page being read, in order, with its own `id` ("" when none or
1082
+ * not a plain id). The words exclude a footnote marker, as the stored heading's do.
1083
+ */
1084
+ const withHeadingIds =
1085
+ (parse: (html: string) => Document, into: { text: string; id: string }[]) =>
1086
+ (html: string): Document => {
1087
+ const doc = parse(html);
1088
+ for (const h of Array.from(doc.body.querySelectorAll(":scope > h2, :scope > h3"))) {
1089
+ const copy = h.cloneNode(true) as Element;
1090
+ copy.querySelectorAll("sup").forEach((s) => s.remove());
1091
+ const id = h.getAttribute("id") ?? "";
1092
+ into.push({ text: words(copy.textContent ?? ""), id: isHeadingAnchor(id) ? id : "" });
1093
+ }
1094
+ return doc;
1095
+ };
1096
+
1097
+ /**
1098
+ * RT9: an imported heading keeps its page's id as `anchor`, so the site's own links to it keep
1099
+ * working, but only when it differs from the id its words make: our own markup reads back with
1100
+ * no anchor at all, and a heading's id follows its words unless the page said otherwise.
1101
+ */
1102
+ function keepHeadingIds(value: PortableText, ids: { text: string; id: string }[]): PortableText {
1103
+ if (!ids.some((h) => h.id)) return value;
1104
+ const heads = value.filter(
1105
+ (n): n is PortableTextBlock => n._type === "block" && !(n as PortableTextBlock).listItem && ((n as PortableTextBlock).style === "h2" || (n as PortableTextBlock).style === "h3"),
1106
+ );
1107
+ let at = 0;
1108
+ const marked: PortableTextBlock[] = [];
1109
+ for (const head of heads) {
1110
+ const text = words(head.children.map((c) => (typeof (c as { text?: unknown }).text === "string" ? (c as { text: string }).text : "")).join(""));
1111
+ while (at < ids.length && ids[at].text !== text) at++;
1112
+ if (at >= ids.length) break;
1113
+ if (ids[at].id) {
1114
+ head.anchor = ids[at].id;
1115
+ marked.push(head);
1116
+ }
1117
+ at++;
1118
+ }
1119
+ // The ids the words alone would make: an anchor equal to its heading's is not kept.
1120
+ const set = new Set<unknown>(marked);
1121
+ const plain = value.map((n) => (set.has(n) ? { ...n, anchor: undefined } : n));
1122
+ const auto = outlineOf(plain).nodes;
1123
+ value.forEach((n, i) => {
1124
+ if (set.has(n) && auto.get(plain[i]) === (n as PortableTextBlock).anchor) delete (n as PortableTextBlock).anchor;
1125
+ });
1126
+ return value;
1127
+ }
1128
+
1129
+ const allowsInlineObject = (schema: PtSchema | undefined, name: string): boolean =>
1130
+ !schema || schema.inlineObjects.some((o) => o.name === name);
1131
+
1132
+ /**
1133
+ * RT6: V2's end list (`<ol data-rt="footnotes">`, or a legacy `ol.cs-footnotes`) is derived, so it
1134
+ * is dropped before the deserialiser sees it (V2 `hydrate`). A legacy marker that kept its note only
1135
+ * in the list (`<sup id="fnref-N">`, no `data-note`) is lent that note first.
1136
+ */
1137
+ const withFootnoteNotes =
1138
+ (parse: (html: string) => Document) =>
1139
+ (html: string): Document => {
1140
+ const doc = parse(html);
1141
+ const lists = Array.from(doc.body.querySelectorAll(":scope > ol[data-rt=footnotes], :scope > ol.cs-footnotes"));
1142
+ if (lists.length === 0) return doc;
1143
+ const legacy: Record<number, string> = {};
1144
+ for (const ol of lists) {
1145
+ for (const li of Array.from(ol.querySelectorAll(":scope > li[id^='fn-']"))) {
1146
+ const n = Number(li.id.slice(3));
1147
+ const copy = li.cloneNode(true) as Element;
1148
+ copy.querySelectorAll("a[href^='#fnref-']").forEach((a) => a.remove());
1149
+ if (n) legacy[n] = (copy.textContent ?? "").replace(/\u200b/g, "").trim();
1150
+ }
1151
+ ol.remove();
1152
+ }
1153
+ for (const sup of Array.from(doc.body.querySelectorAll("sup[data-rt=fn], sup[id^='fnref-']"))) {
1154
+ if (sup.hasAttribute("data-note")) continue;
1155
+ const n = Number(sup.id.replace("fnref-", "")) || Number(sup.textContent) || 0;
1156
+ sup.setAttribute("data-note", legacy[n] ?? "");
1157
+ }
1158
+ return doc;
1159
+ };
1160
+
1161
+ /**
1162
+ * RT6: a footnote marker, ours or V2's `<sup data-rt="fn" data-note>` (or a legacy `<sup
1163
+ * id="fnref-N">`) → the `footnote` inline object. Only the note is read: the number, the ids and
1164
+ * the link inside are derived.
1165
+ */
1166
+ function footnoteRule(key: KeyGenerator): DeserializerRule {
1167
+ return {
1168
+ deserialize(el: Node) {
1169
+ if (!isElement(el) || el.nodeName.toLowerCase() !== "sup") return undefined;
1170
+ if (el.getAttribute("data-rt") !== "fn" && !(el.getAttribute("id") ?? "").startsWith("fnref-")) return undefined;
1171
+ return { _type: PT_TYPES.footnote, _key: key(), note: el.getAttribute("data-note") ?? "" } as never;
1172
+ },
1173
+ };
1174
+ }
1175
+
1004
1176
  const isElement = (n: unknown): n is Element =>
1005
1177
  !!n && typeof n === "object" && (n as Node).nodeType === 1;
1006
1178
 
@@ -1032,11 +1204,18 @@ const withoutBlockWhitespace =
1032
1204
  export function htmlToPortableText(html: string, opts: IngestOptions = {}): PortableText {
1033
1205
  if (typeof html !== "string" || html.trim() === "") return [];
1034
1206
  const key = opts.keyGenerator ?? randomKey;
1207
+ const headingIds: { text: string; id: string }[] = [];
1035
1208
  const parse =
1036
1209
  opts.parseHtml ?? (typeof DOMParser !== "undefined" ? (markup: string) => new DOMParser().parseFromString(markup, "text/html") : undefined);
1037
1210
  const converted = ptHtmlToPortableText(html, {
1038
1211
  // The browser's own parser when none is passed (Node must pass one), so both drop the same whitespace.
1039
- ...(parse ? { parseHtml: withoutBlockWhitespace(parse) } : {}),
1212
+ ...(parse
1213
+ ? {
1214
+ parseHtml: ((p) => (opts.headingAnchors ? withHeadingIds(p, headingIds) : p))(
1215
+ allowsInlineObject(opts.schema, PT_TYPES.footnote) ? withFootnoteNotes(withoutBlockWhitespace(parse)) : withoutBlockWhitespace(parse),
1216
+ ),
1217
+ }
1218
+ : {}),
1040
1219
  // `ptSchemaFor` returns a schema DEFINITION — the same object `EditorProvider` takes, so
1041
1220
  // the editor and this filter are literally configured from one value. The deserialiser
1042
1221
  // wants the COMPILED form, so the compile happens here rather than at every call site.
@@ -1059,12 +1238,15 @@ export function htmlToPortableText(html: string, opts: IngestOptions = {}): Port
1059
1238
  ...(allowsBlockObject(opts.schema, PT_TYPES.table)
1060
1239
  ? [tableFigureRule(key, (cell) => htmlToPortableText(cell, opts)), tableRule(key, (cell) => htmlToPortableText(cell, opts))]
1061
1240
  : []),
1241
+ ...(allowsInlineObject(opts.schema, PT_TYPES.footnote) ? [footnoteRule(key)] : []),
1062
1242
  ...(allowsBlockObject(opts.schema, PT_TYPES.code) ? [codeRule(key)] : []),
1063
1243
  ...(allowsBlockObject(opts.schema, PT_TYPES.pullQuote) ? [pullQuoteRule(key)] : []),
1064
1244
  ...(allowsBlockObject(opts.schema, PT_TYPES.keyTakeaways) ? [keyTakeawaysRule(key)] : []),
1065
1245
  // Before the media rule, which would take the iframe out of our own embed figure (and an image out of a gallery).
1066
1246
  ...(allowsBlockObject(opts.schema, PT_TYPES.gallery) ? [galleryFigureRule(key)] : []),
1067
1247
  ...(allowsBlockObject(opts.schema, PT_TYPES.tableOfContents) ? [tocRule(key)] : []),
1248
+ referenceRule(key, opts.schema),
1249
+ ...(allowsBlockObject(opts.schema, PT_TYPES.columns) ? [columnsRule(key, (inner) => htmlToPortableText(inner, opts))] : []),
1068
1250
  embedFigureRule(key, opts.schema),
1069
1251
  mediaRule(key, opts.schema, opts.onReceipt),
1070
1252
  checklistItemRule(opts.schema),
@@ -1075,7 +1257,8 @@ export function htmlToPortableText(html: string, opts: IngestOptions = {}): Port
1075
1257
  ],
1076
1258
  keyGenerator: key,
1077
1259
  });
1078
- return normalise(converted, key);
1260
+ const value = normalise(converted, key);
1261
+ return opts.headingAnchors ? keepHeadingIds(value, headingIds) : value;
1079
1262
  }
1080
1263
 
1081
1264
  /**
package/src/plain.ts CHANGED
@@ -2,6 +2,7 @@ import { isPortableTextBlock, isPortableTextSpan, PT_TYPES, type PortableText, t
2
2
  import { migrateTable, tableGroupLayout } from "./table";
3
3
  import { keyTakeawaysView } from "./key-takeaways";
4
4
  import { galleryView } from "./gallery";
5
+ import { columnsView } from "./columns";
5
6
  import { componentCopyLines, detachedComponentCopy } from "./component-copy";
6
7
 
7
8
  /**
@@ -55,6 +56,15 @@ export function portableTextToPlainText(value: PortableText | null | undefined):
55
56
  lines.push(...galleryView(node).images.map((x) => x.caption).filter(Boolean));
56
57
  continue;
57
58
  }
59
+ /** A column's text is words a reader sees, in column order (a legacy `builtin:columns` too). */
60
+ const cols = columnsView(node);
61
+ if (cols) {
62
+ for (const c of cols.columns) {
63
+ const text = portableTextToPlainText(c.content);
64
+ if (text) lines.push(text);
65
+ }
66
+ continue;
67
+ }
58
68
  const table = migrateTable(node);
59
69
  if (table) {
60
70
  lines.push(...tableLines(table));
package/src/read.ts CHANGED
@@ -3,7 +3,7 @@ import { lexicalToPortableText } from "./from-lexical";
3
3
  import { migrateTable } from "./table";
4
4
  import { detachedComponentCopy, type DetachedComponentCopy } from "./component-copy";
5
5
  import { randomKey, type KeyGenerator } from "./key";
6
- import type { EmbedCard, RichTextEmbeds } from "@bettercms-ai/types";
6
+ import type { EmbedCard, ResolvedAuthor, ResolvedPost, RichTextEmbeds, RichTextResolved } from "@bettercms-ai/types";
7
7
 
8
8
  /**
9
9
  * THE reader. One function, every stored shape, and it never throws.
@@ -298,3 +298,40 @@ export function embeddedComponentRender(
298
298
  const copy = detachedComponentCopy(value);
299
299
  return copy ? { kind: "copy", copy } : null;
300
300
  }
301
+
302
+ const strings = <K extends string>(o: Record<string, unknown>, keys: readonly K[]): Partial<Record<K, string>> =>
303
+ Object.fromEntries(keys.filter((k) => typeof o[k] === "string").map((k) => [k, o[k]])) as Partial<Record<K, string>>;
304
+
305
+ /**
306
+ * RT8 (G-RT8): the entries a DELIVERED value's author bios and related posts point at, which the
307
+ * delivery puts beside the value (`value.resolved`); undefined when there are none. Never read from
308
+ * a stored envelope: nothing stores it. Checked, not trusted: a post keeps only its string fields
309
+ * and needs a title and url, an author a name, and `related` only lists of ids.
310
+ */
311
+ export function readStructuredResolved(props: unknown): RichTextResolved | undefined {
312
+ if (!isRecord(props)) return undefined;
313
+ const candidate = props.content ?? props.value;
314
+ const raw = isRecord(candidate) ? candidate.resolved : undefined;
315
+ if (!isRecord(raw)) return undefined;
316
+ const posts: Record<string, ResolvedPost> = {};
317
+ if (isRecord(raw.posts)) {
318
+ for (const [id, p] of Object.entries(raw.posts)) {
319
+ if (isRecord(p) && typeof p.title === "string" && typeof p.url === "string") {
320
+ posts[id] = { title: p.title, url: p.url, ...strings(p, ["cover", "category"] as const) };
321
+ }
322
+ }
323
+ }
324
+ const authors: Record<string, ResolvedAuthor> = {};
325
+ if (isRecord(raw.authors)) {
326
+ for (const [id, a] of Object.entries(raw.authors)) {
327
+ if (isRecord(a) && typeof a.name === "string") authors[id] = { name: a.name, ...strings(a, ["avatar", "bio", "url"] as const) };
328
+ }
329
+ }
330
+ const related: Record<string, string[]> = {};
331
+ if (isRecord(raw.related)) {
332
+ for (const [key, ids] of Object.entries(raw.related)) {
333
+ if (Array.isArray(ids)) related[key] = ids.filter((id): id is string => typeof id === "string");
334
+ }
335
+ }
336
+ return { posts, authors, related };
337
+ }
@@ -0,0 +1,147 @@
1
+ import type { ResolvedAuthor, ResolvedPost, RichTextResolved } from "@bettercms-ai/types";
2
+ import { PT_TYPES } from "./types";
3
+ import { safeHref } from "./href";
4
+
5
+ /**
6
+ * RT8: the two blocks that point at entries. An author bio stores its author as a reference;
7
+ * related posts store a title, a mode, a count and (hand-picked) references. What a reader sees
8
+ * of the entries (names, titles, urls, covers, bios) is derived, and NEVER stored (G-RT8): the
9
+ * caller resolves it whenever a page is produced (delivery, the hosted apply, a draft preview) and
10
+ * passes `RichTextResolved` in. The renderers never fetch. Three branches, the same on both:
11
+ * - no `resolved` at all: a placeholder aside carrying only what is stored, so a stored html
12
+ * cache stays a pure function of the value and reads back exactly;
13
+ * - resolved, nothing to show (no post resolves, the author is missing): nothing;
14
+ * - resolved: V2's aside, filled.
15
+ */
16
+ export const PT_RELATED_LIMITS = { titleChars: 40, minCount: 1, maxCount: 6, posts: 6 } as const;
17
+ export const RELATED_MODES = ["topic", "newest", "manual"] as const;
18
+ export type RelatedMode = (typeof RELATED_MODES)[number];
19
+
20
+ const isRecord = (v: unknown): v is Record<string, unknown> =>
21
+ !!v && typeof v === "object" && !Array.isArray(v);
22
+ const refOf = (v: unknown): string => (isRecord(v) && typeof v._ref === "string" ? v._ref : "");
23
+
24
+ /** The same widened check a rich-text image gets: http(s), or a site-relative path. */
25
+ const imageSrc = (raw: string): string | null =>
26
+ safeHref(raw) ?? (raw !== "" && !raw.startsWith("//") && !/[:\s]/.test(raw) ? raw : null);
27
+
28
+ /**
29
+ * What a reader sees of a related-posts block: its title, mode and count, the hand-picked ids
30
+ * (V2's `data-posts`), and the posts to list, in order: the block's own `posts` when manual,
31
+ * else the ids the server chose (`resolved.related[_key]`), at most `count`. An id the server
32
+ * could not resolve (deleted, unpublished) is skipped, and so is a post whose url is refused.
33
+ */
34
+ export function relatedPostsView(value: unknown, resolved: RichTextResolved = {}) {
35
+ const v = isRecord(value) ? value : {};
36
+ const title = typeof v.title === "string" ? v.title.trim() : "";
37
+ const mode: RelatedMode = v.mode === "manual" || v.mode === "newest" ? v.mode : "topic";
38
+ const n = typeof v.count === "number" && Number.isInteger(v.count) ? v.count : 3;
39
+ const count = Math.min(PT_RELATED_LIMITS.maxCount, Math.max(PT_RELATED_LIMITS.minCount, n));
40
+ const picked = (Array.isArray(v.posts) ? v.posts : []).map(refOf).filter(Boolean);
41
+ const key = typeof v._key === "string" ? v._key : "";
42
+ const ids = mode === "manual" ? picked : (resolved.related?.[key] ?? []).slice(0, count);
43
+ const items: (ResolvedPost & { id: string })[] = [];
44
+ for (const id of ids) {
45
+ const post = resolved.posts?.[id];
46
+ const url = post && typeof post.url === "string" ? safeHref(post.url) : null;
47
+ if (post && typeof post.title === "string" && url) items.push({ ...post, id, url });
48
+ }
49
+ return { key, title, mode, count, picked, items };
50
+ }
51
+
52
+ /** What a reader sees of an author bio: the referenced id, and the author when the server resolved one. */
53
+ export function authorBioView(value: unknown, resolved: RichTextResolved = {}): { key: string; id: string; author: (ResolvedAuthor & { avatar?: string }) | null } {
54
+ const key = isRecord(value) && typeof value._key === "string" ? value._key : "";
55
+ const id = refOf(isRecord(value) ? value.author : undefined);
56
+ const a = id ? resolved.authors?.[id] : undefined;
57
+ if (!a || typeof a.name !== "string") return { key, id, author: null };
58
+ const avatar = typeof a.avatar === "string" ? imageSrc(a.avatar.trim()) : null;
59
+ return {
60
+ key,
61
+ id,
62
+ author: {
63
+ name: a.name,
64
+ ...(avatar ? { avatar } : {}),
65
+ ...(typeof a.bio === "string" && a.bio.trim() ? { bio: a.bio.trim() } : {}),
66
+ },
67
+ };
68
+ }
69
+
70
+ /** One reason a stored author bio or related-posts block is refused, with its block key. */
71
+ export type PortableTextReferenceProblem = { block: string; path: Array<string | number>; code: string; message: string };
72
+
73
+ /**
74
+ * Every reason a value's author bios and related posts cannot be stored, by block and path:
75
+ * `author_bio_invalid`, `related_posts_invalid` (a shape problem, a duplicated pick) or
76
+ * `related_posts_limit_exceeded` (V2's 40-character title, 1 to 6, at most 6 picks). As with key
77
+ * takeaways, V2's required author and its "at least one pick" are the editor's, not the write's:
78
+ * an author still choosing must be able to autosave. Derived keys are refused by R8, by name.
79
+ */
80
+ export function portableTextReferenceProblems(value: unknown): PortableTextReferenceProblem[] {
81
+ const out: PortableTextReferenceProblem[] = [];
82
+ for (const node of Array.isArray(value) ? value : []) {
83
+ if (!isRecord(node)) continue;
84
+ const block = typeof node._key === "string" ? node._key : "";
85
+ const bad = (path: Array<string | number>, code: string, message: string) => out.push({ block, path, code, message: `${code}: ${message}` });
86
+ if (node._type === PT_TYPES.authorBio) {
87
+ // name, avatar and bio are derived: R8 refuses them by name.
88
+ for (const k of Object.keys(node)) if (!["_type", "_key", "author", "name", "avatar", "bio", "resolved"].includes(k)) bad([k], "author_bio_invalid", `unknown key "${k}" (author)`);
89
+ if (node.author !== undefined && (!isRecord(node.author) || typeof node.author._ref !== "string" || !node.author._ref)) {
90
+ bad(["author"], "author_bio_invalid", "author must be a reference { _ref }");
91
+ }
92
+ }
93
+ if (node._type === PT_TYPES.relatedPosts) {
94
+ const keys = ["_type", "_key", "title", "mode", "count", "posts", "resolved"];
95
+ for (const k of Object.keys(node)) if (!keys.includes(k)) bad([k], "related_posts_invalid", `unknown key "${k}" (title, mode, count, posts)`);
96
+ if (node.title !== undefined) {
97
+ if (typeof node.title !== "string") bad(["title"], "related_posts_invalid", "title must be a string");
98
+ else if (node.title.length > PT_RELATED_LIMITS.titleChars) {
99
+ bad(["title"], "related_posts_limit_exceeded", `title is ${node.title.length} characters; at most ${PT_RELATED_LIMITS.titleChars}`);
100
+ }
101
+ }
102
+ if (!(RELATED_MODES as readonly unknown[]).includes(node.mode)) bad(["mode"], "related_posts_invalid", 'mode must be "topic", "newest" or "manual"');
103
+ if (typeof node.count !== "number" || !Number.isInteger(node.count) || node.count < PT_RELATED_LIMITS.minCount || node.count > PT_RELATED_LIMITS.maxCount) {
104
+ bad(["count"], "related_posts_limit_exceeded", `count must be a whole number from ${PT_RELATED_LIMITS.minCount} to ${PT_RELATED_LIMITS.maxCount}`);
105
+ }
106
+ // Absent is no picks (an automatic block needs none).
107
+ if (node.posts === undefined) continue;
108
+ if (!Array.isArray(node.posts)) {
109
+ bad(["posts"], "related_posts_invalid", "posts must be an array of references");
110
+ continue;
111
+ }
112
+ const seen = new Set<string>();
113
+ node.posts.forEach((p, i) => {
114
+ const ref = refOf(p);
115
+ if (!ref || !isRecord(p) || typeof p._key !== "string") return bad(["posts", i], "related_posts_invalid", "a post is { _key, _ref }");
116
+ // A post's title, slug, url, cover and category are derived: R8 refuses them by name.
117
+ for (const k of Object.keys(p)) if (!["_key", "_ref", "title", "slug", "url", "cover", "category"].includes(k)) bad(["posts", i, k], "related_posts_invalid", `unknown key "${k}" (_ref)`);
118
+ if (seen.has(ref)) bad(["posts", i], "related_posts_invalid", `"${ref}" is picked twice`);
119
+ seen.add(ref);
120
+ });
121
+ if (node.posts.length > PT_RELATED_LIMITS.posts) {
122
+ bad(["posts"], "related_posts_limit_exceeded", `${node.posts.length} posts; at most ${PT_RELATED_LIMITS.posts}`);
123
+ }
124
+ }
125
+ }
126
+ return out;
127
+ }
128
+
129
+ /**
130
+ * `relatedPosts.no_topic_field`: a `topic` block in a collection with no topic field has nothing to
131
+ * rank by, so it is refused by name rather than quietly listing nothing. The caller knows the
132
+ * collection's config and says whether it has one.
133
+ */
134
+ export function relatedPostsTopicProblems(value: unknown, opts: { topicField: boolean }): PortableTextReferenceProblem[] {
135
+ if (opts.topicField) return [];
136
+ const out: PortableTextReferenceProblem[] = [];
137
+ for (const node of Array.isArray(value) ? value : []) {
138
+ if (!isRecord(node) || node._type !== PT_TYPES.relatedPosts || node.mode !== "topic") continue;
139
+ out.push({
140
+ block: typeof node._key === "string" ? node._key : "",
141
+ path: ["mode"],
142
+ code: "relatedPosts.no_topic_field",
143
+ message: "relatedPosts.no_topic_field: the collection has no topic field to rank related posts by; choose newest or hand-picked",
144
+ });
145
+ }
146
+ return out;
147
+ }
package/src/schema.ts CHANGED
@@ -248,6 +248,33 @@ export function ptSchemaFor(accept: readonly AcceptElement[]): PtSchema {
248
248
  ],
249
249
  });
250
250
  }
251
+ // RT7: columns of text; the generic block above still carries a legacy `builtin:columns`.
252
+ if (has("columns")) {
253
+ blockObjects.push({
254
+ name: PT_TYPES.columns,
255
+ title: "Columns",
256
+ fields: [
257
+ { name: "gap", type: "string" },
258
+ { name: "columns", type: "object" },
259
+ ],
260
+ });
261
+ }
262
+ // RT8: the two reference blocks; what they point at is resolved at render (references.ts).
263
+ if (has("authorBio")) {
264
+ blockObjects.push({ name: PT_TYPES.authorBio, title: "Author bio", fields: [{ name: "author", type: "object" }] });
265
+ }
266
+ if (has("relatedPosts")) {
267
+ blockObjects.push({
268
+ name: PT_TYPES.relatedPosts,
269
+ title: "Related posts",
270
+ fields: [
271
+ { name: "title", type: "string" },
272
+ { name: "mode", type: "string" },
273
+ { name: "count", type: "number" },
274
+ { name: "posts", type: "object" },
275
+ ],
276
+ });
277
+ }
251
278
  // RT5: the table of contents; its outline is derived from the headings (toc.ts).
252
279
  if (has("tableOfContents")) {
253
280
  blockObjects.push({
@@ -284,6 +311,8 @@ export function ptSchemaFor(accept: readonly AcceptElement[]): PtSchema {
284
311
  // content element, and a stored mention must survive in a field that never offered one.
285
312
  inlineObjects: [
286
313
  { name: PT_TYPES.mention, title: "Mention", fields: [{ name: "label", type: "string" }] },
314
+ // RT6: a footnote stores only its note; its number and the end list are derived (footnote.ts).
315
+ ...(has("footnotes") ? [{ name: PT_TYPES.footnote, title: "Footnote", fields: [{ name: "note", type: "string" as const }] }] : []),
287
316
  ],
288
317
  };
289
318
  }
package/src/toc.ts CHANGED
@@ -27,6 +27,13 @@ export interface OutlineItem {
27
27
  const isRecord = (v: unknown): v is Record<string, unknown> =>
28
28
  !!v && typeof v === "object" && !Array.isArray(v);
29
29
 
30
+ /** A heading id kept from an imported page: a plain HTML id a link can name (`#…`) without escaping. */
31
+ export const isHeadingAnchor = (v: unknown): v is string => typeof v === "string" && /^[A-Za-z0-9][A-Za-z0-9_:.-]{0,127}$/.test(v);
32
+
33
+ /** Does this value hold a heading anchor? A comparison reads the page with anchors only when it does (ingest `headingAnchors`). */
34
+ export const hasHeadingAnchors = (value: unknown): boolean =>
35
+ Array.isArray(value) && value.some((n) => isRecord(n) && n._type === "block" && isHeadingAnchor(n.anchor));
36
+
30
37
  const headingText = (node: Record<string, unknown>): string =>
31
38
  (Array.isArray(node.children) ? node.children : [])
32
39
  .map((c) => (isRecord(c) && typeof c.text === "string" ? c.text : ""))
@@ -36,21 +43,31 @@ const headingText = (node: Record<string, unknown>): string =>
36
43
 
37
44
  /**
38
45
  * The value's top-level h2 and h3, in order, each with a de-duplicated id (V2 `outlineOf`): a
39
- * heading with no words is left out. `nodes` maps each heading node to its id, which is how a
40
- * renderer finds the id of the heading it is drawing.
46
+ * heading with no words is left out. A heading's own `anchor` (an imported page's id, RT9) is its
47
+ * id, the first time it appears; every other heading's slug steps around the anchors. `nodes` maps
48
+ * each heading node to its id, which is how a renderer finds the id of the heading it is drawing.
41
49
  */
42
50
  export function outlineOf(value: unknown): { items: OutlineItem[]; nodes: WeakMap<object, string> } {
43
- const items: OutlineItem[] = [];
44
- const nodes = new WeakMap<object, string>();
51
+ const heads: { node: Record<string, unknown>; text: string; anchor: string | null }[] = [];
45
52
  const used = new Set<string>();
46
53
  for (const node of Array.isArray(value) ? value : []) {
47
54
  if (!isRecord(node) || node._type !== "block" || node.listItem || (node.style !== "h2" && node.style !== "h3")) continue;
48
55
  const text = headingText(node);
49
56
  if (!text) continue;
50
- const base = slugify(text);
51
- let id = base;
52
- for (let n = 2; used.has(id); n++) id = `${base}-${n}`;
53
- used.add(id);
57
+ const anchor = isHeadingAnchor(node.anchor) && !used.has(node.anchor) ? node.anchor : null;
58
+ if (anchor) used.add(anchor);
59
+ heads.push({ node, text, anchor });
60
+ }
61
+ const items: OutlineItem[] = [];
62
+ const nodes = new WeakMap<object, string>();
63
+ for (const { node, text, anchor } of heads) {
64
+ let id = anchor;
65
+ if (!id) {
66
+ const base = slugify(text);
67
+ id = base;
68
+ for (let n = 2; used.has(id); n++) id = `${base}-${n}`;
69
+ used.add(id);
70
+ }
54
71
  nodes.set(node, id);
55
72
  items.push({ id, text, depth: node.style === "h2" ? 2 : 3 });
56
73
  }