@bettercms-ai/sdk 1.46.0 → 1.47.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/dist/index.cjs CHANGED
@@ -99,6 +99,7 @@ __export(index_exports, {
99
99
  listPages: () => listPages,
100
100
  listSubmissions: () => listSubmissions,
101
101
  listWorkspaces: () => listWorkspaces,
102
+ markEditable: () => markEditable,
102
103
  onContentApplied: () => onContentApplied,
103
104
  plain: () => plain,
104
105
  portableText: () => portableText,
@@ -123,6 +124,7 @@ __export(index_exports, {
123
124
  signInWithGoogle: () => signInWithGoogle,
124
125
  signUp: () => signUp,
125
126
  stegaClean: () => stegaClean,
127
+ stegaSplit: () => stegaSplit,
126
128
  stripStega: () => stripStega,
127
129
  submitForm: () => submitForm,
128
130
  toggleOption: () => toggleOption,
@@ -1429,6 +1431,7 @@ function createClient(options) {
1429
1431
  }
1430
1432
  const draft = perspective === "drafts";
1431
1433
  const stega = options.stega ?? draft;
1434
+ const stegaParam = stega === 2 ? "2" : stega ? "1" : void 0;
1432
1435
  return {
1433
1436
  perspective,
1434
1437
  async getEntry(slug, opts) {
@@ -1436,12 +1439,12 @@ function createClient(options) {
1436
1439
  token: previewToken,
1437
1440
  depth: opts?.depth,
1438
1441
  select: opts?.select?.join(","),
1439
- stega: stega ? "1" : void 0
1442
+ stega: stegaParam
1440
1443
  })}` : `${base}/content-entries/${encodeURIComponent(slug)}${qs({
1441
1444
  depth: opts?.depth,
1442
1445
  select: opts?.select?.join(","),
1443
1446
  perspective: draft ? "draft" : void 0,
1444
- stega: draft && stega ? "1" : void 0,
1447
+ stega: draft ? stegaParam : void 0,
1445
1448
  projectId: defaultProjectId,
1446
1449
  branch
1447
1450
  })}`;
@@ -1456,7 +1459,7 @@ function createClient(options) {
1456
1459
  select: opts?.select?.join(","),
1457
1460
  ...listQueryParams(opts?.filter, opts?.sort),
1458
1461
  perspective: draft ? "draft" : void 0,
1459
- stega: draft && stega ? "1" : void 0,
1462
+ stega: draft ? stegaParam : void 0,
1460
1463
  projectId: defaultProjectId,
1461
1464
  branch
1462
1465
  })}`;
@@ -1466,7 +1469,7 @@ function createClient(options) {
1466
1469
  if (draft && previewToken && tokenSlug(previewToken) === slug) {
1467
1470
  const url = `${apiUrl}/api/v1/preview/${encodeURIComponent(slug)}${qs({
1468
1471
  token: previewToken,
1469
- stega: stega ? "1" : void 0
1472
+ stega: stegaParam
1470
1473
  })}`;
1471
1474
  const d = unwrap(await fetchJSON(url));
1472
1475
  return {
@@ -1496,6 +1499,8 @@ function createClient(options) {
1496
1499
  page: opts?.page,
1497
1500
  perPage: opts?.perPage,
1498
1501
  slug: opts?.slug,
1502
+ // Framed only on a branch, the draft lane of this list; never v1, which frames no blocks.
1503
+ stega: draft && stega === 2 ? "2" : void 0,
1499
1504
  projectId: defaultProjectId,
1500
1505
  branch
1501
1506
  })}`;
@@ -1511,7 +1516,7 @@ function createClient(options) {
1511
1516
  return unwrap(await fetchJSON(url));
1512
1517
  },
1513
1518
  async getLayout() {
1514
- const url = `${base}/layout${qs({ perspective: draft ? "draft" : void 0, projectId: defaultProjectId, branch })}`;
1519
+ const url = `${base}/layout${qs({ perspective: draft ? "draft" : void 0, stega: draft && stega === 2 ? "2" : void 0, projectId: defaultProjectId, branch })}`;
1515
1520
  try {
1516
1521
  return unwrap(await fetchJSON(url));
1517
1522
  } catch (error) {
@@ -1539,7 +1544,7 @@ var ZERO = "\u200B";
1539
1544
  var ONE = "\u200C";
1540
1545
  var START = "\u2060";
1541
1546
  var END = "\u2061";
1542
- var FRAME = new RegExp(`${START}([${ZERO}${ONE}]*)${END}`);
1547
+ var FRAMES = new RegExp(`${START}([${ZERO}${ONE}]*)${END}`, "g");
1543
1548
  function toBits(s) {
1544
1549
  const bytes = new TextEncoder().encode(s);
1545
1550
  let out = "";
@@ -1560,23 +1565,50 @@ function fromBits(bits) {
1560
1565
  function encodeStega(clean, payload) {
1561
1566
  return `${clean}${START}${toBits(JSON.stringify(payload))}${END}`;
1562
1567
  }
1563
- function decodeStega(value) {
1564
- const m = value.match(FRAME);
1565
- if (!m || m[1] == null) return null;
1568
+ var V2_TYPES = { e: "entry", p: "page", l: "layout", c: "component" };
1569
+ function payloadOf(bits) {
1566
1570
  try {
1567
- const payload = JSON.parse(fromBits(m[1]));
1568
- return { clean: value.replace(FRAME, ""), payload };
1571
+ const raw = JSON.parse(fromBits(bits));
1572
+ if (typeof raw.t === "string" && raw.t in V2_TYPES && typeof raw.id === "string" && typeof raw.f === "string") {
1573
+ return { ws: "", type: V2_TYPES[raw.t], id: raw.id, slug: "", field: raw.f };
1574
+ }
1575
+ return raw;
1569
1576
  } catch {
1570
1577
  return null;
1571
1578
  }
1572
1579
  }
1573
- function stripStega(value) {
1574
- return value.replace(FRAME, "");
1580
+ var TARGETS = { entry: 1, page: 1, layout: 1, component: 1 };
1581
+ var stegaRef = (payload) => typeof payload.type === "string" && payload.type in TARGETS && typeof payload.id === "string" && typeof payload.field === "string" ? `${payload.type}:${payload.id}:${payload.field}` : null;
1582
+ function decodeStega(value) {
1583
+ for (const frame of value.matchAll(FRAMES)) {
1584
+ if (!frame[1]) continue;
1585
+ const payload = payloadOf(frame[1]);
1586
+ if (!payload) return null;
1587
+ const ref = stegaRef(payload);
1588
+ return { clean: stegaClean(value), payload, ...ref ? { ref } : {} };
1589
+ }
1590
+ return null;
1591
+ }
1592
+ function stegaSplit(value) {
1593
+ const decoded = decodeStega(value);
1594
+ return { clean: decoded?.clean ?? stegaClean(value), ref: decoded?.ref ?? null };
1575
1595
  }
1576
1596
  var STEGA_OPEN = `${START}${END}`;
1577
- var FRAMES = new RegExp(`${START}([${ZERO}${ONE}]*)${END}`, "g");
1578
1597
  function stegaClean(value) {
1579
- return value.replace(FRAMES, "");
1598
+ if (typeof value === "string") return value.includes(START) ? value.replace(FRAMES, "") : value;
1599
+ if (Array.isArray(value)) {
1600
+ const out = value.map((v) => stegaClean(v));
1601
+ return out.some((v, i) => v !== value[i]) ? out : value;
1602
+ }
1603
+ if (value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype) {
1604
+ const entries = Object.entries(value).map(([k, v]) => [k, stegaClean(v)]);
1605
+ return entries.some(([k, v]) => v !== value[k]) ? Object.fromEntries(entries) : value;
1606
+ }
1607
+ return value;
1608
+ }
1609
+ var stripStega = stegaClean;
1610
+ function stegaFrames(text) {
1611
+ return [...text.matchAll(FRAMES)].map((m) => ({ index: m.index, end: m.index + m[0].length, payload: m[1] ? payloadOf(m[1]) : null, open: !m[1] }));
1580
1612
  }
1581
1613
  function resolveStega(node) {
1582
1614
  let offset = 0;
@@ -1588,22 +1620,126 @@ function resolveStega(node) {
1588
1620
  }
1589
1621
  const text = up.textContent ?? "";
1590
1622
  const opens = [];
1591
- for (const frame of text.matchAll(FRAMES)) {
1592
- if (!frame[1]) {
1623
+ for (const frame of stegaFrames(text)) {
1624
+ if (frame.open) {
1593
1625
  opens.push(frame.index);
1594
1626
  continue;
1595
1627
  }
1596
1628
  const open = opens.pop();
1597
- if (frame.index + frame[0].length <= offset || open === void 0 || open >= offset + length) continue;
1598
- try {
1599
- return { payload: JSON.parse(fromBits(frame[1])), element: up };
1600
- } catch {
1601
- break;
1629
+ if (frame.end <= offset || open === void 0 || open >= offset + length) continue;
1630
+ if (!frame.payload) break;
1631
+ return { payload: frame.payload, element: up };
1632
+ }
1633
+ }
1634
+ return null;
1635
+ }
1636
+
1637
+ // src/mark-editable.ts
1638
+ var BOUND = ["data-bcms-field", "data-bcms-layout-field", "data-bcms-entry-field"];
1639
+ var NOT_PROSE = { SCRIPT: "script", STYLE: "style", NOSCRIPT: "script", TEMPLATE: "script", TITLE: "head", TEXTAREA: "form" };
1640
+ var INLINE_BREAKS = /* @__PURE__ */ new Set(["BR", "WBR"]);
1641
+ var SVG = "http://www.w3.org/2000/svg";
1642
+ var START2 = "\u2060";
1643
+ var isBound = (el) => BOUND.some((name) => el.hasAttribute(name));
1644
+ function refIn(text) {
1645
+ const payload = stegaFrames(text).find((frame) => frame.payload)?.payload;
1646
+ return payload ? stegaRef(payload) : null;
1647
+ }
1648
+ function stampOf(ref, pageId) {
1649
+ const [target, id] = ref.split(":", 2);
1650
+ const path = ref.slice(`${target}:${id}:`.length);
1651
+ if (!path) return null;
1652
+ if (target === "entry") return ["data-bcms-entry-field", ref];
1653
+ if (target === "layout") return ["data-bcms-layout-field", ref];
1654
+ if (target === "page" && pageId && id === pageId) return ["data-bcms-field", path];
1655
+ return null;
1656
+ }
1657
+ function markEditable(root, opts) {
1658
+ const leaks = [];
1659
+ const texts = [];
1660
+ const all = [];
1661
+ const leak = (kind, where, text) => leaks.push({ kind, ref: refIn(text), where });
1662
+ const visit = (el, skip) => {
1663
+ for (const attr of Array.from(el.attributes)) {
1664
+ if (!attr.value.includes(START2)) continue;
1665
+ leak("attribute", `${el.tagName.toLowerCase()}[${attr.name}]`, attr.value);
1666
+ el.setAttribute(attr.name, stegaClean(attr.value));
1667
+ }
1668
+ const own = el.tagName === "HEAD" ? "head" : NOT_PROSE[el.tagName];
1669
+ const next = skip ?? own ?? (el.namespaceURI === SVG ? "bound" : null) ?? (el !== root && isBound(el) ? "bound" : null);
1670
+ for (const node of [...Array.from(el.childNodes), ...Array.from(el.content?.childNodes ?? [])]) {
1671
+ if (node.nodeType === 1) visit(node, next);
1672
+ else if (node.nodeType === 8 && node.data.includes(START2)) {
1673
+ const comment = node;
1674
+ leak("comment", el.tagName.toLowerCase(), comment.data);
1675
+ comment.data = stegaClean(comment.data);
1676
+ } else if (node.nodeType === 3) {
1677
+ const text = node;
1678
+ all.push(text);
1679
+ if (next === null) texts.push(text);
1680
+ else if (next !== "bound" && text.data.includes(START2)) {
1681
+ leak(next, el.tagName.toLowerCase(), text.data);
1682
+ text.data = stegaClean(text.data);
1683
+ }
1602
1684
  }
1603
1685
  }
1686
+ };
1687
+ visit(root, null);
1688
+ const starts = [];
1689
+ let full = "";
1690
+ for (const text of texts) {
1691
+ starts.push(full.length);
1692
+ full += text.data;
1693
+ }
1694
+ const nodeAt = (offset) => {
1695
+ let lo = 0;
1696
+ let hi = starts.length - 1;
1697
+ while (lo < hi) {
1698
+ const mid = lo + hi + 1 >> 1;
1699
+ if (starts[mid] <= offset) lo = mid;
1700
+ else hi = mid - 1;
1701
+ }
1702
+ return lo;
1703
+ };
1704
+ let stamped = 0;
1705
+ let unresolved = 0;
1706
+ const opens = [];
1707
+ for (const frame of stegaFrames(full)) {
1708
+ if (frame.open) {
1709
+ opens.push(frame.index);
1710
+ continue;
1711
+ }
1712
+ const start = opens.pop() ?? starts[nodeAt(frame.index)];
1713
+ if (!frame.payload) continue;
1714
+ const first = texts[nodeAt(start)];
1715
+ const last = texts[nodeAt(frame.end - 1)];
1716
+ const holder = commonElement(first, last);
1717
+ const ref = stegaRef(frame.payload);
1718
+ const stamp = ref ? stampOf(ref, opts.pageId) : null;
1719
+ if (!holder || holder === root || !stamp || !holdsOnly(holder, stegaClean(full.slice(start, frame.end)))) {
1720
+ unresolved += 1;
1721
+ continue;
1722
+ }
1723
+ holder.setAttribute(stamp[0], stamp[1]);
1724
+ holder.setAttribute("data-bcms-kind", "text");
1725
+ holder.setAttribute("data-bcms-match", "stega");
1726
+ stamped += 1;
1604
1727
  }
1728
+ if (opts.strip) {
1729
+ for (const text of all) if (text.data.includes(START2)) text.data = stegaClean(text.data);
1730
+ }
1731
+ return { stamped, leaks, unresolved };
1732
+ }
1733
+ function commonElement(a, b) {
1734
+ const above = /* @__PURE__ */ new Set();
1735
+ for (let n = a.parentNode; n; n = n.parentNode) above.add(n);
1736
+ for (let n = b.parentNode; n; n = n.parentNode) if (above.has(n) && n.nodeType === 1) return n;
1605
1737
  return null;
1606
1738
  }
1739
+ function holdsOnly(el, value) {
1740
+ if (stegaClean(el.textContent ?? "").trim() !== value.trim()) return false;
1741
+ return el.childElementCount === 0 || Array.from(el.querySelectorAll("*")).every((child) => INLINE_BREAKS.has(child.tagName) || stegaClean(child.textContent ?? "").trim() !== "");
1742
+ }
1607
1743
 
1608
1744
  // src/content-applied.ts
1609
1745
  var CONTENT_APPLIED = "bcms:content-applied";
@@ -1762,7 +1898,10 @@ function entrySeoInput(entry) {
1762
1898
  noindex: noindex === true
1763
1899
  };
1764
1900
  }
1765
- function resolveSeo(page, defaults = {}, location = {}) {
1901
+ function resolveSeo(framedPage, framedDefaults = {}, location = {}) {
1902
+ const { title: pageTitle, metaTitle, metaDescription, metaJson, slug, noindex } = framedPage;
1903
+ const page = stegaClean({ title: pageTitle, metaTitle, metaDescription, metaJson, slug, noindex });
1904
+ const defaults = stegaClean(framedDefaults);
1766
1905
  const meta = page.metaJson ?? {};
1767
1906
  const title = page.metaTitle || page.title || "";
1768
1907
  const description = page.metaDescription || defaults.metaDescription || "";
@@ -2279,6 +2418,7 @@ function inEditor(win = globalThis.window) {
2279
2418
  listPages,
2280
2419
  listSubmissions,
2281
2420
  listWorkspaces,
2421
+ markEditable,
2282
2422
  onContentApplied,
2283
2423
  plain,
2284
2424
  portableText,
@@ -2303,6 +2443,7 @@ function inEditor(win = globalThis.window) {
2303
2443
  signInWithGoogle,
2304
2444
  signUp,
2305
2445
  stegaClean,
2446
+ stegaSplit,
2306
2447
  stripStega,
2307
2448
  submitForm,
2308
2449
  toggleOption,