@conloca/mdx 0.2.0 → 0.2.2
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/mdx/compile.d.ts.map +1 -1
- package/dist/mdx/compile.js +10 -1
- package/dist/mdx/compile.js.map +1 -1
- package/dist/mdx/evaluate.d.ts.map +1 -1
- package/dist/mdx/evaluate.js +39 -19
- package/dist/mdx/evaluate.js.map +1 -1
- package/dist/mdx/literal-attributes.d.ts +26 -0
- package/dist/mdx/literal-attributes.d.ts.map +1 -0
- package/dist/mdx/literal-attributes.js +40 -0
- package/dist/mdx/literal-attributes.js.map +1 -0
- package/dist/mdx/restrict-expressions.d.ts +2 -0
- package/dist/mdx/restrict-expressions.d.ts.map +1 -1
- package/dist/mdx/restrict-expressions.js +12 -1
- package/dist/mdx/restrict-expressions.js.map +1 -1
- package/dist/sandbox-frame.d.ts.map +1 -1
- package/dist/sandbox-frame.js +44 -25
- package/dist/sandbox-frame.js.map +1 -1
- package/package.json +7 -7
- package/src/editor-styles.css +22 -20
- package/src/mdx/compile.ts +10 -1
- package/src/mdx/evaluate.ts +46 -16
- package/src/mdx/literal-attributes.ts +50 -0
- package/src/mdx/restrict-expressions.ts +12 -1
- package/src/sandbox-frame.ts +36 -19
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/mdx/compile.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/mdx/compile.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAKpD,wBAAsB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAiD3E"}
|
package/dist/mdx/compile.js
CHANGED
|
@@ -4,6 +4,7 @@ import remarkFrontmatter from 'remark-frontmatter';
|
|
|
4
4
|
import remarkGfm from 'remark-gfm';
|
|
5
5
|
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
|
|
6
6
|
import { applyHeadingIds } from './heading-outline.js';
|
|
7
|
+
import { remarkDropUnrenderedAttributes } from './literal-attributes.js';
|
|
7
8
|
import { remarkRestrictExpressions } from './restrict-expressions.js';
|
|
8
9
|
export async function compileMDX(content) {
|
|
9
10
|
try {
|
|
@@ -26,7 +27,15 @@ export async function compileMDX(content) {
|
|
|
26
27
|
// `import`/`export` parse into `mdxjsEsm` nodes at parse time (so restrict
|
|
27
28
|
// sees and rejects them), while the legitimate `export const frontmatter`
|
|
28
29
|
// that `remarkMdxFrontmatter` injects appears only after restrict has run.
|
|
29
|
-
|
|
30
|
+
// `remarkDropUnrenderedAttributes` drops what the published fragment
|
|
31
|
+
// drops (`style={{…}}`, lists, `null`), so the preview matches it.
|
|
32
|
+
remarkPlugins: [
|
|
33
|
+
remarkFrontmatter,
|
|
34
|
+
remarkRestrictExpressions,
|
|
35
|
+
remarkDropUnrenderedAttributes,
|
|
36
|
+
remarkMdxFrontmatter,
|
|
37
|
+
remarkGfm,
|
|
38
|
+
],
|
|
30
39
|
rehypePlugins: [collectOutline],
|
|
31
40
|
});
|
|
32
41
|
return {
|
package/dist/mdx/compile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/mdx/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,iBAAiB,MAAM,oBAAoB,CAAC;AACnD,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,oBAAoB,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAE,eAAe,EAA0C,MAAM,sBAAsB,CAAC;AAC/F,OAAO,EAAE,yBAAyB,EAAE,MA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"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAQ3C,OAAO,EAAmB,KAAK,eAAe,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../../src/mdx/evaluate.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAQ3C,OAAO,EAAmB,KAAK,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAgH7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,sBAAsB,CAC1C,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC;IAAE,SAAS,EAAE,aAAa,GAAG,IAAI,CAAC;IAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,SAAS,eAAe,EAAE,CAAA;CAAE,CAAC,CAkBlG"}
|
package/dist/mdx/evaluate.js
CHANGED
|
@@ -7,6 +7,7 @@ import remarkParse from 'remark-parse';
|
|
|
7
7
|
import remarkRehype from 'remark-rehype';
|
|
8
8
|
import { unified } from 'unified';
|
|
9
9
|
import { applyHeadingIds } from './heading-outline.js';
|
|
10
|
+
import { literalAttributeValue } from './literal-attributes.js';
|
|
10
11
|
import { remarkRestrictExpressions } from './restrict-expressions.js';
|
|
11
12
|
/**
|
|
12
13
|
* Make every JSX element in the tree statically renderable.
|
|
@@ -18,10 +19,12 @@ import { remarkRestrictExpressions } from './restrict-expressions.js';
|
|
|
18
19
|
* JSON-literal `{…}` attribute values (the only expressions
|
|
19
20
|
* `remarkRestrictExpressions` admits) are resolved into plain static values,
|
|
20
21
|
* so rendering never needs an expression evaluator: strings pass through,
|
|
21
|
-
* numbers stringify (identical SSR output), `{true}`
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* numbers stringify (identical SSR output), and `{true}`/`{false}` reach React
|
|
23
|
+
* as booleans, which it renders as it renders any boolean prop: `{false}`
|
|
24
|
+
* leaves out `disabled` but writes `aria-hidden="false"` and `data-open="false"`.
|
|
25
|
+
* `null` and object literals drop the attribute. The converter reads an object
|
|
26
|
+
* value as an expression to run, which this path never does, so `style={{…}}`
|
|
27
|
+
* on an HTML element is dropped too.
|
|
25
28
|
*/
|
|
26
29
|
function resolveStaticJsx() {
|
|
27
30
|
return (tree) => {
|
|
@@ -38,22 +41,11 @@ function resolveStaticJsx() {
|
|
|
38
41
|
if (!value || typeof value !== 'object')
|
|
39
42
|
return [attr];
|
|
40
43
|
const raw = value.value;
|
|
41
|
-
|
|
44
|
+
// The same rule compileMDX applies, so the editor preview drops what this drops.
|
|
45
|
+
const literal = typeof raw === 'string' ? literalAttributeValue(raw) : undefined;
|
|
46
|
+
if (literal === undefined)
|
|
42
47
|
return [];
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
parsed = JSON.parse(raw.trim());
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
return []; // non-literal — already dropped by the restrict gate; belt and braces
|
|
49
|
-
}
|
|
50
|
-
if (typeof parsed === 'string')
|
|
51
|
-
return [{ ...attr, value: parsed }];
|
|
52
|
-
if (typeof parsed === 'number')
|
|
53
|
-
return [{ ...attr, value: String(parsed) }];
|
|
54
|
-
if (parsed === true)
|
|
55
|
-
return [{ ...attr, value: null }];
|
|
56
|
-
return [];
|
|
48
|
+
return [{ ...attr, value: typeof literal === 'number' ? String(literal) : literal }];
|
|
57
49
|
});
|
|
58
50
|
}
|
|
59
51
|
if (Array.isArray(node.children))
|
|
@@ -62,6 +54,33 @@ function resolveStaticJsx() {
|
|
|
62
54
|
walk(tree);
|
|
63
55
|
};
|
|
64
56
|
}
|
|
57
|
+
/** Nothing, or only the HTML whitespace a line break or a space between two tags leaves. */
|
|
58
|
+
const BLANK = /^[\t\n\f\r ]*$/;
|
|
59
|
+
/**
|
|
60
|
+
* Lift HTML tags out of a paragraph that holds nothing else. Markdown reads a tag written on one line,
|
|
61
|
+
* `<div>x</div>`, as the start of a paragraph, and `<p><div>x</div></p>` is HTML a browser splits into
|
|
62
|
+
* an empty paragraph and a loose `<div>`. The `@mdx-js/mdx` compiler this pipeline replaced does the
|
|
63
|
+
* same lifting (its `remarkMarkAndUnravel`); a paragraph with any text beside its tags keeps its `<p>`.
|
|
64
|
+
*/
|
|
65
|
+
function unravelJsxParagraphs() {
|
|
66
|
+
const lift = (node) => {
|
|
67
|
+
node.children = node.children.flatMap((child) => {
|
|
68
|
+
if (child.type !== 'paragraph')
|
|
69
|
+
return [child];
|
|
70
|
+
const tags = child.children.filter((inline) => inline.type === 'mdxJsxTextElement');
|
|
71
|
+
const onlyTags = child.children.every((inline) => inline.type === 'mdxJsxTextElement' || (inline.type === 'text' && BLANK.test(inline.value)));
|
|
72
|
+
if (tags.length === 0 || !onlyTags)
|
|
73
|
+
return [child];
|
|
74
|
+
// A tag's inline content stays as it is: written on one line, it never held a block.
|
|
75
|
+
return tags.map((tag) => ({ ...tag, type: 'mdxJsxFlowElement', children: tag.children }));
|
|
76
|
+
});
|
|
77
|
+
for (const child of node.children) {
|
|
78
|
+
if ('children' in child)
|
|
79
|
+
lift(child);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
return (tree) => lift(tree);
|
|
83
|
+
}
|
|
65
84
|
/**
|
|
66
85
|
* Markdown → mdast (with MDX JSX syntax) → hast, with the JSX element nodes
|
|
67
86
|
* passed through for `toJsxRuntime` to turn into React elements directly.
|
|
@@ -76,6 +95,7 @@ const processor = unified()
|
|
|
76
95
|
// unified's strict Plugin shape here rather than forking their signatures.
|
|
77
96
|
.use(remarkRestrictExpressions)
|
|
78
97
|
.use(resolveStaticJsx)
|
|
98
|
+
.use(unravelJsxParagraphs)
|
|
79
99
|
.use(remarkRehype, { passThrough: ['mdxJsxFlowElement', 'mdxJsxTextElement'] });
|
|
80
100
|
/**
|
|
81
101
|
* Turn an MDX content string into a React component WITHOUT executing any
|
package/dist/mdx/evaluate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../../src/mdx/evaluate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAGxD,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,iBAAiB,MAAM,oBAAoB,CAAC;AACnD,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,EAAe,OAAO,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAwB,MAAM,sBAAsB,CAAC;AAC7E,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAetE
|
|
1
|
+
{"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../../src/mdx/evaluate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAGxD,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,iBAAiB,MAAM,oBAAoB,CAAC;AACnD,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,EAAe,OAAO,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAwB,MAAM,sBAAsB,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAetE;;;;;;;;;;;;;;;;GAgBG;AACH,SAAS,g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,OAAO;YACL,SAAS,EAAE,IAAI;YACf,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,uBAAuB,CAAC;YAC1E,OAAO,EAAE,EAAE;SACZ,CAAC;IACJ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface JsxAttributeNode {
|
|
2
|
+
type?: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
value?: unknown;
|
|
5
|
+
}
|
|
6
|
+
interface TreeNode {
|
|
7
|
+
type?: string;
|
|
8
|
+
children?: TreeNode[];
|
|
9
|
+
attributes?: JsxAttributeNode[];
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The value a JSON-literal attribute expression (`{3}`, `{true}`, `{"x"}`) has
|
|
14
|
+
* on a published fragment, or undefined when the attribute renders as nothing
|
|
15
|
+
* there: an object (`style={{…}}`), a list, `null`, or anything not JSON.
|
|
16
|
+
*/
|
|
17
|
+
export declare function literalAttributeValue(expression: string): string | number | boolean | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Remark transform that drops every JSX attribute whose `{…}` value renders as
|
|
20
|
+
* nothing on a published fragment, so a compiled preview shows what will be
|
|
21
|
+
* published. It runs on the markdown tree, before the compiler writes the
|
|
22
|
+
* style objects that align table columns, so those are kept.
|
|
23
|
+
*/
|
|
24
|
+
export declare function remarkDropUnrenderedAttributes(): (tree: TreeNode) => void;
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=literal-attributes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal-attributes.d.ts","sourceRoot":"","sources":["../../src/mdx/literal-attributes.ts"],"names":[],"mappings":"AAAA,UAAU,gBAAgB;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,UAAU,QAAQ;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;IACtB,UAAU,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAChC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAQ/F;AAED;;;;;GAKG;AACH,wBAAgB,8BAA8B,WAC9B,QAAQ,KAAG,IAAI,CAc9B"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The value a JSON-literal attribute expression (`{3}`, `{true}`, `{"x"}`) has
|
|
3
|
+
* on a published fragment, or undefined when the attribute renders as nothing
|
|
4
|
+
* there: an object (`style={{…}}`), a list, `null`, or anything not JSON.
|
|
5
|
+
*/
|
|
6
|
+
export function literalAttributeValue(expression) {
|
|
7
|
+
let parsed;
|
|
8
|
+
try {
|
|
9
|
+
parsed = JSON.parse(expression.trim());
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
return typeof parsed === 'string' || typeof parsed === 'number' || typeof parsed === 'boolean' ? parsed : undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Remark transform that drops every JSX attribute whose `{…}` value renders as
|
|
18
|
+
* nothing on a published fragment, so a compiled preview shows what will be
|
|
19
|
+
* published. It runs on the markdown tree, before the compiler writes the
|
|
20
|
+
* style objects that align table columns, so those are kept.
|
|
21
|
+
*/
|
|
22
|
+
export function remarkDropUnrenderedAttributes() {
|
|
23
|
+
return (tree) => {
|
|
24
|
+
const walk = (node) => {
|
|
25
|
+
if (Array.isArray(node.attributes)) {
|
|
26
|
+
node.attributes = node.attributes.filter((attr) => {
|
|
27
|
+
const value = attr?.value;
|
|
28
|
+
if (!value || typeof value !== 'object')
|
|
29
|
+
return true;
|
|
30
|
+
const expression = value.value;
|
|
31
|
+
return typeof expression === 'string' && literalAttributeValue(expression) !== undefined;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
if (Array.isArray(node.children))
|
|
35
|
+
node.children.forEach(walk);
|
|
36
|
+
};
|
|
37
|
+
walk(tree);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=literal-attributes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal-attributes.js","sourceRoot":"","sources":["../../src/mdx/literal-attributes.ts"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAC,UAAkB;IACtD,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;IACzC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AACtH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,8BAA8B;IAC5C,OAAO,CAAC,IAAc,EAAQ,EAAE;QAC9B,MAAM,IAAI,GAAG,CAAC,IAAc,EAAQ,EAAE;YACpC,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;gBACnC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;oBAChD,MAAM,KAAK,GAAG,IAAI,EAAE,KAAK,CAAC;oBAC1B,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;wBAAE,OAAO,IAAI,CAAC;oBACrD,MAAM,UAAU,GAAI,KAA6B,CAAC,KAAK,CAAC;oBACxD,OAAO,OAAO,UAAU,KAAK,QAAQ,IAAI,qBAAqB,CAAC,UAAU,CAAC,KAAK,SAAS,CAAC;gBAC3F,CAAC,CAAC,CAAC;YACL,CAAC;YACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;gBAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAChE,CAAC,CAAC;QACF,IAAI,CAAC,IAAI,CAAC,CAAC;IACb,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -17,6 +17,8 @@ interface MdastAttribute {
|
|
|
17
17
|
* `mdxTextExpression`) and top-level ESM `import`/`export` (`mdxjsEsm`) reach
|
|
18
18
|
* `new AsyncFunction`/`evaluate()` as live code, so they fail the compile
|
|
19
19
|
* with a clear error; content fragments never legitimately need either.
|
|
20
|
+
* A comment, `{/* … *\/}`, holds no code and is removed instead, so it
|
|
21
|
+
* renders as nothing, as it does in MDX.
|
|
20
22
|
* 2. HTML injection — `dangerouslySetInnerHTML` (React's raw-HTML escape hatch,
|
|
21
23
|
* which a JSON-literal object slips past the expression exemption), inline
|
|
22
24
|
* `on*` handlers, dangerous URL schemes (`javascript:`/`vbscript:`/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"restrict-expressions.d.ts","sourceRoot":"","sources":["../../src/mdx/restrict-expressions.ts"],"names":[],"mappings":"AAAA,UAAU,SAAS;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,SAAS,EAAE,CAAC;IACvB,UAAU,CAAC,EAAE,cAAc,EAAE,CAAC;IAC9B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,UAAU,cAAc;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;
|
|
1
|
+
{"version":3,"file":"restrict-expressions.d.ts","sourceRoot":"","sources":["../../src/mdx/restrict-expressions.ts"],"names":[],"mappings":"AAAA,UAAU,SAAS;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,SAAS,EAAE,CAAC;IACvB,UAAU,CAAC,EAAE,cAAc,EAAE,CAAC;IAC9B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,UAAU,cAAc;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAmHD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBA8BkB;AAClB,wBAAgB,yBAAyB,WACzB,SAAS,KAAG,IAAI,CAyB/B"}
|
|
@@ -49,6 +49,15 @@ function isDangerousElement(node) {
|
|
|
49
49
|
const name = typeof node.name === 'string' ? node.name : '';
|
|
50
50
|
return /^[a-z]/.test(name) && DANGEROUS_ELEMENTS.has(name.toLowerCase());
|
|
51
51
|
}
|
|
52
|
+
/** `{/* … *\/}`, the way MDX writes a comment: the parsed program has no
|
|
53
|
+
* statements, so nothing in it can run. Without the parsed program it is
|
|
54
|
+
* not trusted to be one. */
|
|
55
|
+
function isComment(node) {
|
|
56
|
+
if (node?.type !== 'mdxFlowExpression' && node?.type !== 'mdxTextExpression')
|
|
57
|
+
return false;
|
|
58
|
+
const program = node.data?.estree;
|
|
59
|
+
return Array.isArray(program?.body) && program.body.length === 0;
|
|
60
|
+
}
|
|
52
61
|
/** A JSON-literal expression whose parsed value is a dangerous-scheme URL string
|
|
53
62
|
* (`href={"data:text/html,…"}` round-trips through `JSON.parse` yet navigates). */
|
|
54
63
|
function isDangerousLiteralUrl(value) {
|
|
@@ -112,6 +121,8 @@ function sanitizeAttributes(attributes) {
|
|
|
112
121
|
* `mdxTextExpression`) and top-level ESM `import`/`export` (`mdxjsEsm`) reach
|
|
113
122
|
* `new AsyncFunction`/`evaluate()` as live code, so they fail the compile
|
|
114
123
|
* with a clear error; content fragments never legitimately need either.
|
|
124
|
+
* A comment, `{/* … *\/}`, holds no code and is removed instead, so it
|
|
125
|
+
* renders as nothing, as it does in MDX.
|
|
115
126
|
* 2. HTML injection — `dangerouslySetInnerHTML` (React's raw-HTML escape hatch,
|
|
116
127
|
* which a JSON-literal object slips past the expression exemption), inline
|
|
117
128
|
* `on*` handlers, dangerous URL schemes (`javascript:`/`vbscript:`/
|
|
@@ -150,7 +161,7 @@ export function remarkRestrictExpressions() {
|
|
|
150
161
|
if (Array.isArray(node.attributes))
|
|
151
162
|
node.attributes = sanitizeAttributes(node.attributes);
|
|
152
163
|
if (Array.isArray(node.children)) {
|
|
153
|
-
node.children = node.children.filter((child) => !isDangerousElement(child));
|
|
164
|
+
node.children = node.children.filter((child) => !isDangerousElement(child) && !isComment(child));
|
|
154
165
|
node.children.forEach(walk);
|
|
155
166
|
}
|
|
156
167
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"restrict-expressions.js","sourceRoot":"","sources":["../../src/mdx/restrict-expressions.ts"],"names":[],"mappings":"AAaA;;;0BAG0B;AAC1B,SAAS,mBAAmB,CAAC,KAAc;IACzC,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IACrD,MAAM,GAAG,GAAI,KAA6B,CAAC,KAAK,CAAC;IACjD,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IACzC,IAAI,CAAC;QACH,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;8CAE8C;AAC9C,MAAM,QAAQ,GAAG,qEAAqE,CAAC;AAEvF;2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8B,CAAC"}
|
package/dist/sandbox-frame.js
CHANGED
|
@@ -5,7 +5,14 @@
|
|
|
5
5
|
* frame's HTML at `SANDBOX_FRAME_PATH`.
|
|
6
6
|
*/
|
|
7
7
|
export function createSandboxRuntime() {
|
|
8
|
+
// Everything below stays inside this function: the frame receives it as source text.
|
|
8
9
|
const Fragment = Symbol('sandbox.fragment');
|
|
10
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
11
|
+
// A child is built before its parent, so an element written inside <svg>
|
|
12
|
+
// starts out as HTML, which a browser does not draw there. The <svg> builds
|
|
13
|
+
// it again as SVG from what each HTML element was made of, kept here, which
|
|
14
|
+
// is the namespace React would have given it.
|
|
15
|
+
const madeOf = new WeakMap();
|
|
9
16
|
const toNodes = (value) => {
|
|
10
17
|
if (value == null || value === true || value === false)
|
|
11
18
|
return [];
|
|
@@ -17,6 +24,41 @@ export function createSandboxRuntime() {
|
|
|
17
24
|
return [value];
|
|
18
25
|
return [document.createTextNode(String(value))];
|
|
19
26
|
};
|
|
27
|
+
const element = (type, p, inSvg) => {
|
|
28
|
+
const svg = inSvg || type === 'svg';
|
|
29
|
+
const el = svg ? document.createElementNS(SVG_NS, type) : document.createElement(type);
|
|
30
|
+
if (!svg)
|
|
31
|
+
madeOf.set(el, { type, props: p });
|
|
32
|
+
for (const key in p) {
|
|
33
|
+
if (key === 'children' || key === 'key')
|
|
34
|
+
continue;
|
|
35
|
+
const value = p[key];
|
|
36
|
+
if (/^on/i.test(key))
|
|
37
|
+
continue; // never wire an event handler
|
|
38
|
+
if (key === 'className') {
|
|
39
|
+
el.setAttribute('class', String(value));
|
|
40
|
+
}
|
|
41
|
+
else if (key === 'htmlFor') {
|
|
42
|
+
el.setAttribute('for', String(value));
|
|
43
|
+
}
|
|
44
|
+
else if (key === 'style' && value && typeof value === 'object') {
|
|
45
|
+
Object.assign(el.style, value);
|
|
46
|
+
}
|
|
47
|
+
else if (value === true) {
|
|
48
|
+
el.setAttribute(key, '');
|
|
49
|
+
}
|
|
50
|
+
else if (value != null && value !== false) {
|
|
51
|
+
el.setAttribute(key, String(value));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// What a <foreignObject> holds is HTML again, as in a browser.
|
|
55
|
+
const svgChildren = svg && type !== 'foreignObject';
|
|
56
|
+
for (const child of toNodes(p.children)) {
|
|
57
|
+
const source = svgChildren ? madeOf.get(child) : undefined;
|
|
58
|
+
el.appendChild(source ? element(source.type, source.props, true) : child);
|
|
59
|
+
}
|
|
60
|
+
return el;
|
|
61
|
+
};
|
|
20
62
|
const jsx = (type, props) => {
|
|
21
63
|
const p = props || {};
|
|
22
64
|
if (type === Fragment) {
|
|
@@ -27,31 +69,8 @@ export function createSandboxRuntime() {
|
|
|
27
69
|
}
|
|
28
70
|
if (typeof type === 'function')
|
|
29
71
|
return type(p);
|
|
30
|
-
if (typeof type === 'string')
|
|
31
|
-
|
|
32
|
-
for (const key in p) {
|
|
33
|
-
if (key === 'children' || key === 'key')
|
|
34
|
-
continue;
|
|
35
|
-
const value = p[key];
|
|
36
|
-
if (/^on/i.test(key))
|
|
37
|
-
continue; // never wire an event handler
|
|
38
|
-
if (key === 'className') {
|
|
39
|
-
el.setAttribute('class', String(value));
|
|
40
|
-
}
|
|
41
|
-
else if (key === 'style' && value && typeof value === 'object') {
|
|
42
|
-
Object.assign(el.style, value);
|
|
43
|
-
}
|
|
44
|
-
else if (value === true) {
|
|
45
|
-
el.setAttribute(key, '');
|
|
46
|
-
}
|
|
47
|
-
else if (value != null && value !== false) {
|
|
48
|
-
el.setAttribute(key, String(value));
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
for (const child of toNodes(p.children))
|
|
52
|
-
el.appendChild(child);
|
|
53
|
-
return el;
|
|
54
|
-
}
|
|
72
|
+
if (typeof type === 'string')
|
|
73
|
+
return element(type, p, false);
|
|
55
74
|
const missing = document.createElement('span');
|
|
56
75
|
missing.setAttribute('data-sandbox-unknown', '');
|
|
57
76
|
return missing;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox-frame.js","sourceRoot":"","sources":["../src/sandbox-frame.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA6BH,MAAM,UAAU,oBAAoB;IAMlC,MAAM,QAAQ,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;IAC5C,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,QAAQ;YAAE,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC5G,IAAI,KAAK,YAAY,IAAI;YAAE,OAAO,CAAC,KAAK,CAAC,CAAC;QAC1C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAClD,CAAC,CAAC;IACF,MAAM,
|
|
1
|
+
{"version":3,"file":"sandbox-frame.js","sourceRoot":"","sources":["../src/sandbox-frame.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA6BH,MAAM,UAAU,oBAAoB;IAMlC,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,MAAM,OAAO,GAAG,CAAC,KAAc,EAAU,EAAE;QACzC,IAAI,KAAK,IAAI,IAAI,IAAI,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/mdx",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
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.2",
|
|
16
16
|
"@mdx-js/mdx": "^3.1.1",
|
|
17
17
|
"@mdx-js/react": "^3.1.1",
|
|
18
18
|
"@mdxeditor/editor": "^4.2.3",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"remark-frontmatter": "^5.0.0",
|
|
25
25
|
"remark-gfm": "^4.0.1",
|
|
26
26
|
"remark-mdx": "^3.1.1",
|
|
27
|
-
"remark-mdx-frontmatter": "^
|
|
27
|
+
"remark-mdx-frontmatter": "^6.0.0",
|
|
28
28
|
"remark-parse": "^11.0.0",
|
|
29
29
|
"remark-rehype": "^11.1.2",
|
|
30
30
|
"tslib": "^2.8.1",
|
|
@@ -35,12 +35,12 @@
|
|
|
35
35
|
"react-dom": "^19.0.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@happy-dom/global-registrator": "
|
|
38
|
+
"@happy-dom/global-registrator": "21.0.4",
|
|
39
39
|
"@testing-library/react": "^16.3.3",
|
|
40
40
|
"@types/mdast": "^4.0.4",
|
|
41
41
|
"@types/react": "19.2.18",
|
|
42
42
|
"@types/react-dom": "19.2.7",
|
|
43
|
-
"happy-dom": "
|
|
43
|
+
"happy-dom": "21.0.4",
|
|
44
44
|
"mdast-util-from-markdown": "^2.0.3",
|
|
45
45
|
"mdast-util-gfm": "^3.1.0",
|
|
46
46
|
"micromark-extension-gfm": "^3.0.0",
|
|
@@ -86,9 +86,9 @@
|
|
|
86
86
|
],
|
|
87
87
|
"executor": "@smoothbricks/nx-plugin:bounded-exec",
|
|
88
88
|
"options": {
|
|
89
|
-
"command": "bun --conditions=production
|
|
89
|
+
"command": "bun test --timeout=0 --conditions=production",
|
|
90
90
|
"cwd": "{projectRoot}",
|
|
91
|
-
"
|
|
91
|
+
"idleTimeoutMs": 180000,
|
|
92
92
|
"killAfterMs": 10000
|
|
93
93
|
}
|
|
94
94
|
},
|
package/src/editor-styles.css
CHANGED
|
@@ -25,37 +25,39 @@
|
|
|
25
25
|
/* L0 heading scale. Tailwind preflight sets `h1..h6 { font-size: inherit;
|
|
26
26
|
font-weight: inherit }`, so BlockType → Heading looks identical to a
|
|
27
27
|
paragraph unless something restores real sizes. Use :where() so specificity
|
|
28
|
-
stays (0,0,1) and
|
|
29
|
-
|
|
28
|
+
stays (0,0,1) and host class selectors still win. Host-probe rules sit at
|
|
29
|
+
the same specificity, so head order alone would pick between them; the
|
|
30
|
+
probe also sets `--conloca-host-hN-*` on the heading, which these rules
|
|
31
|
+
read first, so the host's value wins whichever sheet loads last. */
|
|
30
32
|
:where(.conloca-prose--editor) h1 {
|
|
31
|
-
font-size: 2.25em;
|
|
32
|
-
font-weight: 700;
|
|
33
|
-
line-height: 1.2;
|
|
33
|
+
font-size: var(--conloca-host-h1-font-size, 2.25em);
|
|
34
|
+
font-weight: var(--conloca-host-h1-font-weight, 700);
|
|
35
|
+
line-height: var(--conloca-host-h1-line-height, 1.2);
|
|
34
36
|
}
|
|
35
37
|
:where(.conloca-prose--editor) h2 {
|
|
36
|
-
font-size: 1.75em;
|
|
37
|
-
font-weight: 700;
|
|
38
|
-
line-height: 1.25;
|
|
38
|
+
font-size: var(--conloca-host-h2-font-size, 1.75em);
|
|
39
|
+
font-weight: var(--conloca-host-h2-font-weight, 700);
|
|
40
|
+
line-height: var(--conloca-host-h2-line-height, 1.25);
|
|
39
41
|
}
|
|
40
42
|
:where(.conloca-prose--editor) h3 {
|
|
41
|
-
font-size: 1.375em;
|
|
42
|
-
font-weight: 600;
|
|
43
|
-
line-height: 1.3;
|
|
43
|
+
font-size: var(--conloca-host-h3-font-size, 1.375em);
|
|
44
|
+
font-weight: var(--conloca-host-h3-font-weight, 600);
|
|
45
|
+
line-height: var(--conloca-host-h3-line-height, 1.3);
|
|
44
46
|
}
|
|
45
47
|
:where(.conloca-prose--editor) h4 {
|
|
46
|
-
font-size: 1.125em;
|
|
47
|
-
font-weight: 600;
|
|
48
|
-
line-height: 1.35;
|
|
48
|
+
font-size: var(--conloca-host-h4-font-size, 1.125em);
|
|
49
|
+
font-weight: var(--conloca-host-h4-font-weight, 600);
|
|
50
|
+
line-height: var(--conloca-host-h4-line-height, 1.35);
|
|
49
51
|
}
|
|
50
52
|
:where(.conloca-prose--editor) h5 {
|
|
51
|
-
font-size: 1em;
|
|
52
|
-
font-weight: 600;
|
|
53
|
-
line-height: 1.4;
|
|
53
|
+
font-size: var(--conloca-host-h5-font-size, 1em);
|
|
54
|
+
font-weight: var(--conloca-host-h5-font-weight, 600);
|
|
55
|
+
line-height: var(--conloca-host-h5-line-height, 1.4);
|
|
54
56
|
}
|
|
55
57
|
:where(.conloca-prose--editor) h6 {
|
|
56
|
-
font-size: 0.875em;
|
|
57
|
-
font-weight: 600;
|
|
58
|
-
line-height: 1.4;
|
|
58
|
+
font-size: var(--conloca-host-h6-font-size, 0.875em);
|
|
59
|
+
font-weight: var(--conloca-host-h6-font-weight, 600);
|
|
60
|
+
line-height: var(--conloca-host-h6-line-height, 1.4);
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
/* Editor chrome styles only. Prose typography is intentionally NOT
|
package/src/mdx/compile.ts
CHANGED
|
@@ -5,6 +5,7 @@ import remarkGfm from 'remark-gfm';
|
|
|
5
5
|
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
|
|
6
6
|
import type { MDXCompileResult } from '../types.js';
|
|
7
7
|
import { applyHeadingIds, type FragmentHeading, type OutlineTree } from './heading-outline.js';
|
|
8
|
+
import { remarkDropUnrenderedAttributes } from './literal-attributes.js';
|
|
8
9
|
import { remarkRestrictExpressions } from './restrict-expressions.js';
|
|
9
10
|
|
|
10
11
|
export async function compileMDX(content: string): Promise<MDXCompileResult> {
|
|
@@ -30,7 +31,15 @@ export async function compileMDX(content: string): Promise<MDXCompileResult> {
|
|
|
30
31
|
// `import`/`export` parse into `mdxjsEsm` nodes at parse time (so restrict
|
|
31
32
|
// sees and rejects them), while the legitimate `export const frontmatter`
|
|
32
33
|
// that `remarkMdxFrontmatter` injects appears only after restrict has run.
|
|
33
|
-
|
|
34
|
+
// `remarkDropUnrenderedAttributes` drops what the published fragment
|
|
35
|
+
// drops (`style={{…}}`, lists, `null`), so the preview matches it.
|
|
36
|
+
remarkPlugins: [
|
|
37
|
+
remarkFrontmatter,
|
|
38
|
+
remarkRestrictExpressions,
|
|
39
|
+
remarkDropUnrenderedAttributes,
|
|
40
|
+
remarkMdxFrontmatter,
|
|
41
|
+
remarkGfm,
|
|
42
|
+
],
|
|
34
43
|
rehypePlugins: [collectOutline],
|
|
35
44
|
});
|
|
36
45
|
|
package/src/mdx/evaluate.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
2
|
-
import type { Root } from 'mdast';
|
|
2
|
+
import type { Parent, Root, RootContent } from 'mdast';
|
|
3
3
|
import type { ComponentType } from 'react';
|
|
4
4
|
import { Fragment, jsx, jsxs } from 'react/jsx-runtime';
|
|
5
5
|
import remarkFrontmatter from 'remark-frontmatter';
|
|
@@ -9,6 +9,7 @@ import remarkParse from 'remark-parse';
|
|
|
9
9
|
import remarkRehype from 'remark-rehype';
|
|
10
10
|
import { type Plugin, unified } from 'unified';
|
|
11
11
|
import { applyHeadingIds, type FragmentHeading } from './heading-outline.js';
|
|
12
|
+
import { literalAttributeValue } from './literal-attributes.js';
|
|
12
13
|
import { remarkRestrictExpressions } from './restrict-expressions.js';
|
|
13
14
|
|
|
14
15
|
interface JsxAttributeNode {
|
|
@@ -34,10 +35,12 @@ interface TreeNode {
|
|
|
34
35
|
* JSON-literal `{…}` attribute values (the only expressions
|
|
35
36
|
* `remarkRestrictExpressions` admits) are resolved into plain static values,
|
|
36
37
|
* so rendering never needs an expression evaluator: strings pass through,
|
|
37
|
-
* numbers stringify (identical SSR output), `{true}`
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
38
|
+
* numbers stringify (identical SSR output), and `{true}`/`{false}` reach React
|
|
39
|
+
* as booleans, which it renders as it renders any boolean prop: `{false}`
|
|
40
|
+
* leaves out `disabled` but writes `aria-hidden="false"` and `data-open="false"`.
|
|
41
|
+
* `null` and object literals drop the attribute. The converter reads an object
|
|
42
|
+
* value as an expression to run, which this path never does, so `style={{…}}`
|
|
43
|
+
* on an HTML element is dropped too.
|
|
41
44
|
*/
|
|
42
45
|
function resolveStaticJsx() {
|
|
43
46
|
return (tree: TreeNode): void => {
|
|
@@ -55,17 +58,10 @@ function resolveStaticJsx() {
|
|
|
55
58
|
const value = attr?.value;
|
|
56
59
|
if (!value || typeof value !== 'object') return [attr];
|
|
57
60
|
const raw = (value as { value?: unknown }).value;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
} catch {
|
|
63
|
-
return []; // non-literal — already dropped by the restrict gate; belt and braces
|
|
64
|
-
}
|
|
65
|
-
if (typeof parsed === 'string') return [{ ...attr, value: parsed }];
|
|
66
|
-
if (typeof parsed === 'number') return [{ ...attr, value: String(parsed) }];
|
|
67
|
-
if (parsed === true) return [{ ...attr, value: null }];
|
|
68
|
-
return [];
|
|
61
|
+
// The same rule compileMDX applies, so the editor preview drops what this drops.
|
|
62
|
+
const literal = typeof raw === 'string' ? literalAttributeValue(raw) : undefined;
|
|
63
|
+
if (literal === undefined) return [];
|
|
64
|
+
return [{ ...attr, value: typeof literal === 'number' ? String(literal) : literal }];
|
|
69
65
|
});
|
|
70
66
|
}
|
|
71
67
|
if (Array.isArray(node.children)) node.children.forEach(walk);
|
|
@@ -74,6 +70,39 @@ function resolveStaticJsx() {
|
|
|
74
70
|
};
|
|
75
71
|
}
|
|
76
72
|
|
|
73
|
+
/** Nothing, or only the HTML whitespace a line break or a space between two tags leaves. */
|
|
74
|
+
const BLANK = /^[\t\n\f\r ]*$/;
|
|
75
|
+
|
|
76
|
+
/** A JSX element standing on its own as a block, like `<div>` written over several lines. */
|
|
77
|
+
type JsxBlock = Extract<RootContent, { type: 'mdxJsxFlowElement' }>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Lift HTML tags out of a paragraph that holds nothing else. Markdown reads a tag written on one line,
|
|
81
|
+
* `<div>x</div>`, as the start of a paragraph, and `<p><div>x</div></p>` is HTML a browser splits into
|
|
82
|
+
* an empty paragraph and a loose `<div>`. The `@mdx-js/mdx` compiler this pipeline replaced does the
|
|
83
|
+
* same lifting (its `remarkMarkAndUnravel`); a paragraph with any text beside its tags keeps its `<p>`.
|
|
84
|
+
*/
|
|
85
|
+
function unravelJsxParagraphs() {
|
|
86
|
+
const lift = (node: Parent): void => {
|
|
87
|
+
node.children = node.children.flatMap((child): RootContent[] => {
|
|
88
|
+
if (child.type !== 'paragraph') return [child];
|
|
89
|
+
const tags = child.children.filter((inline) => inline.type === 'mdxJsxTextElement');
|
|
90
|
+
const onlyTags = child.children.every(
|
|
91
|
+
(inline) => inline.type === 'mdxJsxTextElement' || (inline.type === 'text' && BLANK.test(inline.value)),
|
|
92
|
+
);
|
|
93
|
+
if (tags.length === 0 || !onlyTags) return [child];
|
|
94
|
+
// A tag's inline content stays as it is: written on one line, it never held a block.
|
|
95
|
+
return tags.map(
|
|
96
|
+
(tag): JsxBlock => ({ ...tag, type: 'mdxJsxFlowElement', children: tag.children as JsxBlock['children'] }),
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
for (const child of node.children) {
|
|
100
|
+
if ('children' in child) lift(child);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
return (tree: Root): void => lift(tree);
|
|
104
|
+
}
|
|
105
|
+
|
|
77
106
|
/**
|
|
78
107
|
* Markdown → mdast (with MDX JSX syntax) → hast, with the JSX element nodes
|
|
79
108
|
* passed through for `toJsxRuntime` to turn into React elements directly.
|
|
@@ -88,6 +117,7 @@ const processor = unified()
|
|
|
88
117
|
// unified's strict Plugin shape here rather than forking their signatures.
|
|
89
118
|
.use(remarkRestrictExpressions as unknown as Plugin<[], Root>)
|
|
90
119
|
.use(resolveStaticJsx as unknown as Plugin<[], Root>)
|
|
120
|
+
.use(unravelJsxParagraphs)
|
|
91
121
|
.use(remarkRehype, { passThrough: ['mdxJsxFlowElement', 'mdxJsxTextElement'] });
|
|
92
122
|
|
|
93
123
|
/**
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
interface JsxAttributeNode {
|
|
2
|
+
type?: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
value?: unknown;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface TreeNode {
|
|
8
|
+
type?: string;
|
|
9
|
+
children?: TreeNode[];
|
|
10
|
+
attributes?: JsxAttributeNode[];
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The value a JSON-literal attribute expression (`{3}`, `{true}`, `{"x"}`) has
|
|
16
|
+
* on a published fragment, or undefined when the attribute renders as nothing
|
|
17
|
+
* there: an object (`style={{…}}`), a list, `null`, or anything not JSON.
|
|
18
|
+
*/
|
|
19
|
+
export function literalAttributeValue(expression: string): string | number | boolean | undefined {
|
|
20
|
+
let parsed: unknown;
|
|
21
|
+
try {
|
|
22
|
+
parsed = JSON.parse(expression.trim());
|
|
23
|
+
} catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
return typeof parsed === 'string' || typeof parsed === 'number' || typeof parsed === 'boolean' ? parsed : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Remark transform that drops every JSX attribute whose `{…}` value renders as
|
|
31
|
+
* nothing on a published fragment, so a compiled preview shows what will be
|
|
32
|
+
* published. It runs on the markdown tree, before the compiler writes the
|
|
33
|
+
* style objects that align table columns, so those are kept.
|
|
34
|
+
*/
|
|
35
|
+
export function remarkDropUnrenderedAttributes() {
|
|
36
|
+
return (tree: TreeNode): void => {
|
|
37
|
+
const walk = (node: TreeNode): void => {
|
|
38
|
+
if (Array.isArray(node.attributes)) {
|
|
39
|
+
node.attributes = node.attributes.filter((attr) => {
|
|
40
|
+
const value = attr?.value;
|
|
41
|
+
if (!value || typeof value !== 'object') return true;
|
|
42
|
+
const expression = (value as { value?: unknown }).value;
|
|
43
|
+
return typeof expression === 'string' && literalAttributeValue(expression) !== undefined;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(node.children)) node.children.forEach(walk);
|
|
47
|
+
};
|
|
48
|
+
walk(tree);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -64,6 +64,15 @@ function isDangerousElement(node: MdastNode): boolean {
|
|
|
64
64
|
return /^[a-z]/.test(name) && DANGEROUS_ELEMENTS.has(name.toLowerCase());
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/** `{/* … *\/}`, the way MDX writes a comment: the parsed program has no
|
|
68
|
+
* statements, so nothing in it can run. Without the parsed program it is
|
|
69
|
+
* not trusted to be one. */
|
|
70
|
+
function isComment(node: MdastNode): boolean {
|
|
71
|
+
if (node?.type !== 'mdxFlowExpression' && node?.type !== 'mdxTextExpression') return false;
|
|
72
|
+
const program = (node.data as { estree?: { body?: unknown } } | undefined)?.estree;
|
|
73
|
+
return Array.isArray(program?.body) && program.body.length === 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
67
76
|
/** A JSON-literal expression whose parsed value is a dangerous-scheme URL string
|
|
68
77
|
* (`href={"data:text/html,…"}` round-trips through `JSON.parse` yet navigates). */
|
|
69
78
|
function isDangerousLiteralUrl(value: unknown): boolean {
|
|
@@ -123,6 +132,8 @@ function sanitizeAttributes(attributes: MdastAttribute[]): MdastAttribute[] {
|
|
|
123
132
|
* `mdxTextExpression`) and top-level ESM `import`/`export` (`mdxjsEsm`) reach
|
|
124
133
|
* `new AsyncFunction`/`evaluate()` as live code, so they fail the compile
|
|
125
134
|
* with a clear error; content fragments never legitimately need either.
|
|
135
|
+
* A comment, `{/* … *\/}`, holds no code and is removed instead, so it
|
|
136
|
+
* renders as nothing, as it does in MDX.
|
|
126
137
|
* 2. HTML injection — `dangerouslySetInnerHTML` (React's raw-HTML escape hatch,
|
|
127
138
|
* which a JSON-literal object slips past the expression exemption), inline
|
|
128
139
|
* `on*` handlers, dangerous URL schemes (`javascript:`/`vbscript:`/
|
|
@@ -164,7 +175,7 @@ export function remarkRestrictExpressions() {
|
|
|
164
175
|
}
|
|
165
176
|
if (Array.isArray(node.attributes)) node.attributes = sanitizeAttributes(node.attributes);
|
|
166
177
|
if (Array.isArray(node.children)) {
|
|
167
|
-
node.children = node.children.filter((child) => !isDangerousElement(child));
|
|
178
|
+
node.children = node.children.filter((child) => !isDangerousElement(child) && !isComment(child));
|
|
168
179
|
node.children.forEach(walk);
|
|
169
180
|
}
|
|
170
181
|
};
|
package/src/sandbox-frame.ts
CHANGED
|
@@ -38,7 +38,14 @@ export function createSandboxRuntime(): {
|
|
|
38
38
|
jsxs: (type: unknown, props: Record<string, unknown>) => Node;
|
|
39
39
|
jsxDEV: (type: unknown, props: Record<string, unknown>) => Node;
|
|
40
40
|
} {
|
|
41
|
+
// Everything below stays inside this function: the frame receives it as source text.
|
|
41
42
|
const Fragment = Symbol('sandbox.fragment');
|
|
43
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
44
|
+
// A child is built before its parent, so an element written inside <svg>
|
|
45
|
+
// starts out as HTML, which a browser does not draw there. The <svg> builds
|
|
46
|
+
// it again as SVG from what each HTML element was made of, kept here, which
|
|
47
|
+
// is the namespace React would have given it.
|
|
48
|
+
const madeOf = new WeakMap<Node, { type: string; props: Record<string, unknown> }>();
|
|
42
49
|
const toNodes = (value: unknown): Node[] => {
|
|
43
50
|
if (value == null || value === true || value === false) return [];
|
|
44
51
|
if (Array.isArray(value)) return value.flatMap(toNodes);
|
|
@@ -46,6 +53,34 @@ export function createSandboxRuntime(): {
|
|
|
46
53
|
if (value instanceof Node) return [value];
|
|
47
54
|
return [document.createTextNode(String(value))];
|
|
48
55
|
};
|
|
56
|
+
const element = (type: string, p: Record<string, unknown>, inSvg: boolean): Element => {
|
|
57
|
+
const svg = inSvg || type === 'svg';
|
|
58
|
+
const el = svg ? document.createElementNS(SVG_NS, type) : document.createElement(type);
|
|
59
|
+
if (!svg) madeOf.set(el, { type, props: p });
|
|
60
|
+
for (const key in p) {
|
|
61
|
+
if (key === 'children' || key === 'key') continue;
|
|
62
|
+
const value = p[key];
|
|
63
|
+
if (/^on/i.test(key)) continue; // never wire an event handler
|
|
64
|
+
if (key === 'className') {
|
|
65
|
+
el.setAttribute('class', String(value));
|
|
66
|
+
} else if (key === 'htmlFor') {
|
|
67
|
+
el.setAttribute('for', String(value));
|
|
68
|
+
} else if (key === 'style' && value && typeof value === 'object') {
|
|
69
|
+
Object.assign((el as HTMLElement).style, value as Record<string, string>);
|
|
70
|
+
} else if (value === true) {
|
|
71
|
+
el.setAttribute(key, '');
|
|
72
|
+
} else if (value != null && value !== false) {
|
|
73
|
+
el.setAttribute(key, String(value));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// What a <foreignObject> holds is HTML again, as in a browser.
|
|
77
|
+
const svgChildren = svg && type !== 'foreignObject';
|
|
78
|
+
for (const child of toNodes(p.children)) {
|
|
79
|
+
const source = svgChildren ? madeOf.get(child) : undefined;
|
|
80
|
+
el.appendChild(source ? element(source.type, source.props, true) : child);
|
|
81
|
+
}
|
|
82
|
+
return el;
|
|
83
|
+
};
|
|
49
84
|
const jsx = (type: unknown, props: Record<string, unknown>): Node => {
|
|
50
85
|
const p = props || {};
|
|
51
86
|
if (type === Fragment) {
|
|
@@ -54,25 +89,7 @@ export function createSandboxRuntime(): {
|
|
|
54
89
|
return frag;
|
|
55
90
|
}
|
|
56
91
|
if (typeof type === 'function') return (type as (props: Record<string, unknown>) => Node)(p);
|
|
57
|
-
if (typeof type === 'string')
|
|
58
|
-
const el = document.createElement(type);
|
|
59
|
-
for (const key in p) {
|
|
60
|
-
if (key === 'children' || key === 'key') continue;
|
|
61
|
-
const value = p[key];
|
|
62
|
-
if (/^on/i.test(key)) continue; // never wire an event handler
|
|
63
|
-
if (key === 'className') {
|
|
64
|
-
el.setAttribute('class', String(value));
|
|
65
|
-
} else if (key === 'style' && value && typeof value === 'object') {
|
|
66
|
-
Object.assign((el as HTMLElement).style, value as Record<string, string>);
|
|
67
|
-
} else if (value === true) {
|
|
68
|
-
el.setAttribute(key, '');
|
|
69
|
-
} else if (value != null && value !== false) {
|
|
70
|
-
el.setAttribute(key, String(value));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
for (const child of toNodes(p.children)) el.appendChild(child);
|
|
74
|
-
return el;
|
|
75
|
-
}
|
|
92
|
+
if (typeof type === 'string') return element(type, p, false);
|
|
76
93
|
const missing = document.createElement('span');
|
|
77
94
|
missing.setAttribute('data-sandbox-unknown', '');
|
|
78
95
|
return missing;
|