@preprio/toolkit 1.0.0-rc.4 → 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.
- package/README.md +2 -0
- package/dist/astro/index.cjs +126 -18
- package/dist/astro/index.js +128 -19
- package/dist/{chunk-A2J7R4LR.js → chunk-I26P2EQI.js} +1 -1
- package/dist/{chunk-QWTSVPEQ.js → chunk-OCTRCNW2.js} +1 -1
- package/dist/{chunk-KM2GME62.js → chunk-RWFAFYHU.js} +90 -1
- package/dist/{chunk-6PMBN7OR.js → chunk-SAOE54OB.js} +1 -1
- package/dist/{chunk-K62RW46G.js → chunk-WQRPW3TM.js} +1 -1
- package/dist/index.cjs +128 -19
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +44 -23
- package/dist/{live-preview-HCBNBC6C.js → live-preview-NUDEDAYQ.js} +2 -2
- package/dist/nextjs/components.cjs +1 -1
- package/dist/nextjs/components.js +2 -2
- package/dist/nextjs/index.cjs +189 -20
- package/dist/nextjs/index.d.cts +47 -1
- package/dist/nextjs/index.d.ts +47 -1
- package/dist/nextjs/index.js +190 -21
- package/dist/nextjs/live-action.js +3 -3
- package/dist/nextjs/live.cjs +102 -6
- package/dist/nextjs/live.d.cts +7 -1
- package/dist/nextjs/live.d.ts +7 -1
- package/dist/nextjs/live.js +8 -6
- package/dist/nuxt/index.cjs +1 -1
- package/dist/nuxt/index.js +1 -1
- package/dist/nuxt/{live-preview-KO4IKWTX.js → live-preview-V2X6GPSK.js} +88 -1
- package/dist/nuxt/live.cjs +130 -17
- package/dist/nuxt/live.js +38 -18
- package/dist/react/components.cjs +1 -1
- package/dist/react/components.js +1 -1
- package/dist/sveltekit/index.cjs +1 -1
- package/dist/sveltekit/index.js +1 -1
- package/dist/sveltekit/{live-preview-KO4IKWTX.js → live-preview-V2X6GPSK.js} +88 -1
- package/dist/sveltekit/live.cjs +130 -17
- package/dist/sveltekit/live.js +38 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -179,6 +179,8 @@ const config = {
|
|
|
179
179
|
};
|
|
180
180
|
```
|
|
181
181
|
|
|
182
|
+
Test fixtures written by hand against the generated types then need an `_id` too. Use `createPreprIds({ optionalInTypes: true })` instead to type it as optional, see [Hand-written test data](./docs/live-preview.md#hand-written-test-data).
|
|
183
|
+
|
|
182
184
|
No Codegen? See [the other two ways](./docs/live-preview.md#preparing-your-queries).
|
|
183
185
|
|
|
184
186
|
**5. Run your query results through the live preview helper**, with your preview flag as `enabled`:
|
package/dist/astro/index.cjs
CHANGED
|
@@ -34,7 +34,7 @@ __export(astro_exports, {
|
|
|
34
34
|
module.exports = __toCommonJS(astro_exports);
|
|
35
35
|
|
|
36
36
|
// src/version.ts
|
|
37
|
-
var VERSION = "1.0.0-rc.
|
|
37
|
+
var VERSION = "1.0.0-rc.6";
|
|
38
38
|
|
|
39
39
|
// src/core/constants.ts
|
|
40
40
|
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
@@ -565,6 +565,94 @@ function indexSnapshot(data) {
|
|
|
565
565
|
|
|
566
566
|
// src/core/live-preview/patch.ts
|
|
567
567
|
var import_stega2 = require("@vercel/stega");
|
|
568
|
+
|
|
569
|
+
// src/core/live-preview/internal-links.ts
|
|
570
|
+
var INTERNAL_HREF = /^[pa]uuid#/;
|
|
571
|
+
var ANCHOR = /<a\b([^>]*)>([\s\S]*?)<\/a>/gi;
|
|
572
|
+
var HREF = /(\shref\s*=\s*)(["'])(.*?)\2/i;
|
|
573
|
+
var TAGS = /<[^>]+>/g;
|
|
574
|
+
function anchorsIn(html) {
|
|
575
|
+
const out = [];
|
|
576
|
+
for (const match of html.matchAll(ANCHOR)) {
|
|
577
|
+
const attributes = match[1];
|
|
578
|
+
const inner = match[2];
|
|
579
|
+
out.push({
|
|
580
|
+
start: match.index ?? 0,
|
|
581
|
+
end: (match.index ?? 0) + match[0].length,
|
|
582
|
+
attributes,
|
|
583
|
+
inner,
|
|
584
|
+
href: HREF.exec(attributes)?.[3] ?? null,
|
|
585
|
+
text: inner.replace(TAGS, "").trim()
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
return out;
|
|
589
|
+
}
|
|
590
|
+
var isInternal = (anchor) => anchor.href !== null && INTERNAL_HREF.test(anchor.href);
|
|
591
|
+
function fit(pushed, page) {
|
|
592
|
+
if (!isInternal(pushed) && pushed.href !== page.href) return 0;
|
|
593
|
+
if (isInternal(pushed) && page.href !== null && INTERNAL_HREF.test(page.href))
|
|
594
|
+
return 0;
|
|
595
|
+
return pushed.text === page.text ? 2 : 1;
|
|
596
|
+
}
|
|
597
|
+
function align(pushed, page) {
|
|
598
|
+
const rows = pushed.length + 1;
|
|
599
|
+
const cols = page.length + 1;
|
|
600
|
+
const score = Array.from(
|
|
601
|
+
{ length: rows },
|
|
602
|
+
() => new Array(cols).fill(0)
|
|
603
|
+
);
|
|
604
|
+
for (let i2 = pushed.length - 1; i2 >= 0; i2--) {
|
|
605
|
+
for (let j2 = page.length - 1; j2 >= 0; j2--) {
|
|
606
|
+
const pair = fit(pushed[i2], page[j2]);
|
|
607
|
+
score[i2][j2] = Math.max(
|
|
608
|
+
score[i2 + 1][j2],
|
|
609
|
+
score[i2][j2 + 1],
|
|
610
|
+
pair ? pair + score[i2 + 1][j2 + 1] : 0
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
const out = new Array(pushed.length).fill(-1);
|
|
615
|
+
let i = 0;
|
|
616
|
+
let j = 0;
|
|
617
|
+
while (i < pushed.length && j < page.length) {
|
|
618
|
+
const pair = fit(pushed[i], page[j]);
|
|
619
|
+
if (pair && score[i][j] === pair + score[i + 1][j + 1]) {
|
|
620
|
+
out[i++] = j++;
|
|
621
|
+
} else if (score[i + 1][j] >= score[i][j + 1]) {
|
|
622
|
+
i++;
|
|
623
|
+
} else {
|
|
624
|
+
j++;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return out;
|
|
628
|
+
}
|
|
629
|
+
function resolveInternalLinks(current, value) {
|
|
630
|
+
const pushed = anchorsIn(value);
|
|
631
|
+
if (!pushed.some(isInternal)) return value;
|
|
632
|
+
const page = typeof current === "string" ? anchorsIn(current) : [];
|
|
633
|
+
const pairs = align(pushed, page);
|
|
634
|
+
let out = "";
|
|
635
|
+
let cursor = 0;
|
|
636
|
+
pushed.forEach((anchor, i) => {
|
|
637
|
+
if (!isInternal(anchor)) return;
|
|
638
|
+
out += value.slice(cursor, anchor.start);
|
|
639
|
+
const match = pairs[i] >= 0 ? page[pairs[i]] : null;
|
|
640
|
+
if (match?.href != null) {
|
|
641
|
+
const href = match.href;
|
|
642
|
+
const attributes = anchor.attributes.replace(
|
|
643
|
+
HREF,
|
|
644
|
+
(_all, lead, quote) => `${lead}${quote}${href}${quote}`
|
|
645
|
+
);
|
|
646
|
+
out += `<a${attributes}>${anchor.inner}</a>`;
|
|
647
|
+
} else {
|
|
648
|
+
out += anchor.inner;
|
|
649
|
+
}
|
|
650
|
+
cursor = anchor.end;
|
|
651
|
+
});
|
|
652
|
+
return out + value.slice(cursor);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// src/core/live-preview/patch.ts
|
|
568
656
|
var FORBIDDEN_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
569
657
|
function mergeObject(current, incoming) {
|
|
570
658
|
let changed = false;
|
|
@@ -662,7 +750,8 @@ function writeAtPath(root, fullPath, value, markChanged = () => {
|
|
|
662
750
|
} else if (!isPatchable(container[key])) {
|
|
663
751
|
return root;
|
|
664
752
|
} else {
|
|
665
|
-
|
|
753
|
+
const linked = typeof value === "string" ? resolveInternalLinks(container[key], value) : value;
|
|
754
|
+
next = keepEditLink(container[key], linked);
|
|
666
755
|
}
|
|
667
756
|
if (container[key] === next) return root;
|
|
668
757
|
const newRoot = cloneNode(root);
|
|
@@ -1480,34 +1569,53 @@ function entryIdOf(element) {
|
|
|
1480
1569
|
const first = element.items[0];
|
|
1481
1570
|
return isObject(first) && typeof first._id === "string" ? first._id : void 0;
|
|
1482
1571
|
}
|
|
1483
|
-
function
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1572
|
+
function encodeLink(payload) {
|
|
1573
|
+
try {
|
|
1574
|
+
return (0, import_stega4.legacyStegaEncode)(payload);
|
|
1575
|
+
} catch {
|
|
1576
|
+
return (0, import_stega4.vercelStegaEncode)(payload);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
|
|
1487
1580
|
let payload;
|
|
1488
1581
|
try {
|
|
1489
1582
|
payload = (0, import_stega4.vercelStegaDecode)(encoded);
|
|
1490
1583
|
} catch {
|
|
1491
|
-
return
|
|
1584
|
+
return "";
|
|
1492
1585
|
}
|
|
1493
|
-
if (typeof payload?.href !== "string") return
|
|
1586
|
+
if (typeof payload?.href !== "string") return "";
|
|
1494
1587
|
let url;
|
|
1495
1588
|
try {
|
|
1496
1589
|
url = new URL(payload.href);
|
|
1497
1590
|
} catch {
|
|
1498
|
-
return
|
|
1591
|
+
return "";
|
|
1499
1592
|
}
|
|
1500
|
-
if (url.searchParams.has(ENTRY_PARAM)) return
|
|
1501
|
-
url.searchParams.set(ENTRY_PARAM,
|
|
1593
|
+
if (url.searchParams.has(ENTRY_PARAM)) return "";
|
|
1594
|
+
url.searchParams.set(ENTRY_PARAM, entryId);
|
|
1502
1595
|
const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
|
|
1503
|
-
if (!url.searchParams.has("field") &&
|
|
1504
|
-
url.searchParams.set("field",
|
|
1596
|
+
if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
|
|
1597
|
+
url.searchParams.set("field", entryField);
|
|
1505
1598
|
}
|
|
1506
|
-
return (
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1599
|
+
return encodeLink({ ...payload, href: url.toString() });
|
|
1600
|
+
}
|
|
1601
|
+
var MAX_REWRITTEN = 1e4;
|
|
1602
|
+
var rewritten = /* @__PURE__ */ new Map();
|
|
1603
|
+
function annotate(value, ctx) {
|
|
1604
|
+
if (!ctx.entryId) return value;
|
|
1605
|
+
const { cleaned, encoded } = (0, import_stega4.vercelStegaSplit)(value);
|
|
1606
|
+
if (!encoded) return value;
|
|
1607
|
+
const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
|
|
1608
|
+
const key = `${ctx.entryId}
|
|
1609
|
+
${entryField}
|
|
1610
|
+
${ctx.entryOwnerId ?? ""}
|
|
1611
|
+
${encoded}`;
|
|
1612
|
+
let link = rewritten.get(key);
|
|
1613
|
+
if (link === void 0) {
|
|
1614
|
+
link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
|
|
1615
|
+
if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
|
|
1616
|
+
rewritten.set(key, link);
|
|
1617
|
+
}
|
|
1618
|
+
return link ? cleaned + link : value;
|
|
1511
1619
|
}
|
|
1512
1620
|
function walk(value, selections, ctx) {
|
|
1513
1621
|
if (typeof value === "string") return annotate(value, ctx);
|
package/dist/astro/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/version.ts
|
|
2
|
-
var VERSION = "1.0.0-rc.
|
|
2
|
+
var VERSION = "1.0.0-rc.6";
|
|
3
3
|
|
|
4
4
|
// src/core/constants.ts
|
|
5
5
|
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
@@ -534,6 +534,94 @@ function indexSnapshot(data) {
|
|
|
534
534
|
|
|
535
535
|
// src/core/live-preview/patch.ts
|
|
536
536
|
import { vercelStegaSplit as vercelStegaSplit2 } from "@vercel/stega";
|
|
537
|
+
|
|
538
|
+
// src/core/live-preview/internal-links.ts
|
|
539
|
+
var INTERNAL_HREF = /^[pa]uuid#/;
|
|
540
|
+
var ANCHOR = /<a\b([^>]*)>([\s\S]*?)<\/a>/gi;
|
|
541
|
+
var HREF = /(\shref\s*=\s*)(["'])(.*?)\2/i;
|
|
542
|
+
var TAGS = /<[^>]+>/g;
|
|
543
|
+
function anchorsIn(html) {
|
|
544
|
+
const out = [];
|
|
545
|
+
for (const match of html.matchAll(ANCHOR)) {
|
|
546
|
+
const attributes = match[1];
|
|
547
|
+
const inner = match[2];
|
|
548
|
+
out.push({
|
|
549
|
+
start: match.index ?? 0,
|
|
550
|
+
end: (match.index ?? 0) + match[0].length,
|
|
551
|
+
attributes,
|
|
552
|
+
inner,
|
|
553
|
+
href: HREF.exec(attributes)?.[3] ?? null,
|
|
554
|
+
text: inner.replace(TAGS, "").trim()
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
return out;
|
|
558
|
+
}
|
|
559
|
+
var isInternal = (anchor) => anchor.href !== null && INTERNAL_HREF.test(anchor.href);
|
|
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
|
+
|
|
624
|
+
// src/core/live-preview/patch.ts
|
|
537
625
|
var FORBIDDEN_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
538
626
|
function mergeObject(current, incoming) {
|
|
539
627
|
let changed = false;
|
|
@@ -631,7 +719,8 @@ function writeAtPath(root, fullPath, value, markChanged = () => {
|
|
|
631
719
|
} else if (!isPatchable(container[key])) {
|
|
632
720
|
return root;
|
|
633
721
|
} else {
|
|
634
|
-
|
|
722
|
+
const linked = typeof value === "string" ? resolveInternalLinks(container[key], value) : value;
|
|
723
|
+
next = keepEditLink(container[key], linked);
|
|
635
724
|
}
|
|
636
725
|
if (container[key] === next) return root;
|
|
637
726
|
const newRoot = cloneNode(root);
|
|
@@ -1438,8 +1527,9 @@ async function readLiveLogFromRequest(request) {
|
|
|
1438
1527
|
|
|
1439
1528
|
// src/core/live-preview/edit-links.ts
|
|
1440
1529
|
import {
|
|
1441
|
-
|
|
1530
|
+
legacyStegaEncode,
|
|
1442
1531
|
vercelStegaDecode as vercelStegaDecode2,
|
|
1532
|
+
vercelStegaEncode,
|
|
1443
1533
|
vercelStegaSplit as vercelStegaSplit4
|
|
1444
1534
|
} from "@vercel/stega";
|
|
1445
1535
|
var ENTRY_PARAM = "entry";
|
|
@@ -1453,34 +1543,53 @@ function entryIdOf(element) {
|
|
|
1453
1543
|
const first = element.items[0];
|
|
1454
1544
|
return isObject(first) && typeof first._id === "string" ? first._id : void 0;
|
|
1455
1545
|
}
|
|
1456
|
-
function
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1546
|
+
function encodeLink(payload) {
|
|
1547
|
+
try {
|
|
1548
|
+
return legacyStegaEncode(payload);
|
|
1549
|
+
} catch {
|
|
1550
|
+
return vercelStegaEncode(payload);
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
|
|
1460
1554
|
let payload;
|
|
1461
1555
|
try {
|
|
1462
1556
|
payload = vercelStegaDecode2(encoded);
|
|
1463
1557
|
} catch {
|
|
1464
|
-
return
|
|
1558
|
+
return "";
|
|
1465
1559
|
}
|
|
1466
|
-
if (typeof payload?.href !== "string") return
|
|
1560
|
+
if (typeof payload?.href !== "string") return "";
|
|
1467
1561
|
let url;
|
|
1468
1562
|
try {
|
|
1469
1563
|
url = new URL(payload.href);
|
|
1470
1564
|
} catch {
|
|
1471
|
-
return
|
|
1565
|
+
return "";
|
|
1472
1566
|
}
|
|
1473
|
-
if (url.searchParams.has(ENTRY_PARAM)) return
|
|
1474
|
-
url.searchParams.set(ENTRY_PARAM,
|
|
1567
|
+
if (url.searchParams.has(ENTRY_PARAM)) return "";
|
|
1568
|
+
url.searchParams.set(ENTRY_PARAM, entryId);
|
|
1475
1569
|
const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
|
|
1476
|
-
if (!url.searchParams.has("field") &&
|
|
1477
|
-
url.searchParams.set("field",
|
|
1570
|
+
if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
|
|
1571
|
+
url.searchParams.set("field", entryField);
|
|
1478
1572
|
}
|
|
1479
|
-
return
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1573
|
+
return encodeLink({ ...payload, href: url.toString() });
|
|
1574
|
+
}
|
|
1575
|
+
var MAX_REWRITTEN = 1e4;
|
|
1576
|
+
var rewritten = /* @__PURE__ */ new Map();
|
|
1577
|
+
function annotate(value, ctx) {
|
|
1578
|
+
if (!ctx.entryId) return value;
|
|
1579
|
+
const { cleaned, encoded } = vercelStegaSplit4(value);
|
|
1580
|
+
if (!encoded) return value;
|
|
1581
|
+
const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
|
|
1582
|
+
const key = `${ctx.entryId}
|
|
1583
|
+
${entryField}
|
|
1584
|
+
${ctx.entryOwnerId ?? ""}
|
|
1585
|
+
${encoded}`;
|
|
1586
|
+
let link = rewritten.get(key);
|
|
1587
|
+
if (link === void 0) {
|
|
1588
|
+
link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
|
|
1589
|
+
if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
|
|
1590
|
+
rewritten.set(key, link);
|
|
1591
|
+
}
|
|
1592
|
+
return link ? cleaned + link : value;
|
|
1484
1593
|
}
|
|
1485
1594
|
function walk(value, selections, ctx) {
|
|
1486
1595
|
if (typeof value === "string") return annotate(value, ctx);
|
|
@@ -282,6 +282,94 @@ function indexSnapshot(data) {
|
|
|
282
282
|
|
|
283
283
|
// src/core/live-preview/patch.ts
|
|
284
284
|
import { vercelStegaSplit as vercelStegaSplit2 } from "@vercel/stega";
|
|
285
|
+
|
|
286
|
+
// src/core/live-preview/internal-links.ts
|
|
287
|
+
var INTERNAL_HREF = /^[pa]uuid#/;
|
|
288
|
+
var ANCHOR = /<a\b([^>]*)>([\s\S]*?)<\/a>/gi;
|
|
289
|
+
var HREF = /(\shref\s*=\s*)(["'])(.*?)\2/i;
|
|
290
|
+
var TAGS = /<[^>]+>/g;
|
|
291
|
+
function anchorsIn(html) {
|
|
292
|
+
const out = [];
|
|
293
|
+
for (const match of html.matchAll(ANCHOR)) {
|
|
294
|
+
const attributes = match[1];
|
|
295
|
+
const inner = match[2];
|
|
296
|
+
out.push({
|
|
297
|
+
start: match.index ?? 0,
|
|
298
|
+
end: (match.index ?? 0) + match[0].length,
|
|
299
|
+
attributes,
|
|
300
|
+
inner,
|
|
301
|
+
href: HREF.exec(attributes)?.[3] ?? null,
|
|
302
|
+
text: inner.replace(TAGS, "").trim()
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
return out;
|
|
306
|
+
}
|
|
307
|
+
var isInternal = (anchor) => anchor.href !== null && INTERNAL_HREF.test(anchor.href);
|
|
308
|
+
function fit(pushed, page) {
|
|
309
|
+
if (!isInternal(pushed) && pushed.href !== page.href) return 0;
|
|
310
|
+
if (isInternal(pushed) && page.href !== null && INTERNAL_HREF.test(page.href))
|
|
311
|
+
return 0;
|
|
312
|
+
return pushed.text === page.text ? 2 : 1;
|
|
313
|
+
}
|
|
314
|
+
function align(pushed, page) {
|
|
315
|
+
const rows = pushed.length + 1;
|
|
316
|
+
const cols = page.length + 1;
|
|
317
|
+
const score = Array.from(
|
|
318
|
+
{ length: rows },
|
|
319
|
+
() => new Array(cols).fill(0)
|
|
320
|
+
);
|
|
321
|
+
for (let i2 = pushed.length - 1; i2 >= 0; i2--) {
|
|
322
|
+
for (let j2 = page.length - 1; j2 >= 0; j2--) {
|
|
323
|
+
const pair = fit(pushed[i2], page[j2]);
|
|
324
|
+
score[i2][j2] = Math.max(
|
|
325
|
+
score[i2 + 1][j2],
|
|
326
|
+
score[i2][j2 + 1],
|
|
327
|
+
pair ? pair + score[i2 + 1][j2 + 1] : 0
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const out = new Array(pushed.length).fill(-1);
|
|
332
|
+
let i = 0;
|
|
333
|
+
let j = 0;
|
|
334
|
+
while (i < pushed.length && j < page.length) {
|
|
335
|
+
const pair = fit(pushed[i], page[j]);
|
|
336
|
+
if (pair && score[i][j] === pair + score[i + 1][j + 1]) {
|
|
337
|
+
out[i++] = j++;
|
|
338
|
+
} else if (score[i + 1][j] >= score[i][j + 1]) {
|
|
339
|
+
i++;
|
|
340
|
+
} else {
|
|
341
|
+
j++;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return out;
|
|
345
|
+
}
|
|
346
|
+
function resolveInternalLinks(current, value) {
|
|
347
|
+
const pushed = anchorsIn(value);
|
|
348
|
+
if (!pushed.some(isInternal)) return value;
|
|
349
|
+
const page = typeof current === "string" ? anchorsIn(current) : [];
|
|
350
|
+
const pairs = align(pushed, page);
|
|
351
|
+
let out = "";
|
|
352
|
+
let cursor = 0;
|
|
353
|
+
pushed.forEach((anchor, i) => {
|
|
354
|
+
if (!isInternal(anchor)) return;
|
|
355
|
+
out += value.slice(cursor, anchor.start);
|
|
356
|
+
const match = pairs[i] >= 0 ? page[pairs[i]] : null;
|
|
357
|
+
if (match?.href != null) {
|
|
358
|
+
const href = match.href;
|
|
359
|
+
const attributes = anchor.attributes.replace(
|
|
360
|
+
HREF,
|
|
361
|
+
(_all, lead, quote) => `${lead}${quote}${href}${quote}`
|
|
362
|
+
);
|
|
363
|
+
out += `<a${attributes}>${anchor.inner}</a>`;
|
|
364
|
+
} else {
|
|
365
|
+
out += anchor.inner;
|
|
366
|
+
}
|
|
367
|
+
cursor = anchor.end;
|
|
368
|
+
});
|
|
369
|
+
return out + value.slice(cursor);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/core/live-preview/patch.ts
|
|
285
373
|
var INDEX_SEGMENT = /^\d+$/;
|
|
286
374
|
var FORBIDDEN_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
287
375
|
function mergeObject(current, incoming) {
|
|
@@ -450,7 +538,8 @@ function writeAtPath(root, fullPath, value, markChanged = () => {
|
|
|
450
538
|
} else if (!isPatchable(container[key])) {
|
|
451
539
|
return root;
|
|
452
540
|
} else {
|
|
453
|
-
|
|
541
|
+
const linked = typeof value === "string" ? resolveInternalLinks(container[key], value) : value;
|
|
542
|
+
next = keepEditLink(container[key], linked);
|
|
454
543
|
}
|
|
455
544
|
if (container[key] === next) return root;
|
|
456
545
|
const newRoot = cloneNode(root);
|