@stream-mdx/react 0.5.0 → 1.0.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/CHANGELOG.md +371 -0
- package/README.md +79 -4
- package/dist/blocks-renderer-DAOBFBfi.d.ts +29 -0
- package/dist/blocks-renderer-DQEL1kJW.d.cts +29 -0
- package/dist/components/bottom-stick-scroll-area.cjs +117 -193
- package/dist/components/bottom-stick-scroll-area.mjs +112 -183
- package/dist/components/diff-renderer.cjs +86 -0
- package/dist/components/diff-renderer.d.cts +11 -0
- package/dist/components/diff-renderer.d.ts +11 -0
- package/dist/components/diff-renderer.mjs +66 -0
- package/dist/components/index.cjs +2761 -1508
- package/dist/components/index.d.cts +7 -2
- package/dist/components/index.d.ts +7 -2
- package/dist/components/index.mjs +2730 -1493
- package/dist/components/katex-math.cjs +220 -0
- package/dist/components/katex-math.d.cts +10 -0
- package/dist/components/katex-math.d.ts +10 -0
- package/dist/components/katex-math.mjs +188 -0
- package/dist/contexts/math-tracker.cjs +240 -521
- package/dist/contexts/math-tracker.mjs +235 -513
- package/dist/index-Cmne1yUK.d.cts +723 -0
- package/dist/index-D7dyyu3q.d.ts +723 -0
- package/dist/index.cjs +13029 -6475
- package/dist/index.d.cts +69 -39
- package/dist/index.d.ts +69 -39
- package/dist/index.mjs +13007 -6442
- package/dist/math/display-wrapper.cjs +7 -15
- package/dist/math/display-wrapper.d.cts +2 -2
- package/dist/math/display-wrapper.d.ts +2 -2
- package/dist/math/display-wrapper.mjs +2 -6
- package/dist/node-views-Bpc6Gkgl.d.cts +13 -0
- package/dist/node-views-DpnYdaAS.d.ts +13 -0
- package/dist/renderer/hooks.cjs +35 -20
- package/dist/renderer/hooks.d.cts +22 -1
- package/dist/renderer/hooks.d.ts +22 -1
- package/dist/renderer/hooks.mjs +27 -12
- package/dist/renderer/patch-batching.cjs +5 -8
- package/dist/renderer/patch-coalescing.cjs +5 -8
- package/dist/renderer/patch-commit-scheduler.cjs +138 -356
- package/dist/renderer/patch-commit-scheduler.d.cts +13 -1
- package/dist/renderer/patch-commit-scheduler.d.ts +13 -1
- package/dist/renderer/patch-commit-scheduler.mjs +133 -348
- package/dist/renderer/store.cjs +3775 -1373
- package/dist/renderer/store.d.cts +108 -3
- package/dist/renderer/store.d.ts +108 -3
- package/dist/renderer/store.mjs +3778 -1362
- package/dist/renderer/virtualized-code.cjs +415 -71
- package/dist/renderer/virtualized-code.d.cts +24 -23
- package/dist/renderer/virtualized-code.d.ts +24 -23
- package/dist/renderer/virtualized-code.mjs +409 -62
- package/dist/renderer.cjs +10655 -4334
- package/dist/renderer.d.cts +98 -29
- package/dist/renderer.d.ts +98 -29
- package/dist/renderer.mjs +10582 -4212
- package/dist/server-regions-CPLHGli0.d.cts +32 -0
- package/dist/server-regions-mUhyoKi7.d.ts +32 -0
- package/dist/server.cjs +10195 -3872
- package/dist/server.d.cts +17 -252
- package/dist/server.d.ts +17 -252
- package/dist/server.mjs +9997 -3649
- package/dist/{streaming-markdown-CEI9eDZm.d.ts → streaming-markdown-yELbJxyH.d.ts} +49 -55
- package/dist/{streaming-markdown-DRisClXb.d.cts → streaming-markdown-zVwFuaJG.d.cts} +49 -55
- package/dist/streaming-markdown.cjs +11660 -5378
- package/dist/streaming-markdown.d.cts +7 -2
- package/dist/streaming-markdown.d.ts +7 -2
- package/dist/streaming-markdown.mjs +11679 -5347
- package/dist/trusted-mdx/index.cjs +833 -0
- package/dist/trusted-mdx/index.d.cts +11 -0
- package/dist/trusted-mdx/index.d.ts +11 -0
- package/dist/trusted-mdx/index.mjs +807 -0
- package/dist/trusted-mdx/native-children-slot.cjs +45 -0
- package/dist/trusted-mdx/native-children-slot.d.cts +11 -0
- package/dist/trusted-mdx/native-children-slot.d.ts +11 -0
- package/dist/trusted-mdx/native-children-slot.mjs +24 -0
- package/dist/trusted-mdx/server.cjs +752 -0
- package/dist/trusted-mdx/server.d.cts +24 -0
- package/dist/trusted-mdx/server.d.ts +24 -0
- package/dist/trusted-mdx/server.mjs +724 -0
- package/dist/trusted-mdx-contract-ElKpkDI5.d.cts +113 -0
- package/dist/trusted-mdx-contract-ElKpkDI5.d.ts +113 -0
- package/dist/utils/inline-html.cjs +246 -147
- package/dist/utils/inline-html.d.cts +37 -6
- package/dist/utils/inline-html.d.ts +37 -6
- package/dist/utils/inline-html.mjs +237 -134
- package/package.json +41 -18
- package/dist/index-BVhp_pcF.d.ts +0 -251
- package/dist/index-EaeouYp2.d.cts +0 -251
- package/dist/mdx-client.cjs +0 -408
- package/dist/mdx-client.d.cts +0 -113
- package/dist/mdx-client.d.ts +0 -113
- package/dist/mdx-client.mjs +0 -369
- package/dist/mdx-coordinator.cjs +0 -584
- package/dist/mdx-coordinator.d.cts +0 -21
- package/dist/mdx-coordinator.d.ts +0 -21
- package/dist/mdx-coordinator.mjs +0 -550
- package/dist/renderer/node-views.cjs +0 -1887
- package/dist/renderer/node-views.d.cts +0 -14
- package/dist/renderer/node-views.d.ts +0 -14
- package/dist/renderer/node-views.mjs +0 -1861
|
@@ -0,0 +1,833 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
11
|
+
}, __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
|
|
23
|
+
mod
|
|
24
|
+
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
25
|
+
|
|
26
|
+
// src/trusted-mdx/index.ts
|
|
27
|
+
var trusted_mdx_exports = {};
|
|
28
|
+
__export(trusted_mdx_exports, {
|
|
29
|
+
createTrustedMdxEngine: () => createTrustedMdxEngine
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(trusted_mdx_exports);
|
|
32
|
+
|
|
33
|
+
// src/trusted-mdx/engine.ts
|
|
34
|
+
var import_mdx_compile = require("@stream-mdx/worker/mdx-compile");
|
|
35
|
+
|
|
36
|
+
// src/trusted-mdx/evaluate.ts
|
|
37
|
+
var import_react = __toESM(require("react"), 1), jsxRuntime = __toESM(require("react/jsx-runtime"), 1), AsyncFunction = Object.getPrototypeOf(async function() {
|
|
38
|
+
}).constructor;
|
|
39
|
+
async function evaluateCompiledMdx(code, options, canInvoke) {
|
|
40
|
+
let hostImports = options.imports, resolveSyncHostImport = (specifier) => {
|
|
41
|
+
if (specifier === "react")
|
|
42
|
+
return { default: import_react.default, ...import_react.default };
|
|
43
|
+
if (specifier === "react/jsx-runtime")
|
|
44
|
+
return jsxRuntime;
|
|
45
|
+
if (hostImports && Object.prototype.hasOwnProperty.call(hostImports, specifier))
|
|
46
|
+
return hostImports[specifier];
|
|
47
|
+
throw new Error(
|
|
48
|
+
`Cannot resolve MDX import specifier "${specifier}": no host binding configured. Automatic fetching and network egress are disabled.`
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
async function resolveDynamicHostImport(specifier) {
|
|
52
|
+
return resolveSyncHostImport(specifier);
|
|
53
|
+
}
|
|
54
|
+
let runtime = {
|
|
55
|
+
Fragment: jsxRuntime.Fragment,
|
|
56
|
+
jsx: jsxRuntime.jsx,
|
|
57
|
+
jsxs: jsxRuntime.jsxs,
|
|
58
|
+
_resolveSyncHostImport: resolveSyncHostImport,
|
|
59
|
+
_resolveDynamicHostImport: resolveDynamicHostImport
|
|
60
|
+
}, fn = new AsyncFunction(code);
|
|
61
|
+
return canInvoke() ? fn(runtime) : null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/trusted-mdx/projection.ts
|
|
65
|
+
function getActiveTrustedMdxNativeTree(meta) {
|
|
66
|
+
if (Array.isArray(meta?.trustedMdxNativeChildren)) return meta.trustedMdxNativeChildren;
|
|
67
|
+
if (meta?.htmlOwnership === !0 && Array.isArray(meta.htmlTree)) return meta.htmlTree;
|
|
68
|
+
if (Array.isArray(meta?.htmlParagraphTree)) return meta.htmlParagraphTree;
|
|
69
|
+
if (Array.isArray(meta?.htmlTree)) return meta.htmlTree;
|
|
70
|
+
}
|
|
71
|
+
function readSourceRange(value, sourceLength) {
|
|
72
|
+
if (typeof value != "object" || value === null || Array.isArray(value) || !("from" in value) || !("to" in value))
|
|
73
|
+
return null;
|
|
74
|
+
let from = value.from, to = value.to;
|
|
75
|
+
return typeof from != "number" || typeof to != "number" || !Number.isSafeInteger(from) || !Number.isSafeInteger(to) || from < 0 || to <= from || to > sourceLength ? null : { from, to };
|
|
76
|
+
}
|
|
77
|
+
function readSnapshotFields(value) {
|
|
78
|
+
if (typeof value != "object" || value === null || Array.isArray(value) || !("id" in value) || !("type" in value))
|
|
79
|
+
return null;
|
|
80
|
+
let id = value.id, type = value.type;
|
|
81
|
+
return typeof id != "string" || typeof type != "string" ? null : {
|
|
82
|
+
id,
|
|
83
|
+
type,
|
|
84
|
+
range: "range" in value ? value.range : void 0,
|
|
85
|
+
props: "props" in value ? value.props : void 0,
|
|
86
|
+
children: "children" in value ? value.children : void 0
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function hasNativeBlockOwner(tree) {
|
|
90
|
+
if (!Array.isArray(tree)) return !1;
|
|
91
|
+
let pending = [...tree];
|
|
92
|
+
for (; pending.length > 0; ) {
|
|
93
|
+
let current = pending.pop();
|
|
94
|
+
if (!(typeof current != "object" || current === null || Array.isArray(current))) {
|
|
95
|
+
if ("kind" in current && current.kind === "block" && "block" in current) return !0;
|
|
96
|
+
"children" in current && Array.isArray(current.children) && pending.push(...current.children);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return !1;
|
|
100
|
+
}
|
|
101
|
+
function collectInlineOwners(value, sourceLength, matches) {
|
|
102
|
+
if (!Array.isArray(value)) return;
|
|
103
|
+
let visited = /* @__PURE__ */ new WeakSet(), pending = [...value];
|
|
104
|
+
for (; pending.length > 0; ) {
|
|
105
|
+
let current = pending.pop();
|
|
106
|
+
if (!(typeof current != "object" || current === null || visited.has(current))) {
|
|
107
|
+
if (visited.add(current), Array.isArray(current)) {
|
|
108
|
+
pending.push(...current);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
"kind" in current && current.kind === "trusted-mdx-inline" && "ownerId" in current && typeof current.ownerId == "string" && addMdxOwner(
|
|
112
|
+
matches,
|
|
113
|
+
current.ownerId,
|
|
114
|
+
"inline expression",
|
|
115
|
+
"sourceRange" in current ? current.sourceRange : void 0,
|
|
116
|
+
sourceLength
|
|
117
|
+
), "children" in current && Array.isArray(current.children) && pending.push(...current.children);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function isNestedMdxType(type) {
|
|
122
|
+
return type === "paragraph-mdx" || type === "blockquote-mdx" || type === "list-item-mdx";
|
|
123
|
+
}
|
|
124
|
+
function addMdxOwner(matches, id, label, range, sourceLength) {
|
|
125
|
+
let sourceRange = readSourceRange(range, sourceLength);
|
|
126
|
+
matches.push({
|
|
127
|
+
id,
|
|
128
|
+
label,
|
|
129
|
+
from: sourceRange?.from ?? null,
|
|
130
|
+
to: sourceRange?.to ?? null,
|
|
131
|
+
regions: [],
|
|
132
|
+
placement: null,
|
|
133
|
+
rootElementName: void 0,
|
|
134
|
+
nativeChildren: !1,
|
|
135
|
+
nativeChildOwners: [],
|
|
136
|
+
flowChildren: void 0,
|
|
137
|
+
nativeFlowOwnedBlockIds: [],
|
|
138
|
+
wholeBlockFlowRoot: !1,
|
|
139
|
+
ambiguous: !1
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
function readFlowChildRanges(flowChildren, flowRange, sourceLength) {
|
|
143
|
+
if (flowChildren === void 0) return;
|
|
144
|
+
let ranges = [], previousTo = flowRange.from;
|
|
145
|
+
for (let child of flowChildren) {
|
|
146
|
+
let range = readSourceRange(child, sourceLength);
|
|
147
|
+
if (!range || range.from < flowRange.from || range.to > flowRange.to || range.from < previousTo)
|
|
148
|
+
return null;
|
|
149
|
+
ranges.push(range), previousTo = range.to;
|
|
150
|
+
}
|
|
151
|
+
return ranges;
|
|
152
|
+
}
|
|
153
|
+
function classifyFlowRange(range, childRanges) {
|
|
154
|
+
let directChildIndex;
|
|
155
|
+
for (let index = 0; index < childRanges.length; index++) {
|
|
156
|
+
let childRange = childRanges[index];
|
|
157
|
+
if (!childRange) return null;
|
|
158
|
+
if (range.from >= childRange.from && range.to <= childRange.to) {
|
|
159
|
+
if (directChildIndex !== void 0) return null;
|
|
160
|
+
directChildIndex = index;
|
|
161
|
+
} else if (range.from < childRange.to && range.to > childRange.from)
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
return directChildIndex ?? "root-syntax";
|
|
165
|
+
}
|
|
166
|
+
function findInlineChildRunForRange(range, flowChildren, childRanges) {
|
|
167
|
+
if (!(flowChildren === void 0 || childRanges === void 0 || range.to <= range.from))
|
|
168
|
+
for (let startIndex = 0; startIndex < flowChildren.length; startIndex++) {
|
|
169
|
+
let firstChild = flowChildren[startIndex], firstRange = childRanges[startIndex];
|
|
170
|
+
if (firstChild?.kind !== "inline" || !firstRange || firstRange.from !== range.from || firstRange.to <= firstRange.from) continue;
|
|
171
|
+
let previousRange = firstRange;
|
|
172
|
+
for (let endIndex = startIndex + 1; endIndex < flowChildren.length; endIndex++) {
|
|
173
|
+
let child = flowChildren[endIndex], childRange = childRanges[endIndex];
|
|
174
|
+
if (child?.kind !== "inline" || !childRange || childRange.from !== previousRange.to || childRange.to <= childRange.from) break;
|
|
175
|
+
if (previousRange = childRange, previousRange.to === range.to)
|
|
176
|
+
return {
|
|
177
|
+
startIndex,
|
|
178
|
+
endIndex
|
|
179
|
+
};
|
|
180
|
+
if (previousRange.to > range.to) break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function collectFlowOwnerBlocks(blocks) {
|
|
185
|
+
let flowOwnerBlocks = [], seenBlockIds = /* @__PURE__ */ new Set(), pendingBlocks = [...blocks], pendingNodes = [], visitedNodes = /* @__PURE__ */ new WeakSet();
|
|
186
|
+
for (let blockIndex = 0; blockIndex < pendingBlocks.length; blockIndex++) {
|
|
187
|
+
let block = pendingBlocks[blockIndex];
|
|
188
|
+
if (!block || seenBlockIds.has(block.id)) continue;
|
|
189
|
+
seenBlockIds.add(block.id), flowOwnerBlocks.push(block);
|
|
190
|
+
let meta = block.payload.meta, preparedTree = getActiveTrustedMdxNativeTree(meta);
|
|
191
|
+
for (preparedTree !== void 0 && pendingNodes.push(...preparedTree); pendingNodes.length > 0; ) {
|
|
192
|
+
let node = pendingNodes.pop();
|
|
193
|
+
!node || visitedNodes.has(node) || (visitedNodes.add(node), node.kind === "block" ? pendingBlocks.push(node.block) : (node.kind === "element" || node.kind === "component") && pendingNodes.push(...node.children));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return flowOwnerBlocks;
|
|
197
|
+
}
|
|
198
|
+
function hasWholeFlowRootMatch(block, childRange, matches) {
|
|
199
|
+
return matches.some(
|
|
200
|
+
(match) => match.wholeBlockFlowRoot && match.from === childRange.from && match.to === childRange.to && (match.id === block.id || block.payload.inline?.some(
|
|
201
|
+
(node) => node.kind === "trusted-mdx-inline" && node.ownerId === match.id && node.sourceRange?.from === childRange.from && node.sourceRange?.to === childRange.to
|
|
202
|
+
) === !0)
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
function findPreparedTreeBlock(tree, childRange, sourceLength, exact = !1, wholeFlowRoots) {
|
|
206
|
+
if (!tree) return;
|
|
207
|
+
let pending = [...tree], owner, ownerWidth = -1;
|
|
208
|
+
for (; pending.length > 0; ) {
|
|
209
|
+
let node = pending.pop();
|
|
210
|
+
if (node)
|
|
211
|
+
if (node.kind === "block") {
|
|
212
|
+
let range = readSourceRange(node.block.payload.range, sourceLength);
|
|
213
|
+
if (!range) continue;
|
|
214
|
+
let wholeFlowRoot = wholeFlowRoots !== void 0 && hasWholeFlowRootMatch(node.block, childRange, wholeFlowRoots) && range.from === childRange.from && range.to <= childRange.to;
|
|
215
|
+
(exact ? range.from === childRange.from && range.to === childRange.to || wholeFlowRoot : range.from >= childRange.from && range.to <= childRange.to) && (wholeFlowRoots === void 0 || node.block.type === "mdx" || wholeFlowRoot) && range.to - range.from > ownerWidth && (owner = node.block, ownerWidth = range.to - range.from);
|
|
216
|
+
} else (node.kind === "element" || node.kind === "component") && pending.push(...node.children);
|
|
217
|
+
}
|
|
218
|
+
return owner;
|
|
219
|
+
}
|
|
220
|
+
function assertPreparedTreeOwnersFitFlowChildren(tree, childRanges, flowChildren, flowRange, sourceLength) {
|
|
221
|
+
if (!tree) return;
|
|
222
|
+
let pending = [...tree];
|
|
223
|
+
for (; pending.length > 0; ) {
|
|
224
|
+
let node = pending.pop();
|
|
225
|
+
if (node)
|
|
226
|
+
if (node.kind === "block") {
|
|
227
|
+
let range = readSourceRange(node.block.payload.range, sourceLength);
|
|
228
|
+
if (!range)
|
|
229
|
+
throw new Error("A trusted MDX flow region has a prepared worker block without a source span.");
|
|
230
|
+
if (range.from < flowRange.from || range.to > flowRange.to || range.from === flowRange.from && range.to === flowRange.to) continue;
|
|
231
|
+
if (childRanges === void 0)
|
|
232
|
+
throw new Error("A trusted MDX flow region has prepared tree owners but no direct-child spans.");
|
|
233
|
+
let inlineRun = node.block.type === "paragraph" && Array.isArray(node.block.payload.inline) && node.block.payload.inline.length > 0 ? findInlineChildRunForRange(range, flowChildren, childRanges) : void 0;
|
|
234
|
+
if (classifyFlowRange(range, childRanges) === null && inlineRun === void 0)
|
|
235
|
+
throw new Error("A trusted MDX flow region has prepared tree owners crossing direct-child boundaries.");
|
|
236
|
+
} else (node.kind === "element" || node.kind === "component") && pending.push(...node.children);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function collectFlowNativeBlocks(flowChildren, flowRange, blocks, rootOwnerId, sourceLength, matches, flowOwnerBlocks) {
|
|
240
|
+
let childRanges = readFlowChildRanges(flowChildren, flowRange, sourceLength);
|
|
241
|
+
if (childRanges === null)
|
|
242
|
+
throw new Error("A trusted MDX flow region has invalid or overlapping direct-child spans.");
|
|
243
|
+
let meta = flowOwnerBlocks.find((block) => block.id === rootOwnerId)?.payload.meta, preparedTree = getActiveTrustedMdxNativeTree(meta), preparedTrees = preparedTree === void 0 ? [] : [preparedTree], ownedBlockIds = [];
|
|
244
|
+
for (let block of blocks) {
|
|
245
|
+
if (block.id === rootOwnerId) continue;
|
|
246
|
+
let blockRange = readSourceRange(block.payload.range, sourceLength);
|
|
247
|
+
if (!blockRange || blockRange.from < flowRange.from || blockRange.to > flowRange.to || blockRange.from === flowRange.from && blockRange.to === flowRange.to) continue;
|
|
248
|
+
if (childRanges === void 0)
|
|
249
|
+
throw new Error("A trusted MDX flow region has worker owners but no direct-child spans.");
|
|
250
|
+
let ownership = classifyFlowRange(blockRange, childRanges), inlineRun = block.type === "paragraph" && Array.isArray(block.payload.inline) && block.payload.inline.length > 0 ? findInlineChildRunForRange(blockRange, flowChildren, childRanges) : void 0;
|
|
251
|
+
if (ownership === null && inlineRun === void 0)
|
|
252
|
+
throw new Error("A trusted MDX flow region has worker owners crossing direct-child boundaries.");
|
|
253
|
+
ownedBlockIds.push(block.id);
|
|
254
|
+
}
|
|
255
|
+
for (let tree of preparedTrees)
|
|
256
|
+
hasNativeBlockOwner(tree) && assertPreparedTreeOwnersFitFlowChildren(tree, childRanges, flowChildren, flowRange, sourceLength);
|
|
257
|
+
if (flowChildren === void 0 || flowChildren.length === 0)
|
|
258
|
+
return { childOwners: [], ownedBlockIds };
|
|
259
|
+
if (childRanges === void 0)
|
|
260
|
+
throw new Error("A trusted MDX flow region has native children but no direct-child spans.");
|
|
261
|
+
let groupedInlineOwners, registerGroupedInlineOwner = (block, kind) => {
|
|
262
|
+
if (block.id === rootOwnerId || block.type !== "paragraph" || !Array.isArray(block.payload.inline) || block.payload.inline.length === 0)
|
|
263
|
+
return;
|
|
264
|
+
let range = readSourceRange(block.payload.range, sourceLength);
|
|
265
|
+
if (!range) return;
|
|
266
|
+
let run = findInlineChildRunForRange(range, flowChildren, childRanges);
|
|
267
|
+
if (!run) return;
|
|
268
|
+
let groupedOwners = groupedInlineOwners ?? (groupedInlineOwners = new Array(flowChildren.length));
|
|
269
|
+
for (let index = run.startIndex; index <= run.endIndex; index++) {
|
|
270
|
+
let existing2 = groupedOwners[index];
|
|
271
|
+
if (existing2 && (existing2.block.id !== block.id || existing2.startIndex !== run.startIndex || existing2.endIndex !== run.endIndex))
|
|
272
|
+
throw new Error("A trusted MDX flow region has duplicate native owners for one inline child run.");
|
|
273
|
+
}
|
|
274
|
+
if (groupedOwners[run.startIndex]?.owner.kind === "block" && kind === "prepared-tree") return;
|
|
275
|
+
let candidate = {
|
|
276
|
+
startIndex: run.startIndex,
|
|
277
|
+
endIndex: run.endIndex,
|
|
278
|
+
block,
|
|
279
|
+
owner: kind === "block" ? { kind: "block", blockId: block.id } : { kind: "prepared-tree", blockId: block.id }
|
|
280
|
+
};
|
|
281
|
+
for (let index = run.startIndex; index <= run.endIndex; index++)
|
|
282
|
+
groupedOwners[index] = candidate;
|
|
283
|
+
};
|
|
284
|
+
for (let block of blocks)
|
|
285
|
+
registerGroupedInlineOwner(block, "block");
|
|
286
|
+
for (let tree of preparedTrees) {
|
|
287
|
+
let pending = [...tree], visited = /* @__PURE__ */ new WeakSet();
|
|
288
|
+
for (; pending.length > 0; ) {
|
|
289
|
+
let node = pending.pop();
|
|
290
|
+
!node || visited.has(node) || (visited.add(node), node.kind === "block" ? registerGroupedInlineOwner(node.block, "prepared-tree") : (node.kind === "element" || node.kind === "component") && pending.push(...node.children));
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
let groupedChildRanges;
|
|
294
|
+
for (let startIndex = 0; startIndex < (groupedInlineOwners?.length ?? 0); startIndex++) {
|
|
295
|
+
let groupedOwner = groupedInlineOwners?.[startIndex];
|
|
296
|
+
if (!(!groupedOwner || groupedOwner.startIndex !== startIndex))
|
|
297
|
+
for (let childIndex = groupedOwner.startIndex; childIndex <= groupedOwner.endIndex; childIndex++) {
|
|
298
|
+
let childRange = childRanges[childIndex];
|
|
299
|
+
if (!childRange)
|
|
300
|
+
throw new Error("A trusted MDX flow region has an invalid grouped inline child span.");
|
|
301
|
+
groupedChildRanges ?? (groupedChildRanges = /* @__PURE__ */ new Map()), groupedChildRanges.set(childRange.from, childRange.to);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (groupedChildRanges !== void 0) {
|
|
305
|
+
for (let block of blocks) {
|
|
306
|
+
if (block.id === rootOwnerId) continue;
|
|
307
|
+
let blockRange = readSourceRange(block.payload.range, sourceLength);
|
|
308
|
+
if (blockRange && groupedChildRanges.get(blockRange.from) === blockRange.to)
|
|
309
|
+
throw new Error("A trusted MDX flow region has duplicate native owners inside a grouped inline run.");
|
|
310
|
+
}
|
|
311
|
+
for (let tree of preparedTrees) {
|
|
312
|
+
let pending = [...tree], visited = /* @__PURE__ */ new WeakSet();
|
|
313
|
+
for (; pending.length > 0; ) {
|
|
314
|
+
let node = pending.pop();
|
|
315
|
+
if (!(!node || visited.has(node)))
|
|
316
|
+
if (visited.add(node), node.kind === "block") {
|
|
317
|
+
let blockRange = readSourceRange(node.block.payload.range, sourceLength);
|
|
318
|
+
if (blockRange && groupedChildRanges.get(blockRange.from) === blockRange.to)
|
|
319
|
+
throw new Error("A trusted MDX flow region has duplicate native owners inside a grouped inline run.");
|
|
320
|
+
} else (node.kind === "element" || node.kind === "component") && pending.push(...node.children);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
let childOwners = [];
|
|
325
|
+
for (let childIndex = 0; childIndex < flowChildren.length; childIndex++) {
|
|
326
|
+
let child = flowChildren[childIndex], childRange = childRanges?.[childIndex];
|
|
327
|
+
if (!child || !childRange)
|
|
328
|
+
throw new Error("A trusted MDX flow region has invalid direct-child spans.");
|
|
329
|
+
let groupedOwner = groupedInlineOwners?.[childIndex];
|
|
330
|
+
if (groupedOwner) {
|
|
331
|
+
if (groupedOwner.startIndex !== childIndex || child.kind !== "inline")
|
|
332
|
+
throw new Error("A trusted MDX flow region has an invalid grouped inline native owner.");
|
|
333
|
+
childOwners.push(groupedOwner.owner);
|
|
334
|
+
for (let coveredIndex = childIndex + 1; coveredIndex <= groupedOwner.endIndex; coveredIndex++)
|
|
335
|
+
childOwners.push({ kind: "continuation", ownerIndex: childIndex });
|
|
336
|
+
childIndex = groupedOwner.endIndex;
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
let ownerId, ownerWidth = -1;
|
|
340
|
+
if (child.kind !== "expression")
|
|
341
|
+
for (let block of blocks) {
|
|
342
|
+
if (block.id === rootOwnerId) continue;
|
|
343
|
+
let blockRange = readSourceRange(block.payload.range, sourceLength);
|
|
344
|
+
if (!blockRange || blockRange.from === flowRange.from && blockRange.to === flowRange.to || !(child.kind === "markdown" ? blockRange.from >= childRange.from && blockRange.to <= childRange.to : child.kind === "inline" || block.type === "mdx" ? blockRange.from === childRange.from && blockRange.to === childRange.to : blockRange.from === childRange.from && hasWholeFlowRootMatch(block, childRange, matches))) continue;
|
|
345
|
+
let width = blockRange.to - blockRange.from;
|
|
346
|
+
width > ownerWidth && (ownerId = block.id, ownerWidth = width);
|
|
347
|
+
}
|
|
348
|
+
if (ownerId !== void 0) {
|
|
349
|
+
childOwners.push({ kind: "block", blockId: ownerId });
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (child.kind !== "expression") {
|
|
353
|
+
let preparedOwner;
|
|
354
|
+
for (let tree of preparedTrees)
|
|
355
|
+
if (preparedOwner = findPreparedTreeBlock(
|
|
356
|
+
tree,
|
|
357
|
+
childRange,
|
|
358
|
+
sourceLength,
|
|
359
|
+
child.kind === "jsx",
|
|
360
|
+
child.kind === "jsx" ? matches : void 0
|
|
361
|
+
), preparedOwner) break;
|
|
362
|
+
if (preparedOwner) {
|
|
363
|
+
if (child.kind === "inline") {
|
|
364
|
+
let preparedRange = readSourceRange(preparedOwner.payload.range, sourceLength);
|
|
365
|
+
if (preparedRange?.from !== childRange.from || preparedRange.to !== childRange.to)
|
|
366
|
+
throw new Error("A compiler-authored inline child has no exact prepared native owner.");
|
|
367
|
+
}
|
|
368
|
+
childOwners.push({ kind: "prepared-tree", blockId: preparedOwner.id });
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (child.kind === "markdown" || child.kind === "inline")
|
|
373
|
+
throw new Error("A compiler-authored Markdown child has no prepared native block owner.");
|
|
374
|
+
childOwners.push(null);
|
|
375
|
+
}
|
|
376
|
+
return { childOwners, ownedBlockIds };
|
|
377
|
+
}
|
|
378
|
+
function collectCompilerFlowBlockOwners(source, blocks, regions, matches) {
|
|
379
|
+
let ownerIds = new Set(matches.map((match) => match.id));
|
|
380
|
+
for (let region of regions) {
|
|
381
|
+
if (region.kind !== "render" || region.placement !== "flow" || !("rootElementName" in region)) continue;
|
|
382
|
+
let range = readSourceRange(region, source.length);
|
|
383
|
+
if (!range) continue;
|
|
384
|
+
let exactMatches = matches.filter((match) => match.from === range.from && match.to === range.to);
|
|
385
|
+
if (exactMatches.length > 0) {
|
|
386
|
+
for (let match of exactMatches) match.wholeBlockFlowRoot = !0;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
let block = blocks.find((candidate) => {
|
|
390
|
+
let blockRange = readSourceRange(candidate.payload.range, source.length);
|
|
391
|
+
return blockRange !== null && blockRange.from === range.from && blockRange.to <= range.to;
|
|
392
|
+
});
|
|
393
|
+
!block || ownerIds.has(block.id) || (addMdxOwner(matches, block.id, `${block.type} compiler flow root`, range, source.length), matches[matches.length - 1].wholeBlockFlowRoot = !0, ownerIds.add(block.id));
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function collectPreparedNestedOwners(prepared, sourceLength, matches) {
|
|
397
|
+
if (typeof prepared != "object" || prepared === null || Array.isArray(prepared) || !("children" in prepared) || !Array.isArray(prepared.children))
|
|
398
|
+
return;
|
|
399
|
+
let visited = /* @__PURE__ */ new WeakSet(), pending = [];
|
|
400
|
+
for (let index = 0; index < prepared.children.length; index++) {
|
|
401
|
+
let child = prepared.children[index];
|
|
402
|
+
pending.push(child);
|
|
403
|
+
}
|
|
404
|
+
for (; pending.length > 0; ) {
|
|
405
|
+
let value = pending.pop();
|
|
406
|
+
if (typeof value != "object" || value === null || Array.isArray(value) || visited.has(value))
|
|
407
|
+
continue;
|
|
408
|
+
visited.add(value);
|
|
409
|
+
let node = readSnapshotFields(value), nodeProps = node?.props && typeof node.props == "object" ? node.props : void 0;
|
|
410
|
+
if (nodeProps && "inline" in nodeProps && collectInlineOwners(nodeProps.inline, sourceLength, matches), node && isNestedMdxType(node.type) && addMdxOwner(matches, node.id, `${node.type} segment`, node.range, sourceLength), !(!node || !Array.isArray(node.children)))
|
|
411
|
+
for (let index = 0; index < node.children.length; index++) {
|
|
412
|
+
let child = node.children[index];
|
|
413
|
+
pending.push(child);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
function collectHtmlTreeOwners(tree, sourceLength, matches) {
|
|
418
|
+
if (!Array.isArray(tree)) return;
|
|
419
|
+
let pending = [...tree], visited = /* @__PURE__ */ new WeakSet();
|
|
420
|
+
for (; pending.length > 0; ) {
|
|
421
|
+
let current = pending.pop();
|
|
422
|
+
if (!(typeof current != "object" || current === null || visited.has(current))) {
|
|
423
|
+
if (visited.add(current), Array.isArray(current)) {
|
|
424
|
+
pending.push(...current);
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if ("kind" in current && current.kind === "block" && "block" in current) {
|
|
428
|
+
let candidate = current.block;
|
|
429
|
+
if (typeof candidate == "object" && candidate !== null && !Array.isArray(candidate) && "id" in candidate && typeof candidate.id == "string" && "type" in candidate && typeof candidate.type == "string" && "payload" in candidate && typeof candidate.payload == "object" && candidate.payload !== null) {
|
|
430
|
+
let block = candidate, meta = block.payload.meta;
|
|
431
|
+
if (block.type === "mdx" && addMdxOwner(
|
|
432
|
+
matches,
|
|
433
|
+
block.id,
|
|
434
|
+
"nested block",
|
|
435
|
+
block.payload.range,
|
|
436
|
+
sourceLength
|
|
437
|
+
), collectInlineOwners(block.payload.inline, sourceLength, matches), meta) {
|
|
438
|
+
if (block.type === "paragraph" && Array.isArray(meta.mixedSegments))
|
|
439
|
+
for (let index = 0; index < meta.mixedSegments.length; index++) {
|
|
440
|
+
let segment = meta.mixedSegments[index];
|
|
441
|
+
typeof segment != "object" || segment === null || Array.isArray(segment) || !("kind" in segment) || segment.kind !== "mdx" || addMdxOwner(
|
|
442
|
+
matches,
|
|
443
|
+
`${block.id}::paragraph:segment:${index}`,
|
|
444
|
+
"paragraph segment",
|
|
445
|
+
"range" in segment ? segment.range : void 0,
|
|
446
|
+
sourceLength
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
collectInlineOwners(meta.header, sourceLength, matches), collectInlineOwners(meta.rows, sourceLength, matches), collectPreparedNestedOwners(meta.preparedNested, sourceLength, matches);
|
|
450
|
+
let preparedTree = getActiveTrustedMdxNativeTree(meta);
|
|
451
|
+
preparedTree !== void 0 && pending.push(preparedTree);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
"children" in current && Array.isArray(current.children) && pending.push(...current.children);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
function collectMdxOwners(source, blocks) {
|
|
460
|
+
let matches = [];
|
|
461
|
+
for (let block of blocks) {
|
|
462
|
+
if (block.type === "mdx" && addMdxOwner(
|
|
463
|
+
matches,
|
|
464
|
+
block.id,
|
|
465
|
+
"block",
|
|
466
|
+
block.payload.range,
|
|
467
|
+
source.length
|
|
468
|
+
), block.type === "paragraph") {
|
|
469
|
+
let segments = block.payload.meta?.mixedSegments;
|
|
470
|
+
if (Array.isArray(segments))
|
|
471
|
+
for (let index = 0; index < segments.length; index++) {
|
|
472
|
+
let segment = segments[index];
|
|
473
|
+
typeof segment != "object" || segment === null || Array.isArray(segment) || !("kind" in segment) || segment.kind !== "mdx" || addMdxOwner(
|
|
474
|
+
matches,
|
|
475
|
+
`${block.id}::paragraph:segment:${index}`,
|
|
476
|
+
"paragraph segment",
|
|
477
|
+
"range" in segment ? segment.range : void 0,
|
|
478
|
+
source.length
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
collectInlineOwners(block.payload.inline, source.length, matches);
|
|
483
|
+
let tableMeta = block.payload.meta;
|
|
484
|
+
if (tableMeta) {
|
|
485
|
+
collectInlineOwners(tableMeta.header, source.length, matches), collectInlineOwners(tableMeta.rows, source.length, matches), collectPreparedNestedOwners(tableMeta.preparedNested, source.length, matches);
|
|
486
|
+
let preparedTree = getActiveTrustedMdxNativeTree(tableMeta);
|
|
487
|
+
preparedTree !== void 0 && collectHtmlTreeOwners(preparedTree, source.length, matches);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
let unique = /* @__PURE__ */ new Map();
|
|
491
|
+
for (let match of matches) {
|
|
492
|
+
let existing = unique.get(match.id);
|
|
493
|
+
existing ? (existing.from !== match.from || existing.to !== match.to) && (existing.ambiguous = !0) : unique.set(match.id, match);
|
|
494
|
+
}
|
|
495
|
+
return Array.from(unique.values());
|
|
496
|
+
}
|
|
497
|
+
function describeOwner(match) {
|
|
498
|
+
return match.label === "block" ? `MDX block "${match.id}"` : `MDX ${match.label} "${match.id}"`;
|
|
499
|
+
}
|
|
500
|
+
function isExactNativeMdxAliasRegion(region, owners) {
|
|
501
|
+
if (owners.length !== 2) return !1;
|
|
502
|
+
let first = owners[0], second = owners[1];
|
|
503
|
+
return !first || !second || first.from === null || first.to === null || first.from !== second.from || first.to !== second.to || region.from !== first.from || region.to !== first.to ? !1 : (first.label === "block" || first.label === "nested block") && second.label === "inline expression" && second.id.startsWith(`${first.id}::native-mdx:`) || (second.label === "block" || second.label === "nested block") && first.label === "inline expression" && first.id.startsWith(`${second.id}::native-mdx:`);
|
|
504
|
+
}
|
|
505
|
+
function placementFields(match) {
|
|
506
|
+
return match.placement === null ? {} : { placement: match.placement };
|
|
507
|
+
}
|
|
508
|
+
function projectMdxBlocks(source, blocks, regions, regionComponents) {
|
|
509
|
+
let projections = /* @__PURE__ */ new Map(), flowOwnerBlocks = collectFlowOwnerBlocks(blocks), matches = collectMdxOwners(source, blocks);
|
|
510
|
+
collectCompilerFlowBlockOwners(source, flowOwnerBlocks, regions, matches);
|
|
511
|
+
let ownersByRegion = /* @__PURE__ */ new Map();
|
|
512
|
+
for (let region of regions) {
|
|
513
|
+
let regionRange = readSourceRange(region, source.length);
|
|
514
|
+
if (!regionRange) continue;
|
|
515
|
+
let owners = [];
|
|
516
|
+
for (let match of matches) {
|
|
517
|
+
if (match.from === null || match.to === null) continue;
|
|
518
|
+
let ownerFrom = match.from, ownerTo = match.to;
|
|
519
|
+
if (regionRange.to <= match.from || regionRange.from >= match.to) continue;
|
|
520
|
+
let compilerFlowRoot = region.kind === "render" && "rootElementName" in region, regionContainsOwner = regionRange.from <= match.from && regionRange.to >= match.to;
|
|
521
|
+
if (region.kind !== "module" && (regionRange.from < match.from || regionRange.to > match.to)) {
|
|
522
|
+
if (compilerFlowRoot && regionContainsOwner) continue;
|
|
523
|
+
match.ambiguous = !0;
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
if (!(match.wholeBlockFlowRoot && regionRange.from >= ownerFrom && regionRange.to <= ownerTo && (regionRange.from > ownerFrom || regionRange.to < ownerTo)) && (match.regions.push(region), region.kind !== "module")) {
|
|
527
|
+
if (owners.push(match), region.placement !== "flow" && region.placement !== "inline" || match.placement !== null && match.placement !== region.placement ? match.ambiguous = !0 : match.placement = region.placement, region.kind === "render" && region.placement === "flow" && "rootElementName" in region && regionRange.from === match.from && regionRange.to === match.to && region.kind === "render" && "rootElementName" in region) {
|
|
528
|
+
match.wholeBlockFlowRoot = !0;
|
|
529
|
+
let flowNativeBlocks = collectFlowNativeBlocks(
|
|
530
|
+
region.flowChildren,
|
|
531
|
+
regionRange,
|
|
532
|
+
blocks,
|
|
533
|
+
match.id,
|
|
534
|
+
source.length,
|
|
535
|
+
matches,
|
|
536
|
+
flowOwnerBlocks
|
|
537
|
+
);
|
|
538
|
+
match.nativeFlowOwnedBlockIds = flowNativeBlocks.ownedBlockIds, match.nativeChildOwners = flowNativeBlocks.childOwners, match.flowChildren = region.flowChildren;
|
|
539
|
+
}
|
|
540
|
+
match.nativeChildren = match.nativeChildOwners.some((owner) => owner !== null) && match.placement === "flow", "rootElementName" in region && (match.rootElementName !== void 0 && match.rootElementName !== region.rootElementName ? match.ambiguous = !0 : match.rootElementName = region.rootElementName);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
owners.length > 1 && ownersByRegion.set(region, owners);
|
|
544
|
+
}
|
|
545
|
+
for (let [region, owners] of ownersByRegion)
|
|
546
|
+
if (!isExactNativeMdxAliasRegion(region, owners))
|
|
547
|
+
for (let owner of owners) owner.ambiguous = !0;
|
|
548
|
+
for (let match of matches) {
|
|
549
|
+
let description = describeOwner(match);
|
|
550
|
+
if (match.from === null || match.to === null) {
|
|
551
|
+
projections.set(match.id, {
|
|
552
|
+
kind: "unmapped",
|
|
553
|
+
diagnostic: `${description} has no valid authored source range.`
|
|
554
|
+
});
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
if (!match.ambiguous && match.regions.length > 0) {
|
|
558
|
+
let orderedRegions = match.regions.slice().sort((left, right) => left.from - right.from || left.to - right.to), coveredUntil = match.from;
|
|
559
|
+
for (let region of orderedRegions) {
|
|
560
|
+
let from = Math.max(match.from, region.from), to = Math.min(match.to, region.to);
|
|
561
|
+
if (from < coveredUntil || source.slice(coveredUntil, from).trim().length > 0) {
|
|
562
|
+
match.ambiguous = !0;
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
coveredUntil = to;
|
|
566
|
+
}
|
|
567
|
+
!match.ambiguous && source.slice(coveredUntil, match.to).trim().length > 0 && (match.ambiguous = !0);
|
|
568
|
+
}
|
|
569
|
+
if (match.ambiguous || match.regions.length === 0) {
|
|
570
|
+
projections.set(match.id, {
|
|
571
|
+
kind: "unmapped",
|
|
572
|
+
diagnostic: match.ambiguous ? `${description} cannot be mapped exactly to unique authored regions.` : `${description} (${match.from}..${match.to}) has no corresponding authored MDX region.`,
|
|
573
|
+
...placementFields(match)
|
|
574
|
+
});
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
let renderRegions = match.regions.filter((region) => region.kind === "render");
|
|
578
|
+
if (renderRegions.length > 0) {
|
|
579
|
+
let placement = match.placement;
|
|
580
|
+
if (placement === null) {
|
|
581
|
+
projections.set(match.id, {
|
|
582
|
+
kind: "unmapped",
|
|
583
|
+
diagnostic: `${description} has render regions without compiler placement provenance.`
|
|
584
|
+
});
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
if (match.nativeChildren && match.rootElementName === void 0) {
|
|
588
|
+
projections.set(match.id, {
|
|
589
|
+
kind: "unmapped",
|
|
590
|
+
diagnostic: `${description} has prepared native children but no compiler-authored flow JSX root.`,
|
|
591
|
+
placement
|
|
592
|
+
});
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
let components = [], missingKey = null;
|
|
596
|
+
for (let region of renderRegions) {
|
|
597
|
+
if (!Object.prototype.hasOwnProperty.call(regionComponents, region.key)) {
|
|
598
|
+
missingKey = region.key;
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
let component = regionComponents[region.key];
|
|
602
|
+
if (typeof component != "function") {
|
|
603
|
+
missingKey = region.key;
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
components.push(component);
|
|
607
|
+
}
|
|
608
|
+
projections.set(
|
|
609
|
+
match.id,
|
|
610
|
+
missingKey === null ? {
|
|
611
|
+
kind: "render",
|
|
612
|
+
regions: Object.freeze(components),
|
|
613
|
+
placement,
|
|
614
|
+
...match.nativeChildren ? { nativeChildren: !0 } : {},
|
|
615
|
+
...match.nativeChildOwners.length > 0 ? { nativeChildOwners: Object.freeze([...match.nativeChildOwners]) } : {},
|
|
616
|
+
...match.flowChildren ? { flowChildren: match.flowChildren } : {},
|
|
617
|
+
...match.nativeFlowOwnedBlockIds.length > 0 ? { nativeFlowOwnedBlockIds: Object.freeze([...match.nativeFlowOwnedBlockIds]) } : {},
|
|
618
|
+
...match.wholeBlockFlowRoot ? { wholeBlockFlowRoot: !0 } : {},
|
|
619
|
+
...match.rootElementName === void 0 ? {} : { rootElementName: match.rootElementName }
|
|
620
|
+
} : {
|
|
621
|
+
kind: "unmapped",
|
|
622
|
+
diagnostic: `No evaluated renderer exists for MDX region "${missingKey}".`,
|
|
623
|
+
placement
|
|
624
|
+
}
|
|
625
|
+
);
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
if (match.regions.some((region) => region.kind === "module")) {
|
|
629
|
+
projections.set(match.id, { kind: "module" });
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
projections.set(match.id, {
|
|
633
|
+
kind: "unmapped",
|
|
634
|
+
diagnostic: `${description} has no renderable or module region.`
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
let moduleRanges = [];
|
|
638
|
+
for (let region of regions) {
|
|
639
|
+
if (region.kind !== "module") continue;
|
|
640
|
+
let range = readSourceRange(region, source.length);
|
|
641
|
+
range && moduleRanges.push(range);
|
|
642
|
+
}
|
|
643
|
+
for (let block of blocks) {
|
|
644
|
+
let range = readSourceRange(block.payload.range, source.length);
|
|
645
|
+
range && moduleRanges.some((module2) => module2.from <= range.from && module2.to >= range.to) && projections.set(block.id, { kind: "module" });
|
|
646
|
+
}
|
|
647
|
+
let nativeChildIds = /* @__PURE__ */ new Set();
|
|
648
|
+
for (let projection of projections.values())
|
|
649
|
+
if (projection.kind === "render")
|
|
650
|
+
for (let id of projection.nativeFlowOwnedBlockIds ?? []) nativeChildIds.add(id);
|
|
651
|
+
for (let id of nativeChildIds) {
|
|
652
|
+
let projection = projections.get(id);
|
|
653
|
+
projection ? projections.set(id, { ...projection, nativeFlowParent: !0 }) : projections.set(id, { kind: "native-child", nativeFlowParent: !0 });
|
|
654
|
+
}
|
|
655
|
+
return Object.freeze(Object.fromEntries(projections));
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// src/trusted-mdx/document.ts
|
|
659
|
+
var EMPTY_PROJECTIONS = Object.freeze({});
|
|
660
|
+
function createTrustedMdxDocumentLease(context) {
|
|
661
|
+
let { options, getFactoryEpoch, getOrCompileArtifact, onDispose } = context, leaseEpoch = 0, isDisposed = !1, requestedRevision = 0, projectionRevision = null, renderedRevision = null, status = "idle", error = null, components = Object.freeze({
|
|
662
|
+
...options.components
|
|
663
|
+
}), layout = null, projections = EMPTY_PROJECTIONS, candidateRevision = null, candidateProjections = null, candidateLayout = null, committedRevision = null, committedProjections = null, committedLayout = null, listeners = /* @__PURE__ */ new Set(), createSnapshot = () => Object.freeze({
|
|
664
|
+
requestedRevision,
|
|
665
|
+
projectionRevision,
|
|
666
|
+
renderedRevision,
|
|
667
|
+
status,
|
|
668
|
+
error,
|
|
669
|
+
components,
|
|
670
|
+
layout,
|
|
671
|
+
projections
|
|
672
|
+
}), currentSnapshot = createSnapshot(), publishIfChanged = () => {
|
|
673
|
+
let previous = currentSnapshot;
|
|
674
|
+
if (!(previous.requestedRevision === requestedRevision && previous.projectionRevision === projectionRevision && previous.renderedRevision === renderedRevision && previous.status === status && previous.error === error && previous.components === components && previous.layout === layout && previous.projections === projections)) {
|
|
675
|
+
currentSnapshot = createSnapshot();
|
|
676
|
+
for (let listener of listeners) listener();
|
|
677
|
+
}
|
|
678
|
+
}, getSnapshot = () => currentSnapshot, subscribe = (listener) => (isDisposed || listeners.add(listener), () => {
|
|
679
|
+
listeners.delete(listener);
|
|
680
|
+
}), ownsRevision = (targetRevision, startLeaseEpoch, startFactoryEpoch) => !isDisposed && leaseEpoch === startLeaseEpoch && getFactoryEpoch() === startFactoryEpoch && requestedRevision === targetRevision, failRevision = (targetRevision, startLeaseEpoch, startFactoryEpoch, cause) => {
|
|
681
|
+
ownsRevision(targetRevision, startLeaseEpoch, startFactoryEpoch) && (candidateRevision = null, candidateProjections = null, candidateLayout = null, projectionRevision = committedRevision, projections = committedProjections ?? EMPTY_PROJECTIONS, layout = committedLayout, status = "error", error = cause instanceof Error ? cause.message : String(cause), publishIfChanged());
|
|
682
|
+
};
|
|
683
|
+
return {
|
|
684
|
+
getSnapshot,
|
|
685
|
+
subscribe,
|
|
686
|
+
compileRevision: async (revision) => {
|
|
687
|
+
if (isDisposed) return;
|
|
688
|
+
let targetRevision = ++requestedRevision, startLeaseEpoch = leaseEpoch, startFactoryEpoch = getFactoryEpoch(), isCurrent = () => ownsRevision(targetRevision, startLeaseEpoch, startFactoryEpoch);
|
|
689
|
+
if (status = "compiling", error = null, publishIfChanged(), !isCurrent()) return;
|
|
690
|
+
let artifact;
|
|
691
|
+
try {
|
|
692
|
+
artifact = await getOrCompileArtifact(revision.source);
|
|
693
|
+
} catch (cause) {
|
|
694
|
+
failRevision(targetRevision, startLeaseEpoch, startFactoryEpoch, cause);
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
if (!isCurrent()) return;
|
|
698
|
+
let evaluated;
|
|
699
|
+
try {
|
|
700
|
+
evaluated = await evaluateCompiledMdx(
|
|
701
|
+
artifact.code,
|
|
702
|
+
{ imports: options.imports },
|
|
703
|
+
isCurrent
|
|
704
|
+
);
|
|
705
|
+
} catch (cause) {
|
|
706
|
+
failRevision(targetRevision, startLeaseEpoch, startFactoryEpoch, cause);
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
if (!isCurrent() || evaluated === null) return;
|
|
710
|
+
let nextProjections, nextLayout;
|
|
711
|
+
try {
|
|
712
|
+
if (!isCurrent()) return;
|
|
713
|
+
let regionComponents = evaluated.createRegions({ components });
|
|
714
|
+
if (!isCurrent()) return;
|
|
715
|
+
nextProjections = projectMdxBlocks(revision.source, revision.blocks, artifact.regions, regionComponents), nextLayout = evaluated.layout ?? null;
|
|
716
|
+
} catch (cause) {
|
|
717
|
+
failRevision(targetRevision, startLeaseEpoch, startFactoryEpoch, cause);
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
isCurrent() && (candidateRevision = targetRevision, candidateProjections = nextProjections, candidateLayout = nextLayout, projectionRevision = targetRevision, projections = nextProjections, layout = nextLayout, status = "ready", error = null, publishIfChanged());
|
|
721
|
+
},
|
|
722
|
+
confirmRenderedRevision: (revision) => {
|
|
723
|
+
isDisposed || candidateRevision !== revision || candidateProjections === null || renderedRevision === revision && committedRevision === revision || (renderedRevision = revision, committedRevision = revision, committedProjections = candidateProjections, committedLayout = candidateLayout, candidateRevision = null, candidateProjections = null, candidateLayout = null, publishIfChanged());
|
|
724
|
+
},
|
|
725
|
+
reportRenderError: (revision, cause) => {
|
|
726
|
+
isDisposed || candidateRevision !== revision || candidateProjections === null || (candidateRevision = null, candidateProjections = null, candidateLayout = null, projectionRevision = committedRevision, projections = committedProjections ?? EMPTY_PROJECTIONS, layout = committedLayout, status = "error", error = cause instanceof Error ? cause.message : String(cause), publishIfChanged());
|
|
727
|
+
},
|
|
728
|
+
clear: () => {
|
|
729
|
+
isDisposed || (leaseEpoch += 1, candidateRevision = null, candidateProjections = null, candidateLayout = null, committedRevision = null, committedProjections = null, committedLayout = null, projectionRevision = null, renderedRevision = null, status = "idle", error = null, projections = EMPTY_PROJECTIONS, layout = null, publishIfChanged());
|
|
730
|
+
},
|
|
731
|
+
dispose: () => {
|
|
732
|
+
isDisposed || (isDisposed = !0, leaseEpoch += 1, candidateRevision = null, candidateProjections = null, candidateLayout = null, committedRevision = null, committedProjections = null, committedLayout = null, projectionRevision = null, renderedRevision = null, status = "disposed", error = null, projections = EMPTY_PROJECTIONS, layout = null, listeners.clear(), currentSnapshot = createSnapshot(), onDispose());
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// src/trusted-mdx/engine.ts
|
|
738
|
+
function createTrustedMdxEngine(options = {}) {
|
|
739
|
+
let frozenComponents = options.components ? Object.freeze({ ...options.components }) : void 0, frozenImports;
|
|
740
|
+
if (options.imports) {
|
|
741
|
+
let importNamespaces = [];
|
|
742
|
+
for (let [specifier, namespace] of Object.entries(options.imports))
|
|
743
|
+
importNamespaces.push([specifier, Object.freeze({ ...namespace })]);
|
|
744
|
+
frozenImports = Object.freeze(Object.fromEntries(importNamespaces));
|
|
745
|
+
}
|
|
746
|
+
let immutableOptions = Object.freeze({
|
|
747
|
+
components: frozenComponents,
|
|
748
|
+
imports: frozenImports,
|
|
749
|
+
compile: options.compile,
|
|
750
|
+
compilerIdentity: options.compilerIdentity ?? import_mdx_compile.TRUSTED_MDX_COMPILER_REVISION
|
|
751
|
+
}), compileFn = immutableOptions.compile, factoryEpoch = 0, isDisposed = !1, lastArtifactSource = null, lastArtifact = null, inflightCompilations = /* @__PURE__ */ new Map(), activeLeases = /* @__PURE__ */ new Set(), generationListeners = /* @__PURE__ */ new Set(), getOrCompileArtifact = (source) => {
|
|
752
|
+
if (isDisposed)
|
|
753
|
+
return Promise.reject(new Error("Cannot compile MDX revision: TrustedMdxEngine has been disposed."));
|
|
754
|
+
if (lastArtifactSource === source && lastArtifact !== null)
|
|
755
|
+
return Promise.resolve(lastArtifact);
|
|
756
|
+
let existingCompilation = inflightCompilations.get(source);
|
|
757
|
+
if (existingCompilation) return existingCompilation;
|
|
758
|
+
let startFactoryEpoch = factoryEpoch, compilation;
|
|
759
|
+
return compilation = Promise.resolve().then(() => {
|
|
760
|
+
if (isDisposed || factoryEpoch !== startFactoryEpoch)
|
|
761
|
+
throw new Error("TrustedMDX compilation was invalidated before it started.");
|
|
762
|
+
return compileFn ? compileFn(source) : (0, import_mdx_compile.compileTrustedMdxDocument)(source);
|
|
763
|
+
}).then((artifact) => (!isDisposed && factoryEpoch === startFactoryEpoch && (lastArtifactSource = source, lastArtifact = artifact), artifact)).finally(() => {
|
|
764
|
+
inflightCompilations.get(source) === compilation && inflightCompilations.delete(source);
|
|
765
|
+
}), inflightCompilations.set(source, compilation), compilation;
|
|
766
|
+
}, getFactoryEpoch = () => factoryEpoch, getGeneration = () => factoryEpoch, subscribe = (listener) => (isDisposed || generationListeners.add(listener), () => {
|
|
767
|
+
generationListeners.delete(listener);
|
|
768
|
+
}), notifyGenerationListeners = () => {
|
|
769
|
+
let notificationFailed = !1, notificationFailure;
|
|
770
|
+
for (let listener of generationListeners)
|
|
771
|
+
try {
|
|
772
|
+
listener();
|
|
773
|
+
} catch (cause) {
|
|
774
|
+
notificationFailed || (notificationFailed = !0, notificationFailure = cause);
|
|
775
|
+
}
|
|
776
|
+
if (notificationFailed) throw notificationFailure;
|
|
777
|
+
};
|
|
778
|
+
return {
|
|
779
|
+
createDocument: () => {
|
|
780
|
+
if (isDisposed)
|
|
781
|
+
throw new Error("Cannot create document: TrustedMdxEngine has been disposed.");
|
|
782
|
+
let document = null, createdDocument = createTrustedMdxDocumentLease({
|
|
783
|
+
options: immutableOptions,
|
|
784
|
+
getFactoryEpoch,
|
|
785
|
+
getOrCompileArtifact,
|
|
786
|
+
onDispose: () => {
|
|
787
|
+
document !== null && activeLeases.delete(document);
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
return document = createdDocument, activeLeases.add(createdDocument), createdDocument;
|
|
791
|
+
},
|
|
792
|
+
getGeneration,
|
|
793
|
+
subscribe,
|
|
794
|
+
clearCache: () => {
|
|
795
|
+
if (isDisposed) return;
|
|
796
|
+
factoryEpoch += 1, lastArtifactSource = null, lastArtifact = null, inflightCompilations.clear();
|
|
797
|
+
let notificationFailed = !1, notificationFailure;
|
|
798
|
+
try {
|
|
799
|
+
notifyGenerationListeners();
|
|
800
|
+
} catch (cause) {
|
|
801
|
+
notificationFailed = !0, notificationFailure = cause;
|
|
802
|
+
}
|
|
803
|
+
for (let document of activeLeases)
|
|
804
|
+
try {
|
|
805
|
+
document.clear();
|
|
806
|
+
} catch (cause) {
|
|
807
|
+
notificationFailed || (notificationFailed = !0, notificationFailure = cause);
|
|
808
|
+
}
|
|
809
|
+
if (notificationFailed) throw notificationFailure;
|
|
810
|
+
},
|
|
811
|
+
dispose: () => {
|
|
812
|
+
if (isDisposed) return;
|
|
813
|
+
isDisposed = !0, factoryEpoch += 1, lastArtifactSource = null, lastArtifact = null, inflightCompilations.clear();
|
|
814
|
+
let notificationFailed = !1, notificationFailure;
|
|
815
|
+
try {
|
|
816
|
+
notifyGenerationListeners();
|
|
817
|
+
} catch (cause) {
|
|
818
|
+
notificationFailed = !0, notificationFailure = cause;
|
|
819
|
+
}
|
|
820
|
+
for (let document of activeLeases)
|
|
821
|
+
try {
|
|
822
|
+
document.dispose();
|
|
823
|
+
} catch (cause) {
|
|
824
|
+
notificationFailed || (notificationFailed = !0, notificationFailure = cause);
|
|
825
|
+
}
|
|
826
|
+
if (activeLeases.clear(), generationListeners.clear(), notificationFailed) throw notificationFailure;
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
}
|
|
830
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
831
|
+
0 && (module.exports = {
|
|
832
|
+
createTrustedMdxEngine
|
|
833
|
+
});
|