@conloca/mdx 0.2.3 → 0.2.5
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/.build-inputs.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"packages/mdx/src/mdx/literal-attributes.ts": "8256d84fa7e135c4c8d802bc9259781ef4e1101d61a9605ed2773604114a5100",
|
|
16
16
|
"packages/mdx/src/mdx/restrict-expressions.ts": "6acc4426bd315e0a6bb6a5f94187b7c34710781f370e0ddbd9c8436e0c1d82cc",
|
|
17
17
|
"packages/mdx/src/node.ts": "7f087e8cd4965927a17c388088c3f17567e26af1eb8ef9616808d75ceabe8c99",
|
|
18
|
-
"packages/mdx/src/sandbox-frame.ts": "
|
|
18
|
+
"packages/mdx/src/sandbox-frame.ts": "91338e3e94f78a00bcb0f3260deea22bbfb1c58c4f344eb074c9b7c4e65580b7",
|
|
19
19
|
"packages/mdx/src/types.ts": "99ff35d0a6f59308b7f7ae188aa20164ae04ce043c0e4917199ba09a87b062b8",
|
|
20
20
|
"packages/mdx/tsconfig.lib.json": "381a7f94b78a2598ce415629f8aef13a0664cf60299cffca6a783269c0b0ff88"
|
|
21
21
|
},
|
package/dist/sandbox-frame.d.ts
CHANGED
|
@@ -20,7 +20,10 @@ export interface SandboxRenderMessage {
|
|
|
20
20
|
payload: string;
|
|
21
21
|
/** Optional stylesheet text injected into the frame for preview fidelity. */
|
|
22
22
|
styles?: string;
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Data URLs to show instead of an image the frame cannot load itself, keyed by its stored value: an element's
|
|
25
|
+
* `src` in both modes, and an MDX component's string prop.
|
|
26
|
+
*/
|
|
24
27
|
images?: Record<string, string>;
|
|
25
28
|
}
|
|
26
29
|
export type SandboxOutboundMessage = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox-frame.d.ts","sourceRoot":"","sources":["../src/sandbox-frame.ts"],"names":[],"mappings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GAIG;AACH,eAAO,MAAM,wBAAwB,8LAAwE,CAAC;AAU9G;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,CAc3C;AAED,sEAAsE;AACtE,eAAO,MAAM,kBAAkB,8BAA8B,CAAC"}
|
package/dist/sandbox-frame.js
CHANGED
|
@@ -65,6 +65,29 @@ export function createSandboxRuntime(images) {
|
|
|
65
65
|
}
|
|
66
66
|
return el;
|
|
67
67
|
};
|
|
68
|
+
// A component takes images as props of any name, so every string prop at any depth the host sent bytes for is
|
|
69
|
+
// swapped; `children` is content, not a link, and stays as written.
|
|
70
|
+
// oxlint-disable-next-line unicorn/consistent-function-scoping, anti-slop/no-unknown-returns, anti-slop/no-known-value-widening -- the frame receives this function as source text, so its helpers stay inside it; props are whatever the component takes, so every value passes through as it came.
|
|
71
|
+
const withSentImages = (value, sent) => {
|
|
72
|
+
if (typeof value === 'string') {
|
|
73
|
+
// Inherited keys are never strings, so only a value the host sent is swapped.
|
|
74
|
+
const data = sent[value];
|
|
75
|
+
return typeof data === 'string' ? data : value;
|
|
76
|
+
}
|
|
77
|
+
if (Array.isArray(value))
|
|
78
|
+
return value.map((item) => withSentImages(item, sent));
|
|
79
|
+
if (value === null || typeof value !== 'object')
|
|
80
|
+
return value;
|
|
81
|
+
const prototype = Object.getPrototypeOf(value);
|
|
82
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
83
|
+
return value;
|
|
84
|
+
// oxlint-disable-next-line anti-slop/no-unsafe-dictionary-type -- a copy of the component's own props, values as they came.
|
|
85
|
+
const out = {};
|
|
86
|
+
for (const [key, item] of Object.entries(value))
|
|
87
|
+
out[key] = key === 'children' ? item : withSentImages(item, sent);
|
|
88
|
+
// oxlint-disable-next-line anti-slop/no-known-value-widening -- the copy is as open as the props it came from.
|
|
89
|
+
return out;
|
|
90
|
+
};
|
|
68
91
|
const jsx = (type, props) => {
|
|
69
92
|
const p = props || {};
|
|
70
93
|
if (type === Fragment) {
|
|
@@ -73,8 +96,14 @@ export function createSandboxRuntime(images) {
|
|
|
73
96
|
frag.appendChild(child);
|
|
74
97
|
return frag;
|
|
75
98
|
}
|
|
76
|
-
if (typeof type === 'function')
|
|
77
|
-
|
|
99
|
+
if (typeof type === 'function') {
|
|
100
|
+
// SAFETY: `p` is a plain props object, and withSentImages copies a plain object into a plain object.
|
|
101
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion, anti-slop/no-unsafe-dictionary-type -- same reason as the SAFETY note.
|
|
102
|
+
const componentProps = images ? withSentImages(p, images) : p;
|
|
103
|
+
// SAFETY: compiled MDX passes only components, which take one props object and return this runtime's DOM nodes.
|
|
104
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion, anti-slop/no-unsafe-dictionary-type -- same reason as the SAFETY note.
|
|
105
|
+
return type(componentProps);
|
|
106
|
+
}
|
|
78
107
|
if (typeof type === 'string')
|
|
79
108
|
return element(type, p, false);
|
|
80
109
|
const missing = document.createElement('span');
|
|
@@ -171,10 +200,21 @@ export function sandboxFrameMain(deps) {
|
|
|
171
200
|
});
|
|
172
201
|
});
|
|
173
202
|
};
|
|
174
|
-
const renderHtml = (html) => {
|
|
203
|
+
const renderHtml = (html, images) => {
|
|
175
204
|
// Already allowlist-sanitized by the host; the sandbox contains anything
|
|
176
205
|
// that slips through (opaque origin, no privileged globals, strict CSP).
|
|
177
|
-
|
|
206
|
+
// Parsed inert first, so nothing loads before a sent src replaces the stored one.
|
|
207
|
+
const parsed = new DOMParser().parseFromString(html, 'text/html');
|
|
208
|
+
if (images) {
|
|
209
|
+
parsed.querySelectorAll('[src]').forEach((el) => {
|
|
210
|
+
// Inherited keys are never strings, so only a src the host sent is swapped.
|
|
211
|
+
const sent = images[el.getAttribute('src') ?? ''];
|
|
212
|
+
if (typeof sent === 'string')
|
|
213
|
+
el.setAttribute('src', sent);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
// A leading <style> or <link> lands in the parsed head; keep it with the body, as innerHTML did.
|
|
217
|
+
root.replaceChildren(...Array.from(parsed.head.childNodes), ...Array.from(parsed.body.childNodes));
|
|
178
218
|
post({ __conlocaSandbox: true, kind: 'rendered' });
|
|
179
219
|
postHeight();
|
|
180
220
|
};
|
|
@@ -190,7 +230,7 @@ export function sandboxFrameMain(deps) {
|
|
|
190
230
|
if (data.mode === 'mdx')
|
|
191
231
|
renderMdx(data.payload, data.images);
|
|
192
232
|
else
|
|
193
|
-
renderHtml(data.payload);
|
|
233
|
+
renderHtml(data.payload, data.images);
|
|
194
234
|
}
|
|
195
235
|
catch (error) {
|
|
196
236
|
post({ __conlocaSandbox: true, kind: 'error', message: error instanceof Error ? error.message : String(error) });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox-frame.js","sourceRoot":"","sources":["../src/sandbox-frame.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"sandbox-frame.js","sourceRoot":"","sources":["../src/sandbox-frame.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAkCH,MAAM,UAAU,oBAAoB,CAAC,MAA+B;IAMlE,qFAAqF;IACrF,MAAM,QAAQ,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;IAC5C,MAAM,MAAM,GAAG,4BAA4B,CAAC;IAC5C,yEAAyE;IACzE,4EAA4E;IAC5E,4EAA4E;IAC5E,8CAA8C;IAC9C,MAAM,MAAM,GAAG,IAAI,OAAO,EAA0D,CAAC;IACrF,kJAAkJ;IAClJ,MAAM,OAAO,GAAG,CAAC,KAAc,EAAU,EAAE;QACzC,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,KAAK;YAAE,OAAO,EAAE,CAAC;QAClE,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YAAE,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACxD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAA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D,CAAC,cAAc,CAAC;IACjB,IAAI,SAAS;QAAE,IAAI,SAAS,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACvD,IAAI,CAAC,EAAE,gBAAgB,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AAClD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAC5B,4IAA4I,CAAC;AAE/I;;;;GAIG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,GAAG,iBAAiB,iDAAiD,CAAC;AAE9G;;;;;GAKG;AACH,MAAM,sBAAsB,GAAG,qCAAqC,CAAC;AAErE;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB;IAChC,MAAM,SAAS,GAAG,GAAG,sBAAsB,IAAI,gBAAgB,CAAC,QAAQ,EAAE,2BAA2B,oBAAoB,CAAC,QAAQ,EAAE,mBAAmB,cAAc,CAAC,QAAQ,EAAE,IAAI,CAAC;IACrL,OAAO;QACL,mDAAmD;QACnD,uDAAuD,iBAAiB,IAAI;QAC5E,sGAAsG;QACtG,mEAAmE;QACnE,qEAAqE;QACrE,sEAAsE;QACtE,wDAAwD;QACxD,oEAAoE;QACpE,WAAW,SAAS,WAAW;QAC/B,gBAAgB;KACjB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACb,CAAC;AAED,sEAAsE;AACtE,MAAM,CAAC,MAAM,kBAAkB,GAAG,2BAA2B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@conloca/mdx",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"main": "./dist/index.js",
|
|
13
13
|
"module": "./dist/index.js",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@conloca/content-api": "0.2.
|
|
15
|
+
"@conloca/content-api": "0.2.5",
|
|
16
16
|
"@mdx-js/mdx": "^3.1.1",
|
|
17
17
|
"@mdx-js/react": "^3.1.1",
|
|
18
18
|
"@mdxeditor/editor": "^4.2.3",
|
|
@@ -77,7 +77,6 @@
|
|
|
77
77
|
},
|
|
78
78
|
"nx": {
|
|
79
79
|
"targets": {
|
|
80
|
-
"clean": {},
|
|
81
80
|
"test": {
|
|
82
81
|
"dependsOn": [
|
|
83
82
|
"typecheck-tests",
|
package/src/sandbox-frame.ts
CHANGED
|
@@ -22,7 +22,10 @@ export interface SandboxRenderMessage {
|
|
|
22
22
|
payload: string;
|
|
23
23
|
/** Optional stylesheet text injected into the frame for preview fidelity. */
|
|
24
24
|
styles?: string;
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Data URLs to show instead of an image the frame cannot load itself, keyed by its stored value: an element's
|
|
27
|
+
* `src` in both modes, and an MDX component's string prop.
|
|
28
|
+
*/
|
|
26
29
|
images?: Record<string, string>;
|
|
27
30
|
}
|
|
28
31
|
|
|
@@ -88,6 +91,25 @@ export function createSandboxRuntime(images?: Record<string, string>): {
|
|
|
88
91
|
}
|
|
89
92
|
return el;
|
|
90
93
|
};
|
|
94
|
+
// A component takes images as props of any name, so every string prop at any depth the host sent bytes for is
|
|
95
|
+
// swapped; `children` is content, not a link, and stays as written.
|
|
96
|
+
// oxlint-disable-next-line unicorn/consistent-function-scoping, anti-slop/no-unknown-returns, anti-slop/no-known-value-widening -- the frame receives this function as source text, so its helpers stay inside it; props are whatever the component takes, so every value passes through as it came.
|
|
97
|
+
const withSentImages = (value: unknown, sent: Record<string, string>): unknown => {
|
|
98
|
+
if (typeof value === 'string') {
|
|
99
|
+
// Inherited keys are never strings, so only a value the host sent is swapped.
|
|
100
|
+
const data = sent[value];
|
|
101
|
+
return typeof data === 'string' ? data : value;
|
|
102
|
+
}
|
|
103
|
+
if (Array.isArray(value)) return value.map((item) => withSentImages(item, sent));
|
|
104
|
+
if (value === null || typeof value !== 'object') return value;
|
|
105
|
+
const prototype: unknown = Object.getPrototypeOf(value);
|
|
106
|
+
if (prototype !== Object.prototype && prototype !== null) return value;
|
|
107
|
+
// oxlint-disable-next-line anti-slop/no-unsafe-dictionary-type -- a copy of the component's own props, values as they came.
|
|
108
|
+
const out: Record<string, unknown> = {};
|
|
109
|
+
for (const [key, item] of Object.entries(value)) out[key] = key === 'children' ? item : withSentImages(item, sent);
|
|
110
|
+
// oxlint-disable-next-line anti-slop/no-known-value-widening -- the copy is as open as the props it came from.
|
|
111
|
+
return out;
|
|
112
|
+
};
|
|
91
113
|
const jsx = (type: unknown, props: Record<string, unknown>): Node => {
|
|
92
114
|
const p = props || {};
|
|
93
115
|
if (type === Fragment) {
|
|
@@ -95,7 +117,14 @@ export function createSandboxRuntime(images?: Record<string, string>): {
|
|
|
95
117
|
for (const child of toNodes(p.children)) frag.appendChild(child);
|
|
96
118
|
return frag;
|
|
97
119
|
}
|
|
98
|
-
if (typeof type === 'function')
|
|
120
|
+
if (typeof type === 'function') {
|
|
121
|
+
// SAFETY: `p` is a plain props object, and withSentImages copies a plain object into a plain object.
|
|
122
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion, anti-slop/no-unsafe-dictionary-type -- same reason as the SAFETY note.
|
|
123
|
+
const componentProps = images ? (withSentImages(p, images) as Record<string, unknown>) : p;
|
|
124
|
+
// SAFETY: compiled MDX passes only components, which take one props object and return this runtime's DOM nodes.
|
|
125
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion, anti-slop/no-unsafe-dictionary-type -- same reason as the SAFETY note.
|
|
126
|
+
return (type as (props: Record<string, unknown>) => Node)(componentProps);
|
|
127
|
+
}
|
|
99
128
|
if (typeof type === 'string') return element(type, p, false);
|
|
100
129
|
const missing = document.createElement('span');
|
|
101
130
|
missing.setAttribute('data-sandbox-unknown', '');
|
|
@@ -210,10 +239,20 @@ export function sandboxFrameMain(deps: {
|
|
|
210
239
|
});
|
|
211
240
|
});
|
|
212
241
|
};
|
|
213
|
-
const renderHtml = (html: string): void => {
|
|
242
|
+
const renderHtml = (html: string, images: Record<string, string> | undefined): void => {
|
|
214
243
|
// Already allowlist-sanitized by the host; the sandbox contains anything
|
|
215
244
|
// that slips through (opaque origin, no privileged globals, strict CSP).
|
|
216
|
-
|
|
245
|
+
// Parsed inert first, so nothing loads before a sent src replaces the stored one.
|
|
246
|
+
const parsed = new DOMParser().parseFromString(html, 'text/html');
|
|
247
|
+
if (images) {
|
|
248
|
+
parsed.querySelectorAll('[src]').forEach((el) => {
|
|
249
|
+
// Inherited keys are never strings, so only a src the host sent is swapped.
|
|
250
|
+
const sent = images[el.getAttribute('src') ?? ''];
|
|
251
|
+
if (typeof sent === 'string') el.setAttribute('src', sent);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
// A leading <style> or <link> lands in the parsed head; keep it with the body, as innerHTML did.
|
|
255
|
+
root.replaceChildren(...Array.from(parsed.head.childNodes), ...Array.from(parsed.body.childNodes));
|
|
217
256
|
post({ __conlocaSandbox: true, kind: 'rendered' });
|
|
218
257
|
postHeight();
|
|
219
258
|
};
|
|
@@ -226,7 +265,7 @@ export function sandboxFrameMain(deps: {
|
|
|
226
265
|
if (typeof data.styles === 'string') applyStyles(data.styles);
|
|
227
266
|
try {
|
|
228
267
|
if (data.mode === 'mdx') renderMdx(data.payload, data.images);
|
|
229
|
-
else renderHtml(data.payload);
|
|
268
|
+
else renderHtml(data.payload, data.images);
|
|
230
269
|
} catch (error: unknown) {
|
|
231
270
|
post({ __conlocaSandbox: true, kind: 'error', message: error instanceof Error ? error.message : String(error) });
|
|
232
271
|
}
|