@conloca/mdx 0.1.16 → 0.2.1

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.
Files changed (91) hide show
  1. package/README.md +50 -16
  2. package/dist/code-block-frame.js +1 -0
  3. package/dist/code-block-frame.js.map +1 -0
  4. package/dist/editor-core.d.ts +0 -2
  5. package/dist/editor-core.d.ts.map +1 -1
  6. package/dist/editor-core.js +50 -6
  7. package/dist/editor-core.js.map +1 -0
  8. package/dist/esm-passthrough.d.ts +36 -0
  9. package/dist/esm-passthrough.d.ts.map +1 -0
  10. package/dist/esm-passthrough.js +237 -0
  11. package/dist/esm-passthrough.js.map +1 -0
  12. package/dist/fragments.d.ts +37 -0
  13. package/dist/fragments.d.ts.map +1 -0
  14. package/dist/fragments.js +53 -0
  15. package/dist/fragments.js.map +1 -0
  16. package/dist/index.d.ts +0 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/index.js.map +1 -0
  20. package/dist/markdown-options.js +1 -0
  21. package/dist/markdown-options.js.map +1 -0
  22. package/dist/mdx/compile-request.d.ts +5 -0
  23. package/dist/mdx/compile-request.d.ts.map +1 -0
  24. package/dist/mdx/compile-request.js +40 -0
  25. package/dist/mdx/compile-request.js.map +1 -0
  26. package/dist/mdx/compile.d.ts.map +1 -1
  27. package/dist/mdx/compile.js +30 -1
  28. package/dist/mdx/compile.js.map +1 -0
  29. package/dist/mdx/evaluate.d.ts +18 -23
  30. package/dist/mdx/evaluate.d.ts.map +1 -1
  31. package/dist/mdx/evaluate.js +123 -29
  32. package/dist/mdx/evaluate.js.map +1 -0
  33. package/dist/mdx/heading-outline.d.ts +46 -0
  34. package/dist/mdx/heading-outline.d.ts.map +1 -0
  35. package/dist/mdx/heading-outline.js +114 -0
  36. package/dist/mdx/heading-outline.js.map +1 -0
  37. package/dist/mdx/literal-attributes.d.ts +26 -0
  38. package/dist/mdx/literal-attributes.d.ts.map +1 -0
  39. package/dist/mdx/literal-attributes.js +40 -0
  40. package/dist/mdx/literal-attributes.js.map +1 -0
  41. package/dist/mdx/restrict-expressions.d.ts +45 -0
  42. package/dist/mdx/restrict-expressions.d.ts.map +1 -0
  43. package/dist/mdx/restrict-expressions.js +171 -0
  44. package/dist/mdx/restrict-expressions.js.map +1 -0
  45. package/dist/mdx-editor.js +1 -0
  46. package/dist/mdx-editor.js.map +1 -0
  47. package/dist/node.d.ts +3 -2
  48. package/dist/node.d.ts.map +1 -1
  49. package/dist/node.js +3 -1
  50. package/dist/node.js.map +1 -0
  51. package/dist/sandbox-frame.d.ts +114 -0
  52. package/dist/sandbox-frame.d.ts.map +1 -0
  53. package/dist/sandbox-frame.js +253 -0
  54. package/dist/sandbox-frame.js.map +1 -0
  55. package/dist/types.d.ts +7 -4
  56. package/dist/types.d.ts.map +1 -1
  57. package/dist/types.js +1 -0
  58. package/dist/types.js.map +1 -0
  59. package/package.json +58 -41
  60. package/src/code-block-frame.tsx +262 -0
  61. package/src/editor-core.tsx +403 -0
  62. package/src/editor-styles.css +282 -0
  63. package/src/esm-passthrough.ts +269 -0
  64. package/src/fragments.ts +91 -0
  65. package/src/index.ts +5 -0
  66. package/src/markdown-options.ts +21 -0
  67. package/src/mdx/compile-request.ts +53 -0
  68. package/src/mdx/compile.ts +60 -0
  69. package/src/mdx/evaluate.ts +174 -0
  70. package/src/mdx/heading-outline.ts +153 -0
  71. package/src/mdx/literal-attributes.ts +50 -0
  72. package/src/mdx/restrict-expressions.ts +184 -0
  73. package/src/mdx-editor.tsx +15 -0
  74. package/src/node.ts +25 -0
  75. package/src/sandbox-frame.ts +294 -0
  76. package/src/types.ts +23 -0
  77. package/dist/blocks.d.ts +0 -37
  78. package/dist/blocks.d.ts.map +0 -1
  79. package/dist/blocks.js +0 -51
  80. package/dist/hooks/useMDXEvaluation.d.ts +0 -28
  81. package/dist/hooks/useMDXEvaluation.d.ts.map +0 -1
  82. package/dist/hooks/useMDXEvaluation.js +0 -117
  83. package/dist/index.d.mts +0 -127
  84. package/dist/index.d.mts.map +0 -1
  85. package/dist/index.mjs +0 -516
  86. package/dist/index.mjs.map +0 -1
  87. package/dist/node.d.mts +0 -102
  88. package/dist/node.d.mts.map +0 -1
  89. package/dist/node.mjs +0 -140
  90. package/dist/node.mjs.map +0 -1
  91. package/dist/style.css +0 -88
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fragments.js","sourceRoot":"","sources":["../src/fragments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAgC3D;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,GAAgB,EAAE,MAAc;IACzE,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC;IAEvE,OAAO,OAAO,CAAC,GAAG,CAChB,SAAS,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE;QAC/B,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC9C,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAC7E,MAAM,aAAa,GAAG,WAAW,EAAE,IAAI,EAAE,KAAK,IAAI,WAAW,EAAE,IAAI,IAAI,QAAQ,CAAC,EAAE,CAAC;QAEnF,IAAI,CAAC;YACH,MAAM,iBAAiB,GAAG,MAAM,GAAG,CAAC,YAAY,CAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YACtE,MAAM,aAAa,GACjB,iBAAiB,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,IAAI,iBAAiB,EAAE,SAAS,EAAE,IAAI,IAAI,aAAa,CAAC;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ethods } from '@mdxeditor/editor';
2
2
  export { codeBlockWrapperInfo$ } from './code-block-frame.js';
3
3
  export type { BaseMDXEditorProps } from './editor-core.js';
4
4
  export { BaseMDXEditor } from './editor-core.js';
5
- export type { UseMDXEvaluationOptions, UseMDXEvaluationResult } from './hooks/useMDXEvaluation.js';
6
- export { useMDXEvaluation } from './hooks/useMDXEvaluation.js';
7
5
  export * from './mdx-editor.js';
8
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,YAAY,EAAE,uBAAuB,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACnG,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,cAAc,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,cAAc,iBAAiB,CAAC"}
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { codeBlockWrapperInfo$ } from './code-block-frame.js';
2
2
  export { BaseMDXEditor } from './editor-core.js';
3
- export { useMDXEvaluation } from './hooks/useMDXEvaluation.js';
4
3
  export * from './mdx-editor.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,cAAc,iBAAiB,CAAC"}
@@ -17,3 +17,4 @@ export const TO_MARKDOWN_OPTIONS = {
17
17
  listItemIndent: 'one',
18
18
  tightDefinitions: true,
19
19
  };
20
+ //# sourceMappingURL=markdown-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdown-options.js","sourceRoot":"","sources":["../src/markdown-options.ts"],"names":[],"mappings":"AAEA,4EAA4E;AAC5E,+EAA+E;AAC/E,2EAA2E;AAC3E,4EAA4E;AAC5E,4EAA4E;AAC5E,yDAAyD;AACzD,EAAE;AACF,uEAAuE;AACvE,4EAA4E;AAC5E,UAAU;AACV,MAAM,CAAC,MAAM,mBAAmB,GAAsB;IACpD,MAAM,EAAE,GAAG;IACX,IAAI,EAAE,GAAG;IACT,QAAQ,EAAE,GAAG;IACb,MAAM,EAAE,GAAG;IACX,KAAK,EAAE,GAAG;IACV,cAAc,EAAE,KAAK;IACrB,gBAAgB,EAAE,IAAI;CACvB,CAAC"}
@@ -0,0 +1,5 @@
1
+ /** Answers a compile request with the whole compile result, the one implementation every host mounts. */
2
+ export declare function handleMDXCompileRequest(request: Request, options?: {
3
+ maxChars?: number;
4
+ }): Promise<Response>;
5
+ //# sourceMappingURL=compile-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-request.d.ts","sourceRoot":"","sources":["../../src/mdx/compile-request.ts"],"names":[],"mappings":"AAGA,yGAAyG;AACzG,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAO,GAClC,OAAO,CAAC,QAAQ,CAAC,CA6CnB"}
@@ -0,0 +1,40 @@
1
+ import { ErrorCodes } from '@conloca/content-api';
2
+ import { compileMDX } from './compile.js';
3
+ /** Answers a compile request with the whole compile result, the one implementation every host mounts. */
4
+ export async function handleMDXCompileRequest(request, options = {}) {
5
+ let body;
6
+ try {
7
+ body = await request.json();
8
+ }
9
+ catch {
10
+ return Response.json({ error: { code: ErrorCodes.INVALID_REQUEST, message: 'Invalid JSON body' } }, { status: 400 });
11
+ }
12
+ const mdxContent = typeof body === 'object' && body !== null && 'mdxContent' in body ? body.mdxContent : null;
13
+ if (!mdxContent || typeof mdxContent !== 'string') {
14
+ return Response.json({
15
+ error: {
16
+ code: ErrorCodes.MISSING_REQUIRED_FIELD,
17
+ message: 'mdxContent string required',
18
+ details: { field: 'mdxContent' },
19
+ },
20
+ }, { status: 400 });
21
+ }
22
+ const { maxChars } = options;
23
+ if (maxChars !== undefined && mdxContent.length > maxChars) {
24
+ return Response.json({
25
+ error: {
26
+ code: ErrorCodes.INVALID_REQUEST,
27
+ message: `mdxContent exceeds size limit (${mdxContent.length} > ${maxChars} characters)`,
28
+ },
29
+ }, { status: 413 });
30
+ }
31
+ try {
32
+ return Response.json(await compileMDX(mdxContent));
33
+ }
34
+ catch (error) {
35
+ const message = error instanceof Error ? error.message : 'Failed to compile MDX';
36
+ console.error(`[CMS] ${new URL(request.url).pathname} failed:`, error);
37
+ return Response.json({ error: { code: ErrorCodes.MDX_COMPILE_FAILED, message } }, { status: 422 });
38
+ }
39
+ }
40
+ //# sourceMappingURL=compile-request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-request.js","sourceRoot":"","sources":["../../src/mdx/compile-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,yGAAyG;AACzG,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,OAAgB,EAChB,OAAO,GAA0B,EAAE;IAEnC,IAAI,IAAa,CAAC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,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,uBAAuB,CAAC;QACjF,OAAO,CAAC,KAAK,CAAC,SAAS,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,UAAU,EAAE,KAAK,CAAC,CAAC;QACvE,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,UAAU,CAAC,kBAAkB,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;IACrG,CAAC;AACH,CAAC"}
@@ -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;AAEpD,wBAAsB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAuB3E"}
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"}
@@ -3,17 +3,45 @@ import matter from 'gray-matter';
3
3
  import remarkFrontmatter from 'remark-frontmatter';
4
4
  import remarkGfm from 'remark-gfm';
5
5
  import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
6
+ import { applyHeadingIds } from './heading-outline.js';
7
+ import { remarkDropUnrenderedAttributes } from './literal-attributes.js';
8
+ import { remarkRestrictExpressions } from './restrict-expressions.js';
6
9
  export async function compileMDX(content) {
7
10
  try {
11
+ // Empty options opt out of gray-matter's module-level memo cache — a plain
12
+ // object keyed by the raw input, so inputs naming an Object.prototype
13
+ // member ('valueOf', 'toString', …) return the inherited member instead of
14
+ // a parse.
8
15
  const { data: metadata } = matter(content, {});
16
+ // The editing surfaces read their outline off this compile rather than off
17
+ // the rendered frame, which they cannot see into: the fragment body renders
18
+ // in a sandbox with no same-origin access.
19
+ let outline = [];
20
+ const collectOutline = () => (tree) => {
21
+ outline = applyHeadingIds(tree);
22
+ };
9
23
  const compiled = await mdxCompile(content, {
10
24
  outputFormat: 'function-body',
11
25
  development: process.env.NODE_ENV === 'development',
12
- remarkPlugins: [remarkFrontmatter, remarkMdxFrontmatter, remarkGfm],
26
+ // `remarkRestrictExpressions` runs BEFORE `remarkMdxFrontmatter`: author
27
+ // `import`/`export` parse into `mdxjsEsm` nodes at parse time (so restrict
28
+ // sees and rejects them), while the legitimate `export const frontmatter`
29
+ // that `remarkMdxFrontmatter` injects appears only after restrict has run.
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
+ ],
39
+ rehypePlugins: [collectOutline],
13
40
  });
14
41
  return {
15
42
  code: String(compiled.value),
16
43
  metadata: metadata,
44
+ outline,
17
45
  };
18
46
  }
19
47
  catch (error) {
@@ -25,3 +53,4 @@ export async function compileMDX(content) {
25
53
  throw error;
26
54
  }
27
55
  }
56
+ //# sourceMappingURL=compile.js.map
@@ -0,0 +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,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,IAAI,KAAK,CAAC,2BAA2B,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5E,aAAa,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;YAClC,MAAM,aAAa,CAAC;QACtB,CAAC;QAED,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -1,20 +1,23 @@
1
1
  import type { ComponentType } from 'react';
2
+ import { type FragmentHeading } from './heading-outline.js';
2
3
  /**
3
- * Evaluate MDX content string to a React component.
4
+ * Turn an MDX content string into a React component WITHOUT executing any
5
+ * compiled code.
4
6
  *
5
- * This is a framework-agnostic utility for converting MDX strings into
6
- * executable React components. Works in any Node.js environment (Astro,
7
- * Next.js, Remix, etc.) at build time or server-side.
7
+ * This is the published-site render path for CMS fragments, and it must run
8
+ * where creating code from strings is forbidden, such as Cloudflare Workers —
9
+ * `@mdx-js/mdx`'s `evaluate()` (a `new Function` under the hood) throws
10
+ * `EvalError: Code generation from strings disallowed` there. So instead of
11
+ * compile-and-execute, this parses the MDX to a syntax tree and converts the
12
+ * tree straight to React elements (`hast-util-to-jsx-runtime`). That is
13
+ * sufficient by construction: `remarkRestrictExpressions` already rejects
14
+ * `{…}` expressions and top-level `import`/`export`, and only JSON-literal
15
+ * attribute values survive — nothing in admissible content needs code
16
+ * execution to render.
8
17
  *
9
- * **When to use:**
10
- * - Building static sites with MDX content
11
- * - Server-side rendering MDX as React components
12
- * - Need React components (not HTML strings)
13
- *
14
- * @param mdxContent - The MDX string to evaluate
15
- * @param options - Optional configuration
16
- * @param options.development - Whether to use development mode (default: false)
17
- * @returns Object containing Component (or null) and optional error
18
+ * Unregistered capitalized JSX components fail here (evaluate time) rather
19
+ * than when React renders the component, so callers get the error result and
20
+ * can show their fragment-error UI instead of failing the whole page render.
18
21
  *
19
22
  * @example
20
23
  * ```typescript
@@ -27,18 +30,10 @@ import type { ComponentType } from 'react';
27
30
  * // Render <Component /> in your React app
28
31
  * }
29
32
  * ```
30
- *
31
- * @example
32
- * ```typescript
33
- * // In Astro pages
34
- * const { Component } = await evaluateMDXToComponent(mdxContent);
35
- * // Then render in JSX: <Component />
36
- * ```
37
33
  */
38
- export declare function evaluateMDXToComponent(mdxContent: string, options?: {
39
- development?: boolean;
40
- }): Promise<{
34
+ export declare function evaluateMDXToComponent(mdxContent: string): Promise<{
41
35
  Component: ComponentType | null;
42
36
  error?: Error;
37
+ outline: readonly FragmentHeading[];
43
38
  }>;
44
39
  //# sourceMappingURL=evaluate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../../src/mdx/evaluate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAM3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAsB,sBAAsB,CAC1C,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,OAAO,CAAA;CAAE,GAClC,OAAO,CAAC;IAAE,SAAS,EAAE,aAAa,GAAG,IAAI,CAAC;IAAC,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC,CAe7D"}
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"}
@@ -1,24 +1,120 @@
1
- import { evaluate } from '@mdx-js/mdx';
2
- import * as runtime from 'react/jsx-runtime';
1
+ import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
2
+ import { Fragment, jsx, jsxs } from 'react/jsx-runtime';
3
3
  import remarkFrontmatter from 'remark-frontmatter';
4
4
  import remarkGfm from 'remark-gfm';
5
- import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
5
+ import remarkMdx from 'remark-mdx';
6
+ import remarkParse from 'remark-parse';
7
+ import remarkRehype from 'remark-rehype';
8
+ import { unified } from 'unified';
9
+ import { applyHeadingIds } from './heading-outline.js';
10
+ import { literalAttributeValue } from './literal-attributes.js';
11
+ import { remarkRestrictExpressions } from './restrict-expressions.js';
6
12
  /**
7
- * Evaluate MDX content string to a React component.
13
+ * Make every JSX element in the tree statically renderable.
8
14
  *
9
- * This is a framework-agnostic utility for converting MDX strings into
10
- * executable React components. Works in any Node.js environment (Astro,
11
- * Next.js, Remix, etc.) at build time or server-side.
15
+ * Capitalized component names are rejected with a clear author-facing error —
16
+ * this path has no component registry to resolve them from, and letting them
17
+ * through surfaces as a cryptic converter error instead.
12
18
  *
13
- * **When to use:**
14
- * - Building static sites with MDX content
15
- * - Server-side rendering MDX as React components
16
- * - Need React components (not HTML strings)
19
+ * JSON-literal `{…}` attribute values (the only expressions
20
+ * `remarkRestrictExpressions` admits) are resolved into plain static values,
21
+ * so rendering never needs an expression evaluator: strings pass through,
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.
28
+ */
29
+ function resolveStaticJsx() {
30
+ return (tree) => {
31
+ const walk = (node) => {
32
+ if (node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') {
33
+ const name = typeof node.name === 'string' ? node.name : '';
34
+ if (/^[A-Z]/.test(name)) {
35
+ throw new Error(`Unknown component <${name}>: fragments render as plain content and cannot use React components.`);
36
+ }
37
+ }
38
+ if (Array.isArray(node.attributes)) {
39
+ node.attributes = node.attributes.flatMap((attr) => {
40
+ const value = attr?.value;
41
+ if (!value || typeof value !== 'object')
42
+ return [attr];
43
+ const raw = value.value;
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)
47
+ return [];
48
+ return [{ ...attr, value: typeof literal === 'number' ? String(literal) : literal }];
49
+ });
50
+ }
51
+ if (Array.isArray(node.children))
52
+ node.children.forEach(walk);
53
+ };
54
+ walk(tree);
55
+ };
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
+ }
84
+ /**
85
+ * Markdown → mdast (with MDX JSX syntax) → hast, with the JSX element nodes
86
+ * passed through for `toJsxRuntime` to turn into React elements directly.
87
+ */
88
+ const processor = unified()
89
+ .use(remarkParse)
90
+ .use(remarkMdx)
91
+ .use(remarkFrontmatter)
92
+ .use(remarkGfm)
93
+ // Both transformers use loose structural node types (they predate this
94
+ // pipeline and also run inside @mdx-js's own plugin list), so bridge them to
95
+ // unified's strict Plugin shape here rather than forking their signatures.
96
+ .use(remarkRestrictExpressions)
97
+ .use(resolveStaticJsx)
98
+ .use(unravelJsxParagraphs)
99
+ .use(remarkRehype, { passThrough: ['mdxJsxFlowElement', 'mdxJsxTextElement'] });
100
+ /**
101
+ * Turn an MDX content string into a React component WITHOUT executing any
102
+ * compiled code.
17
103
  *
18
- * @param mdxContent - The MDX string to evaluate
19
- * @param options - Optional configuration
20
- * @param options.development - Whether to use development mode (default: false)
21
- * @returns Object containing Component (or null) and optional error
104
+ * This is the published-site render path for CMS fragments, and it must run
105
+ * where creating code from strings is forbidden, such as Cloudflare Workers —
106
+ * `@mdx-js/mdx`'s `evaluate()` (a `new Function` under the hood) throws
107
+ * `EvalError: Code generation from strings disallowed` there. So instead of
108
+ * compile-and-execute, this parses the MDX to a syntax tree and converts the
109
+ * tree straight to React elements (`hast-util-to-jsx-runtime`). That is
110
+ * sufficient by construction: `remarkRestrictExpressions` already rejects
111
+ * `{…}` expressions and top-level `import`/`export`, and only JSON-literal
112
+ * attribute values survive — nothing in admissible content needs code
113
+ * execution to render.
114
+ *
115
+ * Unregistered capitalized JSX components fail here (evaluate time) rather
116
+ * than when React renders the component, so callers get the error result and
117
+ * can show their fragment-error UI instead of failing the whole page render.
22
118
  *
23
119
  * @example
24
120
  * ```typescript
@@ -31,27 +127,25 @@ import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
31
127
  * // Render <Component /> in your React app
32
128
  * }
33
129
  * ```
34
- *
35
- * @example
36
- * ```typescript
37
- * // In Astro pages
38
- * const { Component } = await evaluateMDXToComponent(mdxContent);
39
- * // Then render in JSX: <Component />
40
- * ```
41
130
  */
42
- export async function evaluateMDXToComponent(mdxContent, options) {
131
+ export async function evaluateMDXToComponent(mdxContent) {
43
132
  try {
44
- const { default: MDXComponent } = await evaluate(mdxContent, {
45
- ...runtime,
46
- remarkPlugins: [remarkFrontmatter, remarkMdxFrontmatter, remarkGfm],
47
- development: options?.development ?? false,
48
- });
49
- return { Component: MDXComponent };
133
+ const mdast = processor.parse(mdxContent);
134
+ const hast = await processor.run(mdast);
135
+ // Before the tree becomes React: assigning the ids and reading the outline
136
+ // in one pass is what keeps a contents link and its heading in agreement.
137
+ const outline = applyHeadingIds(hast);
138
+ // Convert eagerly so bad content (e.g. an unknown component) surfaces as
139
+ // the error result; the returned component just replays the static tree.
140
+ const element = toJsxRuntime(hast, { Fragment, jsx, jsxs });
141
+ return { Component: () => element, outline };
50
142
  }
51
143
  catch (error) {
52
144
  return {
53
145
  Component: null,
54
146
  error: error instanceof Error ? error : new Error('MDX evaluation failed'),
147
+ outline: [],
55
148
  };
56
149
  }
57
150
  }
151
+ //# sourceMappingURL=evaluate.js.map
@@ -0,0 +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,qBAAqB,EAAE,MAAM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AAE,SAAS,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;IAC/C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,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,46 @@
1
+ import type { MDXHeading } from '@conloca/content-api';
2
+ /**
3
+ * One heading of a fragment: the anchor target and the contents entry. Aliased
4
+ * from the compile response's own type rather than restated, so the shape the
5
+ * editor receives over HTTP and the shape this pass produces cannot drift.
6
+ */
7
+ export type FragmentHeading = MDXHeading;
8
+ /**
9
+ * A heading node in either shape this pipeline produces: a plain hast element,
10
+ * or the mdxJsx node `remarkRehype`'s `passThrough` list preserves for a
11
+ * heading the author wrote as literal JSX.
12
+ */
13
+ type OutlineScalar = string | number | boolean | null | undefined;
14
+ interface OutlineAttribute {
15
+ type?: string;
16
+ name?: string | null;
17
+ value?: string | null | object;
18
+ }
19
+ interface OutlineProperties {
20
+ id?: OutlineScalar | readonly OutlineScalar[];
21
+ }
22
+ export interface OutlineTree {
23
+ type: string;
24
+ children?: OutlineTree[];
25
+ value?: string;
26
+ tagName?: string;
27
+ name?: string | null;
28
+ properties?: OutlineProperties;
29
+ attributes?: OutlineAttribute[];
30
+ }
31
+ /**
32
+ * Give every heading in the tree an id and report them in document order.
33
+ *
34
+ * Both pipelines call this: the published render (`evaluate.ts`) and the
35
+ * compile every editing surface goes through (`compile.ts`). One pass assigns
36
+ * the id and records it, so the anchor in the markup and the anchor in the
37
+ * outline cannot disagree — which is the whole reason the outline is produced
38
+ * here rather than read back off rendered HTML.
39
+ *
40
+ * `unknown` rather than a hast `Root`: this package does not depend on
41
+ * `@types/hast`, and the walk is structural anyway — it reads `type`, `name`,
42
+ * `tagName` and `children`, and ignores anything that does not carry them.
43
+ */
44
+ export declare function applyHeadingIds(tree: OutlineTree): FragmentHeading[];
45
+ export {};
46
+ //# sourceMappingURL=heading-outline.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heading-outline.d.ts","sourceRoot":"","sources":["../../src/mdx/heading-outline.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGvD;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC;AAEzC;;;;GAIG;AACH,KAAK,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAElE,UAAU,gBAAgB;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC;CAChC;AAED,UAAU,iBAAiB;IACzB,EAAE,CAAC,EAAE,aAAa,GAAG,SAAS,aAAa,EAAE,CAAC;CAC/C;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,UAAU,CAAC,EAAE,iBAAiB,CAAC;IAC/B,UAAU,CAAC,EAAE,gBAAgB,EAAE,CAAC;CACjC;AAiFD;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,GAAG,eAAe,EAAE,CAuBpE"}
@@ -0,0 +1,114 @@
1
+ import GithubSlugger, { slug } from 'github-slugger';
2
+ const HEADING_NAME = /^h([1-6])$/;
3
+ function headingOf(node) {
4
+ const type = node.type;
5
+ const name = type === 'element' ? node.tagName : node.name;
6
+ if (typeof name !== 'string')
7
+ return null;
8
+ const match = HEADING_NAME.exec(name);
9
+ if (!match)
10
+ return null;
11
+ if (type !== 'element' && type !== 'mdxJsxFlowElement' && type !== 'mdxJsxTextElement')
12
+ return null;
13
+ return { node, depth: Number(match[1]), jsx: type !== 'element' };
14
+ }
15
+ function walk(node, visit) {
16
+ visit(node);
17
+ const children = node.children;
18
+ if (Array.isArray(children)) {
19
+ for (const child of children)
20
+ walk(child, visit);
21
+ }
22
+ }
23
+ /** Visible text of a heading, with `**bold**`, `` `code` `` and the like flattened away. */
24
+ function textOf(node) {
25
+ let text = '';
26
+ walk(node, (child) => {
27
+ if (child.type === 'text' && typeof child.value === 'string')
28
+ text += child.value;
29
+ });
30
+ return text.trim().replace(/\s+/g, ' ');
31
+ }
32
+ function existingIdOf(heading) {
33
+ if (!heading.jsx) {
34
+ const id = heading.node.properties?.id;
35
+ if (typeof id === 'string' && id !== '')
36
+ return id;
37
+ return undefined;
38
+ }
39
+ const attributes = heading.node.attributes;
40
+ if (!Array.isArray(attributes))
41
+ return undefined;
42
+ for (const attribute of attributes) {
43
+ if (attribute.name === 'id' && typeof attribute.value === 'string' && attribute.value !== '') {
44
+ return attribute.value;
45
+ }
46
+ }
47
+ return undefined;
48
+ }
49
+ function setIdOf(heading, id) {
50
+ if (!heading.jsx) {
51
+ const properties = heading.node.properties ?? {};
52
+ properties.id = id;
53
+ heading.node.properties = properties;
54
+ return;
55
+ }
56
+ const attributes = Array.isArray(heading.node.attributes) ? heading.node.attributes : [];
57
+ attributes.push({ type: 'mdxJsxAttribute', name: 'id', value: id });
58
+ heading.node.attributes = attributes;
59
+ }
60
+ /**
61
+ * GitHub's heading-id rules, the ones Astro gives Markdown headings too, so a
62
+ * heading in any script keeps a readable id that does not depend on its
63
+ * position. `section` stands in for a heading that slugs to nothing ('!!!'),
64
+ * because an empty id renders a contents link as a bare `#`; it goes through
65
+ * the slugger like any other text, so a second one becomes `section-1`, not
66
+ * `-1`. Author ids live in their own set because the slugger can only record
67
+ * the slug of a value, never reserve an exact id.
68
+ */
69
+ function idFor(text, slugger, reserved) {
70
+ const base = slug(text) || 'section';
71
+ let id = slugger.slug(base);
72
+ while (reserved.has(id))
73
+ id = slugger.slug(base);
74
+ return id;
75
+ }
76
+ /**
77
+ * Give every heading in the tree an id and report them in document order.
78
+ *
79
+ * Both pipelines call this: the published render (`evaluate.ts`) and the
80
+ * compile every editing surface goes through (`compile.ts`). One pass assigns
81
+ * the id and records it, so the anchor in the markup and the anchor in the
82
+ * outline cannot disagree — which is the whole reason the outline is produced
83
+ * here rather than read back off rendered HTML.
84
+ *
85
+ * `unknown` rather than a hast `Root`: this package does not depend on
86
+ * `@types/hast`, and the walk is structural anyway — it reads `type`, `name`,
87
+ * `tagName` and `children`, and ignores anything that does not carry them.
88
+ */
89
+ export function applyHeadingIds(tree) {
90
+ const headings = [];
91
+ walk(tree, (node) => {
92
+ const heading = headingOf(node);
93
+ if (heading)
94
+ headings.push(heading);
95
+ });
96
+ // Author-chosen ids are reserved before any are generated, so a generated
97
+ // slug never silently takes an id the author already spelled out further down.
98
+ const reserved = new Set();
99
+ for (const heading of headings) {
100
+ const existing = existingIdOf(heading);
101
+ if (existing !== undefined)
102
+ reserved.add(existing);
103
+ }
104
+ const slugger = new GithubSlugger();
105
+ return headings.map((heading) => {
106
+ const text = textOf(heading.node);
107
+ const existing = existingIdOf(heading);
108
+ const id = existing ?? idFor(text, slugger, reserved);
109
+ if (existing === undefined)
110
+ setIdOf(heading, id);
111
+ return { depth: heading.depth, id, text };
112
+ });
113
+ }
114
+ //# sourceMappingURL=heading-outline.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heading-outline.js","sourceRoot":"","sources":["../../src/mdx/heading-outline.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,EAAE,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AA0CrD,MAAM,YAAY,GAAG,YAAY,CAAC;AAElC,SAAS,SAAS,CAAC,I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,CAAC,IAAI,CAAC,CAAC;QAClC,MAAM,QAAQ,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;QACvC,MAAM,EAAE,GAAG,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;QACtD,IAAI,QAAQ,KAAK,SAAS;YAAE,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QACjD,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;IAC5C,CAAC,CAAC,CAAC;AACL,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"}