@preprio/toolkit 1.0.0-rc.5 → 1.0.0-rc.6

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.
@@ -540,6 +540,98 @@ var init_field_name = __esm({
540
540
  }
541
541
  });
542
542
 
543
+ // src/core/live-preview/internal-links.ts
544
+ function anchorsIn(html) {
545
+ const out = [];
546
+ for (const match of html.matchAll(ANCHOR)) {
547
+ const attributes = match[1];
548
+ const inner = match[2];
549
+ out.push({
550
+ start: match.index ?? 0,
551
+ end: (match.index ?? 0) + match[0].length,
552
+ attributes,
553
+ inner,
554
+ href: HREF.exec(attributes)?.[3] ?? null,
555
+ text: inner.replace(TAGS, "").trim()
556
+ });
557
+ }
558
+ return out;
559
+ }
560
+ function fit(pushed, page) {
561
+ if (!isInternal(pushed) && pushed.href !== page.href) return 0;
562
+ if (isInternal(pushed) && page.href !== null && INTERNAL_HREF.test(page.href))
563
+ return 0;
564
+ return pushed.text === page.text ? 2 : 1;
565
+ }
566
+ function align(pushed, page) {
567
+ const rows = pushed.length + 1;
568
+ const cols = page.length + 1;
569
+ const score = Array.from(
570
+ { length: rows },
571
+ () => new Array(cols).fill(0)
572
+ );
573
+ for (let i2 = pushed.length - 1; i2 >= 0; i2--) {
574
+ for (let j2 = page.length - 1; j2 >= 0; j2--) {
575
+ const pair = fit(pushed[i2], page[j2]);
576
+ score[i2][j2] = Math.max(
577
+ score[i2 + 1][j2],
578
+ score[i2][j2 + 1],
579
+ pair ? pair + score[i2 + 1][j2 + 1] : 0
580
+ );
581
+ }
582
+ }
583
+ const out = new Array(pushed.length).fill(-1);
584
+ let i = 0;
585
+ let j = 0;
586
+ while (i < pushed.length && j < page.length) {
587
+ const pair = fit(pushed[i], page[j]);
588
+ if (pair && score[i][j] === pair + score[i + 1][j + 1]) {
589
+ out[i++] = j++;
590
+ } else if (score[i + 1][j] >= score[i][j + 1]) {
591
+ i++;
592
+ } else {
593
+ j++;
594
+ }
595
+ }
596
+ return out;
597
+ }
598
+ function resolveInternalLinks(current, value) {
599
+ const pushed = anchorsIn(value);
600
+ if (!pushed.some(isInternal)) return value;
601
+ const page = typeof current === "string" ? anchorsIn(current) : [];
602
+ const pairs = align(pushed, page);
603
+ let out = "";
604
+ let cursor = 0;
605
+ pushed.forEach((anchor, i) => {
606
+ if (!isInternal(anchor)) return;
607
+ out += value.slice(cursor, anchor.start);
608
+ const match = pairs[i] >= 0 ? page[pairs[i]] : null;
609
+ if (match?.href != null) {
610
+ const href = match.href;
611
+ const attributes = anchor.attributes.replace(
612
+ HREF,
613
+ (_all, lead, quote) => `${lead}${quote}${href}${quote}`
614
+ );
615
+ out += `<a${attributes}>${anchor.inner}</a>`;
616
+ } else {
617
+ out += anchor.inner;
618
+ }
619
+ cursor = anchor.end;
620
+ });
621
+ return out + value.slice(cursor);
622
+ }
623
+ var INTERNAL_HREF, ANCHOR, HREF, TAGS, isInternal;
624
+ var init_internal_links = __esm({
625
+ "src/core/live-preview/internal-links.ts"() {
626
+ "use strict";
627
+ INTERNAL_HREF = /^[pa]uuid#/;
628
+ ANCHOR = /<a\b([^>]*)>([\s\S]*?)<\/a>/gi;
629
+ HREF = /(\shref\s*=\s*)(["'])(.*?)\2/i;
630
+ TAGS = /<[^>]+>/g;
631
+ isInternal = (anchor) => anchor.href !== null && INTERNAL_HREF.test(anchor.href);
632
+ }
633
+ });
634
+
543
635
  // src/core/live-preview/patch.ts
544
636
  function mergeObject(current, incoming) {
545
637
  let changed = false;
@@ -707,7 +799,8 @@ function writeAtPath(root, fullPath, value, markChanged = () => {
707
799
  } else if (!isPatchable(container[key])) {
708
800
  return root;
709
801
  } else {
710
- next = keepEditLink(container[key], value);
802
+ const linked = typeof value === "string" ? resolveInternalLinks(container[key], value) : value;
803
+ next = keepEditLink(container[key], linked);
711
804
  }
712
805
  if (container[key] === next) return root;
713
806
  const newRoot = cloneNode(root);
@@ -823,6 +916,7 @@ var init_patch = __esm({
823
916
  init_guards();
824
917
  import_stega2 = require("@vercel/stega");
825
918
  init_field_name();
919
+ init_internal_links();
826
920
  INDEX_SEGMENT = /^\d+$/;
827
921
  FORBIDDEN_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
828
922
  }
@@ -155,7 +155,7 @@ function createLiveStarter(options = {}) {
155
155
  }
156
156
  },
157
157
  start() {
158
- starting ?? (starting = import("./live-preview-KO4IKWTX.js").then((runtime) => {
158
+ starting ?? (starting = import("./live-preview-V2X6GPSK.js").then((runtime) => {
159
159
  const started = runtime.createLiveStore(options.storeOptions?.());
160
160
  options.onStart?.(started, runtime);
161
161
  bridge = runtime.createLiveBridge(started, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preprio/toolkit",
3
- "version": "1.0.0-rc.5",
3
+ "version": "1.0.0-rc.6",
4
4
  "description": "Prepr personalization, A/B testing and preview toolkit for React, Next.js, Nuxt, Astro, SvelteKit and Express",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Prepr B.V. (https://prepr.io)",