@unbase-app/pages 0.4.1 → 0.5.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/README.md CHANGED
@@ -54,8 +54,9 @@ build is untouched.
54
54
  ## What a production build does with a draft
55
55
 
56
56
  - **Its page is not built.** Astro: the page file compiles to an empty page, so nothing it imports
57
- is bundled, and its HTML is removed. React Router: its `<Route />` leaves the router and its
58
- module becomes empty, so the address falls to the app's own Not Found.
57
+ is bundled, and its HTML is removed. React Router: its `<Route />`, or its route object in a
58
+ `createBrowserRouter([...])` (since 0.5.0), leaves the router and its module becomes empty, so the
59
+ address falls to the app's own Not Found.
59
60
  - **Its address leaves every page list:** sitemaps, comment-modus's `pages.json` and content map,
60
61
  and the entries the page owns in Astro's content store.
61
62
  - **The output is checked for its text.** If a sentence only the draft has is still in a served
package/astro.d.mts CHANGED
@@ -9,6 +9,13 @@ declare function unbasePages(): AstroIntegration;
9
9
  export default unbasePages;
10
10
  export { unbasePages };
11
11
 
12
+ /**
13
+ * Where a host's adapter copies the build output and serves it from, relative to the project
14
+ * (`.vercel/output/static`): the kit cleans drafts there too, and @unbase-app/astro copies comment
15
+ * mode's build files into them.
16
+ */
17
+ export declare const HOST_COPIES: readonly string[];
18
+
12
19
  /** Astro's content store without the entries the drafts own (used by the integration; exported for its tests). */
13
20
  export declare function withoutDraftEntries(
14
21
  code: string,
package/astro.mjs CHANGED
@@ -37,9 +37,10 @@ const clean = (id) => id.replace(/\?.*$/, "");
37
37
  * Where a host's adapter copies the client output and serves it from. The Vercel adapter (11, with
38
38
  * Astro 7) copies dist/client to .vercel/output/static in its own astro:build:done, which can run
39
39
  * before this integration's: a draft removed from dist/client only would still be served (found
40
- * on a copy of a real site, 10-11-2026). So the copy is cleaned and checked as well.
40
+ * on a copy of a real site, 10-11-2026). So the copy is cleaned and checked as well. The one list:
41
+ * @unbase-app/astro copies comment mode's build files into the same folders (17-11-2026).
41
42
  */
42
- const HOST_COPIES = [".vercel/output/static"];
43
+ export const HOST_COPIES = Object.freeze([".vercel/output/static"]);
43
44
  /** Astro's content store as one module: every collection's entries, also in the server's bundle. */
44
45
  const DATA_STORE = "\0astro:data-layer-content";
45
46
 
package/binding.mjs CHANGED
@@ -1331,16 +1331,22 @@ export const untag = (text) => String(text ?? "").replace(TAG, "").replace(TAG_C
1331
1331
  */
1332
1332
  export function tagItem({ collection, id, data, fields = [] }) {
1333
1333
  const esc = (k) => k.replace(/~/g, "~0").replace(/\//g, "~1");
1334
- const walk = (value, pointer, field) => {
1334
+ const walk = (value, pointer, field, picture = false) => {
1335
1335
  if (typeof value === "string") {
1336
1336
  const v = value.trim();
1337
1337
  if (!field || !TAGGED_TYPES.includes(field.type) || !v || /[\u{E0000}-\u{E007F}]/u.test(value) || /^(?:[a-z][a-z0-9+.-]*:|\/|#|www\.)/i.test(v)) return value;
1338
+ // A picture's own texts (its alt text, its focal point "20% 80%") and a value with no letter in
1339
+ // it (a number, a percentage, a coordinate) are what code reads, parses or puts in an attribute,
1340
+ // never prose: a tag would break them (a focal point parsed strictly fell back to the middle, 16-11).
1341
+ if (picture || !/\p{L}/u.test(v)) return value;
1338
1342
  const tag = tagOf({ collection, id, pointer });
1339
1343
  return tag ? value + tag : value;
1340
1344
  }
1341
1345
  if (Array.isArray(value)) return field?.type === "list" ? value.map((v, i) => walk(v, `${pointer}/${i}`, { type: "group", fields: field.fields ?? [] })) : value;
1342
1346
  if (value && typeof value === "object" && field && (field.type === "group" || field.type === "item")) {
1343
- return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, walk(v, `${pointer}/${esc(k)}`, (field.fields ?? []).find((f) => f.key === k) ?? null)]));
1347
+ // A group beside a picture is the picture's: { src, alt, positie } (as entries.ts typedText reads it).
1348
+ const pictureGroup = field.type === "group" && (field.fields ?? []).some((f) => f.type === "image");
1349
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, walk(v, `${pointer}/${esc(k)}`, (field.fields ?? []).find((f) => f.key === k) ?? null, pictureGroup)]));
1344
1350
  }
1345
1351
  return value;
1346
1352
  };
@@ -1575,21 +1581,50 @@ export function itemsIn({ html, collections, adapter, uses = {}, formats = [] })
1575
1581
  };
1576
1582
  const bind = (it, pointer, extra = {}) => ({ binding: { collection: it.collection, item: it.id, field: pointer }, key: keyOf(it, pointer), ...extra });
1577
1583
  const ownText = (node) => node.children.filter((c) => typeof c === "string").join("");
1584
+ const escKey = (k) => k.replace(/~/g, "~0").replace(/\//g, "~1");
1585
+ /**
1586
+ * An item's fields of one type with their values, by pointer: a top-level field, or one in a group
1587
+ * at the top (a picture kept as `{ src, alt, positie }`: its address at `/image/src`). Only fields the
1588
+ * code reads (by their top-level key). A picture's value may also be an object with its `src`.
1589
+ */
1590
+ const slots = (it, type) => {
1591
+ const out = [];
1592
+ for (const f of it.fields) {
1593
+ if (!reads(it.collection, f.key)) continue;
1594
+ if (f.type === type) out.push({ pointer: `/${escKey(f.key)}`, value: it.data?.[f.key] });
1595
+ else if (f.type === "group")
1596
+ for (const g of f.fields ?? []) if (g.type === type) out.push({ pointer: `/${escKey(f.key)}/${escKey(g.key)}`, value: it.data?.[f.key]?.[g.key] });
1597
+ }
1598
+ return out.map((s) => ({ ...s, value: s.value && typeof s.value === "object" && typeof s.value.src === "string" ? s.value.src : s.value }));
1599
+ };
1600
+ /** The item an element with no tag of its own belongs to: one item's tag in its attributes, else its card's. */
1601
+ const itemOfNode = (node) => {
1602
+ const own = new Set(Object.values(node.attrs).flatMap((v) => tagsIn(v).map((r) => `${r.collection}/${r.id}`)));
1603
+ return own.size === 1 ? [...own][0] : own.size ? null : cardItem(node.parent);
1604
+ };
1578
1605
 
1579
1606
  for (const e of elementsOf(root)) {
1580
1607
  const node = e.node;
1608
+ // A link's address from a link field (docs/plan-live-preview.md LP2): marked on the link itself,
1609
+ // apart from the text in it, which binds as any text does.
1610
+ if (node.tag === "a" && typeof node.attrs.href === "string" && node.attrs.href.trim()) {
1611
+ const href = untag(node.attrs.href).trim();
1612
+ const itemKey = itemOfNode(node);
1613
+ const it = itemKey ? items.get(itemKey) : null;
1614
+ const fit = it ? slots(it, "link").filter((s) => typeof s.value === "string" && s.value.trim() === href) : [];
1615
+ if (it && fit.length === 1 && it.file) stamp(node, { "data-unbase-cms": `${it.collection}/${it.id}`, "data-unbase-cms-href": keyOf(it, fit[0].pointer) });
1616
+ }
1581
1617
  if (node.tag === "img") {
1582
1618
  const src = node.attrs.src ?? "";
1583
1619
  if (!src) continue;
1584
- const own = new Set(Object.values(node.attrs).flatMap((v) => tagsIn(v).map((r) => `${r.collection}/${r.id}`)));
1585
- const itemKey = own.size === 1 ? [...own][0] : own.size ? null : cardItem(node.parent);
1586
- const showing = (it) => it.fields.filter((f) => f.type === "image" && reads(it.collection, f.key) && imageShows(src, it.data?.[f.key], adapter)).map((f) => f.key);
1620
+ const itemKey = itemOfNode(node);
1621
+ const showing = (it) => slots(it, "image").filter((s) => imageShows(src, s.value, adapter)).map((s) => s.pointer);
1587
1622
  const it = itemKey ? items.get(itemKey) : null;
1588
1623
  if (it) {
1589
1624
  const fit = showing(it);
1590
- if (fit.length === 1) list(e, { text: "", ...bind(it, `/${fit[0]}`, { image: true }) }, it);
1625
+ if (fit.length === 1) list(e, { text: "", ...bind(it, fit[0], { image: true }) }, it);
1591
1626
  else if (fit.length > 1) list(e, { text: "", reason: "ambiguous", candidates: fit.length }, it);
1592
- else if (it.fields.some((f) => f.type === "image" && reads(it.collection, f.key))) list(e, { text: "", reason: "unproven" }, it);
1627
+ else if (slots(it, "image").length) list(e, { text: "", reason: "unproven" }, it);
1593
1628
  } else {
1594
1629
  const anywhere = [...items.values()].flatMap((x) => showing(x));
1595
1630
  if (anywhere.length) list(e, { text: "", reason: anywhere.length > 1 ? "ambiguous" : "unproven", candidates: anywhere.length }, null);
@@ -1685,3 +1720,58 @@ export function itemsIn({ html, collections, adapter, uses = {}, formats = [] })
1685
1720
  const bound = elements.filter((e) => e.binding).length;
1686
1721
  return { html: untag(out), elements, bound, unbound: elements.length - bound, cards };
1687
1722
  }
1723
+
1724
+ // ── A field a change adds: does it show, and bind? (docs/plan-live-preview.md LP4) ──
1725
+
1726
+ /** The kind of a field as the check below proves it: free text by its token, a picture by its file. */
1727
+ const addedKind = (f) =>
1728
+ TAGGED_TYPES.includes(f.type) ? "text" : f.type === "image" || (f.type === "group" && (f.fields ?? []).some((g) => g.type === "image")) ? "image" : null;
1729
+
1730
+ /**
1731
+ * Whether the fields a change adds to a collection show on the pages its components are on, and
1732
+ * bind there to their field (Build's gate, LP4; ADR-0030 points 4 and 6). The pages are rendered with
1733
+ * the gate's test item laid over (`renders`: `{ path, html }` with the item, its texts tagged, never
1734
+ * committed). A text field shows when its test value is on a page, a picture field when the test
1735
+ * picture is; either binds when `itemsIn` gives that element the field. `live`: a text bound to its
1736
+ * field exactly, so what is typed in the CMS shows on the page at once. Other types are not proven
1737
+ * here (`shows: null`, `reason: "unchecked"`); a page that did not render proves nothing.
1738
+ * @param {{ collection: string, added: FieldInput[], draft: { id: string, file?: string, data: any }, renders: { path: string, html?: string | null }[], collections: (CollectionInput & { items?: Item[] })[], adapter: BindingAdapter, uses?: Record<string, string[]>, formats?: ReturnType<typeof formatsIn> }} input
1739
+ * @returns {{ key: string, type: string, shows: boolean | null, bound: boolean, live: boolean, pages: string[], reason?: string }[]}
1740
+ */
1741
+ export function newFieldShows({ collection, added, draft, renders, collections, adapter, uses = {}, formats = [] }) {
1742
+ const withItem = collections.map((c) => (c.name === collection ? { ...c, items: [...(c.items ?? []).filter((i) => i.id !== draft.id), { id: draft.id, file: draft.file, data: draft.data }] } : c));
1743
+ const pages = renders.filter((r) => typeof r.html === "string" && r.html.length > 0);
1744
+ const bound = new Map(pages.map((p) => [p.path, itemsIn({ html: p.html, collections: withItem, adapter, uses, formats }).elements]));
1745
+ const textOfPage = new Map(pages.map((p) => [p.path, untag(wholeText(parseHtml(p.html)))]));
1746
+ const stemOfPath = (p) => (String(p ?? "").split(/[?#]/)[0].split("/").pop() ?? "").replace(/\.[^.]+$/, "").toLowerCase();
1747
+ const pictureAt = (f) => {
1748
+ const v = draft.data?.[f.key];
1749
+ if (f.type === "image") return typeof v === "string" ? v : typeof v?.src === "string" ? v.src : null;
1750
+ const sub = (f.fields ?? []).find((g) => g.type === "image");
1751
+ return sub && typeof v?.[sub.key] === "string" ? v[sub.key] : null;
1752
+ };
1753
+ return added.map((f) => {
1754
+ const kind = addedKind(f);
1755
+ const base = { key: f.key, type: f.type, bound: false, live: false, pages: [] };
1756
+ if (!kind) return { ...base, shows: null, reason: "unchecked" };
1757
+ const value = kind === "text" ? draft.data?.[f.key] : pictureAt(f);
1758
+ if (typeof value !== "string" || !value.trim()) return { ...base, shows: null, reason: "no-test-value" };
1759
+ if (!pages.length) return { ...base, shows: null, reason: "not-rendered" };
1760
+ const shown = [];
1761
+ let isBound = false;
1762
+ let live = false;
1763
+ for (const p of pages) {
1764
+ const mine = (bound.get(p.path) ?? []).filter((e) => e.binding && e.binding.collection === collection && e.binding.item === draft.id && (e.binding.field === `/${f.key}` || e.binding.field.startsWith(`/${f.key}/`)));
1765
+ const seen =
1766
+ mine.length > 0 ||
1767
+ (kind === "text"
1768
+ ? (textOfPage.get(p.path) ?? "").includes(collapse(untag(value)))
1769
+ : [...p.html.matchAll(/<(?:img|source)\b[^>]*>/gi)].some((m) => m[0].toLowerCase().includes(stemOfPath(value))));
1770
+ if (!seen) continue;
1771
+ shown.push(p.path);
1772
+ if (mine.length) isBound = true;
1773
+ if (mine.some((e) => e.exact)) live = true;
1774
+ }
1775
+ return { ...base, shows: shown.length > 0, bound: isBound, live, pages: shown, ...(shown.length && !isBound ? { reason: "unbound" } : {}) };
1776
+ });
1777
+ }