@conloca/mdx 0.1.15 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -18
- package/dist/code-block-frame.d.ts +41 -0
- package/dist/code-block-frame.d.ts.map +1 -0
- package/dist/code-block-frame.js +187 -0
- package/dist/code-block-frame.js.map +1 -0
- package/dist/editor-core.d.ts +56 -0
- package/dist/editor-core.d.ts.map +1 -0
- package/dist/editor-core.js +225 -0
- package/dist/editor-core.js.map +1 -0
- package/dist/esm-passthrough.d.ts +36 -0
- package/dist/esm-passthrough.d.ts.map +1 -0
- package/dist/esm-passthrough.js +237 -0
- package/dist/esm-passthrough.js.map +1 -0
- package/dist/fragments.d.ts +37 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +53 -0
- package/dist/fragments.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown-options.d.ts +3 -0
- package/dist/markdown-options.d.ts.map +1 -0
- package/dist/markdown-options.js +20 -0
- package/dist/markdown-options.js.map +1 -0
- package/dist/mdx/compile-request.d.ts +5 -0
- package/dist/mdx/compile-request.d.ts.map +1 -0
- package/dist/mdx/compile-request.js +40 -0
- package/dist/mdx/compile-request.js.map +1 -0
- package/dist/mdx/compile.d.ts +3 -0
- package/dist/mdx/compile.d.ts.map +1 -0
- package/dist/mdx/compile.js +47 -0
- package/dist/mdx/compile.js.map +1 -0
- package/dist/mdx/evaluate.d.ts +39 -0
- package/dist/mdx/evaluate.d.ts.map +1 -0
- package/dist/mdx/evaluate.js +131 -0
- package/dist/mdx/evaluate.js.map +1 -0
- package/dist/mdx/heading-outline.d.ts +46 -0
- package/dist/mdx/heading-outline.d.ts.map +1 -0
- package/dist/mdx/heading-outline.js +114 -0
- package/dist/mdx/heading-outline.js.map +1 -0
- package/dist/mdx/restrict-expressions.d.ts +43 -0
- package/dist/mdx/restrict-expressions.d.ts.map +1 -0
- package/dist/mdx/restrict-expressions.js +160 -0
- package/dist/mdx/restrict-expressions.js.map +1 -0
- package/dist/mdx-editor.d.ts +7 -0
- package/dist/mdx-editor.d.ts.map +1 -0
- package/dist/mdx-editor.js +8 -0
- package/dist/mdx-editor.js.map +1 -0
- package/dist/node.d.ts +18 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +18 -0
- package/dist/node.js.map +1 -0
- package/dist/sandbox-frame.d.ts +114 -0
- package/dist/sandbox-frame.d.ts.map +1 -0
- package/dist/sandbox-frame.js +234 -0
- package/dist/sandbox-frame.js.map +1 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +82 -26
- package/src/code-block-frame.tsx +262 -0
- package/src/editor-core.tsx +403 -0
- package/src/editor-styles.css +282 -0
- package/src/esm-passthrough.ts +269 -0
- package/src/fragments.ts +91 -0
- package/src/index.ts +5 -0
- package/src/markdown-options.ts +21 -0
- package/src/mdx/compile-request.ts +53 -0
- package/src/mdx/compile.ts +51 -0
- package/src/mdx/evaluate.ts +144 -0
- package/src/mdx/heading-outline.ts +153 -0
- package/src/mdx/restrict-expressions.ts +173 -0
- package/src/mdx-editor.tsx +15 -0
- package/src/node.ts +25 -0
- package/src/sandbox-frame.ts +277 -0
- package/src/types.ts +23 -0
- package/dist/index.d.mts +0 -96
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -425
- package/dist/index.mjs.map +0 -1
- package/dist/node.d.mts +0 -107
- package/dist/node.d.mts.map +0 -1
- package/dist/node.mjs +0 -125
- package/dist/node.mjs.map +0 -1
|
@@ -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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown-options.d.ts","sourceRoot":"","sources":["../src/markdown-options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAY3E,eAAO,MAAM,mBAAmB,EAAE,iBAQjC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// MDXEditor rebuilds the markdown body from its Lexical AST on every change
|
|
2
|
+
// via `mdast-util-to-markdown`. Without explicit options, the library defaults
|
|
3
|
+
// flip canonical project style: thematic breaks render as `***` instead of
|
|
4
|
+
// `---`, bullets pick `*` over `-`, and emphasis ambiguity inside GFM table
|
|
5
|
+
// cells inserts backslash escapes on every save. Pinning these to match the
|
|
6
|
+
// committed fixtures keeps no-op round-trips diff-clean.
|
|
7
|
+
//
|
|
8
|
+
// Exported as a plain TS module (no React deps) so it can be exercised
|
|
9
|
+
// directly from tests against `mdast-util-to-markdown` without mounting the
|
|
10
|
+
// editor.
|
|
11
|
+
export const TO_MARKDOWN_OPTIONS = {
|
|
12
|
+
bullet: '-',
|
|
13
|
+
rule: '-',
|
|
14
|
+
emphasis: '_',
|
|
15
|
+
strong: '*',
|
|
16
|
+
fence: '`',
|
|
17
|
+
listItemIndent: 'one',
|
|
18
|
+
tightDefinitions: true,
|
|
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;IAClB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,QAAQ,CAAC,IAAI,CAClB,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,UAAU,CAAC,eAAe,EAAE,OAAO,EAAE,mBAAmB,EAAE,EAAE,EAC7E,EAAE,MAA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,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"}
|
|
@@ -0,0 +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;AAIpD,wBAAsB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAyC3E"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { compile as mdxCompile } from '@mdx-js/mdx';
|
|
2
|
+
import matter from 'gray-matter';
|
|
3
|
+
import remarkFrontmatter from 'remark-frontmatter';
|
|
4
|
+
import remarkGfm from 'remark-gfm';
|
|
5
|
+
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
|
|
6
|
+
import { applyHeadingIds } from './heading-outline.js';
|
|
7
|
+
import { remarkRestrictExpressions } from './restrict-expressions.js';
|
|
8
|
+
export async function compileMDX(content) {
|
|
9
|
+
try {
|
|
10
|
+
// Empty options opt out of gray-matter's module-level memo cache — a plain
|
|
11
|
+
// object keyed by the raw input, so inputs naming an Object.prototype
|
|
12
|
+
// member ('valueOf', 'toString', …) return the inherited member instead of
|
|
13
|
+
// a parse.
|
|
14
|
+
const { data: metadata } = matter(content, {});
|
|
15
|
+
// The editing surfaces read their outline off this compile rather than off
|
|
16
|
+
// the rendered frame, which they cannot see into: the fragment body renders
|
|
17
|
+
// in a sandbox with no same-origin access.
|
|
18
|
+
let outline = [];
|
|
19
|
+
const collectOutline = () => (tree) => {
|
|
20
|
+
outline = applyHeadingIds(tree);
|
|
21
|
+
};
|
|
22
|
+
const compiled = await mdxCompile(content, {
|
|
23
|
+
outputFormat: 'function-body',
|
|
24
|
+
development: process.env.NODE_ENV === 'development',
|
|
25
|
+
// `remarkRestrictExpressions` runs BEFORE `remarkMdxFrontmatter`: author
|
|
26
|
+
// `import`/`export` parse into `mdxjsEsm` nodes at parse time (so restrict
|
|
27
|
+
// sees and rejects them), while the legitimate `export const frontmatter`
|
|
28
|
+
// that `remarkMdxFrontmatter` injects appears only after restrict has run.
|
|
29
|
+
remarkPlugins: [remarkFrontmatter, remarkRestrictExpressions, remarkMdxFrontmatter, remarkGfm],
|
|
30
|
+
rehypePlugins: [collectOutline],
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
code: String(compiled.value),
|
|
34
|
+
metadata: metadata,
|
|
35
|
+
outline,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (error instanceof Error) {
|
|
40
|
+
const enhancedError = new Error(`MDX compilation failed: ${error.message}`);
|
|
41
|
+
enhancedError.stack = error.stack;
|
|
42
|
+
throw enhancedError;
|
|
43
|
+
}
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# 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,oBAAoB,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAE,eAAe,EAA0C,MAAM,sBAAsB,CAAC;AAC/F,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAEtE,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,OAAe;IAC9C,IAAI,CAAC;QACH,2EAA2E;QAC3E,sE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Turn an MDX content string into a React component WITHOUT executing any
|
|
5
|
+
* compiled code.
|
|
6
|
+
*
|
|
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.
|
|
17
|
+
*
|
|
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.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { evaluateMDXToComponent } from '@conloca/mdx/node';
|
|
25
|
+
*
|
|
26
|
+
* const { Component, error } = await evaluateMDXToComponent('# Hello\n\nThis is **MDX**');
|
|
27
|
+
* if (error) {
|
|
28
|
+
* console.error('Failed to evaluate:', error);
|
|
29
|
+
* } else if (Component) {
|
|
30
|
+
* // Render <Component /> in your React app
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function evaluateMDXToComponent(mdxContent: string): Promise<{
|
|
35
|
+
Component: ComponentType | null;
|
|
36
|
+
error?: Error;
|
|
37
|
+
outline: readonly FragmentHeading[];
|
|
38
|
+
}>;
|
|
39
|
+
//# sourceMappingURL=evaluate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;AAkF7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
2
|
+
import { Fragment, jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import remarkFrontmatter from 'remark-frontmatter';
|
|
4
|
+
import remarkGfm from 'remark-gfm';
|
|
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 { remarkRestrictExpressions } from './restrict-expressions.js';
|
|
11
|
+
/**
|
|
12
|
+
* Make every JSX element in the tree statically renderable.
|
|
13
|
+
*
|
|
14
|
+
* Capitalized component names are rejected with a clear author-facing error —
|
|
15
|
+
* this path has no component registry to resolve them from, and letting them
|
|
16
|
+
* through surfaces as a cryptic converter error instead.
|
|
17
|
+
*
|
|
18
|
+
* JSON-literal `{…}` attribute values (the only expressions
|
|
19
|
+
* `remarkRestrictExpressions` admits) are resolved into plain static values,
|
|
20
|
+
* so rendering never needs an expression evaluator: strings pass through,
|
|
21
|
+
* numbers stringify (identical SSR output), `{true}` becomes the JSX boolean
|
|
22
|
+
* shorthand, and `{false}`/`null`/object literals drop the attribute. Object
|
|
23
|
+
* props only ever carried data for components, which this static path does not
|
|
24
|
+
* render, so dropping them loses nothing.
|
|
25
|
+
*/
|
|
26
|
+
function resolveStaticJsx() {
|
|
27
|
+
return (tree) => {
|
|
28
|
+
const walk = (node) => {
|
|
29
|
+
if (node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') {
|
|
30
|
+
const name = typeof node.name === 'string' ? node.name : '';
|
|
31
|
+
if (/^[A-Z]/.test(name)) {
|
|
32
|
+
throw new Error(`Unknown component <${name}>: fragments render as plain content and cannot use React components.`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(node.attributes)) {
|
|
36
|
+
node.attributes = node.attributes.flatMap((attr) => {
|
|
37
|
+
const value = attr?.value;
|
|
38
|
+
if (!value || typeof value !== 'object')
|
|
39
|
+
return [attr];
|
|
40
|
+
const raw = value.value;
|
|
41
|
+
if (typeof raw !== 'string')
|
|
42
|
+
return [];
|
|
43
|
+
let parsed;
|
|
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 [];
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(node.children))
|
|
60
|
+
node.children.forEach(walk);
|
|
61
|
+
};
|
|
62
|
+
walk(tree);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Markdown → mdast (with MDX JSX syntax) → hast, with the JSX element nodes
|
|
67
|
+
* passed through for `toJsxRuntime` to turn into React elements directly.
|
|
68
|
+
*/
|
|
69
|
+
const processor = unified()
|
|
70
|
+
.use(remarkParse)
|
|
71
|
+
.use(remarkMdx)
|
|
72
|
+
.use(remarkFrontmatter)
|
|
73
|
+
.use(remarkGfm)
|
|
74
|
+
// Both transformers use loose structural node types (they predate this
|
|
75
|
+
// pipeline and also run inside @mdx-js's own plugin list), so bridge them to
|
|
76
|
+
// unified's strict Plugin shape here rather than forking their signatures.
|
|
77
|
+
.use(remarkRestrictExpressions)
|
|
78
|
+
.use(resolveStaticJsx)
|
|
79
|
+
.use(remarkRehype, { passThrough: ['mdxJsxFlowElement', 'mdxJsxTextElement'] });
|
|
80
|
+
/**
|
|
81
|
+
* Turn an MDX content string into a React component WITHOUT executing any
|
|
82
|
+
* compiled code.
|
|
83
|
+
*
|
|
84
|
+
* This is the published-site render path for CMS fragments, and it must run
|
|
85
|
+
* where creating code from strings is forbidden, such as Cloudflare Workers —
|
|
86
|
+
* `@mdx-js/mdx`'s `evaluate()` (a `new Function` under the hood) throws
|
|
87
|
+
* `EvalError: Code generation from strings disallowed` there. So instead of
|
|
88
|
+
* compile-and-execute, this parses the MDX to a syntax tree and converts the
|
|
89
|
+
* tree straight to React elements (`hast-util-to-jsx-runtime`). That is
|
|
90
|
+
* sufficient by construction: `remarkRestrictExpressions` already rejects
|
|
91
|
+
* `{…}` expressions and top-level `import`/`export`, and only JSON-literal
|
|
92
|
+
* attribute values survive — nothing in admissible content needs code
|
|
93
|
+
* execution to render.
|
|
94
|
+
*
|
|
95
|
+
* Unregistered capitalized JSX components fail here (evaluate time) rather
|
|
96
|
+
* than when React renders the component, so callers get the error result and
|
|
97
|
+
* can show their fragment-error UI instead of failing the whole page render.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* import { evaluateMDXToComponent } from '@conloca/mdx/node';
|
|
102
|
+
*
|
|
103
|
+
* const { Component, error } = await evaluateMDXToComponent('# Hello\n\nThis is **MDX**');
|
|
104
|
+
* if (error) {
|
|
105
|
+
* console.error('Failed to evaluate:', error);
|
|
106
|
+
* } else if (Component) {
|
|
107
|
+
* // Render <Component /> in your React app
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export async function evaluateMDXToComponent(mdxContent) {
|
|
112
|
+
try {
|
|
113
|
+
const mdast = processor.parse(mdxContent);
|
|
114
|
+
const hast = await processor.run(mdast);
|
|
115
|
+
// Before the tree becomes React: assigning the ids and reading the outline
|
|
116
|
+
// in one pass is what keeps a contents link and its heading in agreement.
|
|
117
|
+
const outline = applyHeadingIds(hast);
|
|
118
|
+
// Convert eagerly so bad content (e.g. an unknown component) surfaces as
|
|
119
|
+
// the error result; the returned component just replays the static tree.
|
|
120
|
+
const element = toJsxRuntime(hast, { Fragment, jsx, jsxs });
|
|
121
|
+
return { Component: () => element, outline };
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
return {
|
|
125
|
+
Component: null,
|
|
126
|
+
error: error instanceof Error ? error : new Error('MDX evaluation failed'),
|
|
127
|
+
outline: [],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# 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,yBAAyB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E;QAC1E,MAAM,OAAO,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;QACtC,yEAAyE;QACzE,yEAAyE;QACzE,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;QAC5D,OAAO,EAAE,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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,CAAC,CAAC;IACrD,CAAC;IACD,MAAM,OAAO,GAAG,IAAI,aAAa,EAAE,CAAC;IAEpC,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;QAC9B,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,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,43 @@
|
|
|
1
|
+
interface MdastNode {
|
|
2
|
+
type?: string;
|
|
3
|
+
children?: MdastNode[];
|
|
4
|
+
attributes?: MdastAttribute[];
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
}
|
|
7
|
+
interface MdastAttribute {
|
|
8
|
+
type?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
value?: unknown;
|
|
11
|
+
}
|
|
12
|
+
/** Remark transform that neutralizes the author-code AND HTML-injection vectors
|
|
13
|
+
* in authored MDX at every compile/eval entry point.
|
|
14
|
+
*
|
|
15
|
+
* Two layers, because expression-restriction is NOT HTML sanitization:
|
|
16
|
+
* 1. Author code — standalone `{…}` flow/text expressions (`mdxFlowExpression`/
|
|
17
|
+
* `mdxTextExpression`) and top-level ESM `import`/`export` (`mdxjsEsm`) reach
|
|
18
|
+
* `new AsyncFunction`/`evaluate()` as live code, so they fail the compile
|
|
19
|
+
* with a clear error; content fragments never legitimately need either.
|
|
20
|
+
* 2. HTML injection — `dangerouslySetInnerHTML` (React's raw-HTML escape hatch,
|
|
21
|
+
* which a JSON-literal object slips past the expression exemption), inline
|
|
22
|
+
* `on*` handlers, dangerous URL schemes (`javascript:`/`vbscript:`/
|
|
23
|
+
* `data:text/html`) on URL-bearing attributes and markdown link/image URLs,
|
|
24
|
+
* and script/navigate/embed intrinsic elements (`<script>`, `<iframe>`, …)
|
|
25
|
+
* are dropped or emptied. This layers atop React's own SSR neutralization
|
|
26
|
+
* (it blocks `javascript:` URLs, strips string `on*` handlers, and escapes
|
|
27
|
+
* all text/attribute values). The set above is a renderer-agnostic superset:
|
|
28
|
+
* some of it (`javascript:`, string `on*`) React also covers, but it is
|
|
29
|
+
* re-applied here for the compile and mdast-serializer paths where React is
|
|
30
|
+
* not in the loop, and it adds what React does NOT handle (`data:text/html`,
|
|
31
|
+
* `dangerouslySetInnerHTML`, raw `<script>`/embed elements) — which the
|
|
32
|
+
* published `evaluateMDXToComponent` path would otherwise emit verbatim to
|
|
33
|
+
* every visitor.
|
|
34
|
+
*
|
|
35
|
+
* Non-literal JSX attribute and spread expressions are dropped so a component
|
|
36
|
+
* still renders with its safe props. Shared by `compileMDX` (editor preview) and
|
|
37
|
+
* `evaluateMDXToComponent` (published-site render), so the control is identical
|
|
38
|
+
* on both. In the editor the sandbox is the sufficient boundary; on the published
|
|
39
|
+
* path this transform is the boundary, so it must close the class, not just the
|
|
40
|
+
* eval vector. */
|
|
41
|
+
export declare function remarkRestrictExpressions(): (tree: MdastNode) => void;
|
|
42
|
+
export {};
|
|
43
|
+
//# sourceMappingURL=restrict-expressions.d.ts.map
|
|
@@ -0,0 +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;AA0GD;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBA4BkB;AAClB,wBAAgB,yBAAyB,WACzB,SAAS,KAAG,IAAI,CAyB/B"}
|