@hanzo/mdx-runtime 1.5.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/dist/chunk-5V3MNLHP.js +34 -0
- package/dist/client/index.d.ts +22 -0
- package/dist/client/index.js +8 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.js +169 -0
- package/package.json +68 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/render.ts
|
|
2
|
+
import jsxRuntimeDefault from "react/jsx-runtime";
|
|
3
|
+
var AsyncFunction = Object.getPrototypeOf(executeMdx).constructor;
|
|
4
|
+
async function executeMdx(compiled, options = {}) {
|
|
5
|
+
const { opts: scopeOpts, ...scope } = options.scope ?? {};
|
|
6
|
+
const fullScope = {
|
|
7
|
+
opts: {
|
|
8
|
+
...scopeOpts,
|
|
9
|
+
...options.jsxRuntime ?? jsxRuntimeDefault,
|
|
10
|
+
baseUrl: options.baseUrl
|
|
11
|
+
},
|
|
12
|
+
...scope
|
|
13
|
+
};
|
|
14
|
+
const hydrateFn = new AsyncFunction(...Object.keys(fullScope), compiled);
|
|
15
|
+
return await hydrateFn.apply(hydrateFn, Object.values(fullScope));
|
|
16
|
+
}
|
|
17
|
+
function executeMdxSync(compiled, options = {}) {
|
|
18
|
+
const { opts: scopeOpts, ...scope } = options.scope ?? {};
|
|
19
|
+
const fullScope = {
|
|
20
|
+
opts: {
|
|
21
|
+
...scopeOpts,
|
|
22
|
+
...options.jsxRuntime ?? jsxRuntimeDefault,
|
|
23
|
+
baseUrl: options.baseUrl
|
|
24
|
+
},
|
|
25
|
+
...scope
|
|
26
|
+
};
|
|
27
|
+
const hydrateFn = new Function(...Object.keys(fullScope), compiled);
|
|
28
|
+
return hydrateFn.apply(hydrateFn, Object.values(fullScope));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
executeMdx,
|
|
33
|
+
executeMdxSync
|
|
34
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MDXComponents } from 'mdx/types';
|
|
2
|
+
import { TableOfContents } from '@hanzo/docs-core/toc';
|
|
3
|
+
import { FC } from 'react';
|
|
4
|
+
|
|
5
|
+
type MdxContent = FC<{
|
|
6
|
+
components?: MDXComponents;
|
|
7
|
+
}>;
|
|
8
|
+
interface Options {
|
|
9
|
+
scope?: Record<string, unknown>;
|
|
10
|
+
baseUrl?: string | URL;
|
|
11
|
+
jsxRuntime?: unknown;
|
|
12
|
+
}
|
|
13
|
+
declare function executeMdx(compiled: string, options?: Options): Promise<{
|
|
14
|
+
default: MdxContent;
|
|
15
|
+
toc?: TableOfContents;
|
|
16
|
+
}>;
|
|
17
|
+
declare function executeMdxSync(compiled: string, options?: Options): {
|
|
18
|
+
default: MdxContent;
|
|
19
|
+
toc?: TableOfContents;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { type MdxContent, executeMdx, executeMdxSync };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { TableOfContents } from '@hanzo/docs-core/toc';
|
|
2
|
+
import { MdxContent } from './client/index.js';
|
|
3
|
+
import * as Plugins from '@hanzo/docs-core/mdx-plugins';
|
|
4
|
+
import { CompileOptions } from '@mdx-js/mdx';
|
|
5
|
+
import { MDXComponents } from 'mdx/types';
|
|
6
|
+
import { Pluggable } from 'unified';
|
|
7
|
+
import { VFile, Compatible } from 'vfile';
|
|
8
|
+
import 'react';
|
|
9
|
+
|
|
10
|
+
type ResolvePlugins = Pluggable[] | ((v: Pluggable[]) => Pluggable[]);
|
|
11
|
+
/**
|
|
12
|
+
* Parse frontmatter, currently powered by `gray-matter`
|
|
13
|
+
*/
|
|
14
|
+
declare function parseFrontmatter(content: string): {
|
|
15
|
+
frontmatter: {
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
};
|
|
18
|
+
content: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type FumadocsPresetOptions = Omit<CompileOptions, 'remarkPlugins' | 'rehypePlugins'> & {
|
|
22
|
+
preset?: 'fumadocs';
|
|
23
|
+
remarkPlugins?: ResolvePlugins;
|
|
24
|
+
rehypePlugins?: ResolvePlugins;
|
|
25
|
+
remarkHeadingOptions?: Plugins.RemarkHeadingOptions | false;
|
|
26
|
+
rehypeCodeOptions?: Plugins.RehypeCodeOptions | false;
|
|
27
|
+
rehypeTocOptions?: Plugins.RehypeTocOptions | false;
|
|
28
|
+
remarkCodeTabOptions?: Plugins.RemarkCodeTabOptions | false;
|
|
29
|
+
remarkNpmOptions?: Plugins.RemarkNpmOptions | false;
|
|
30
|
+
/**
|
|
31
|
+
* The directory to find image sizes
|
|
32
|
+
*
|
|
33
|
+
* @defaultValue './public'
|
|
34
|
+
* @deprecated Use `remarkImageOptions.publicDir` instead
|
|
35
|
+
*/
|
|
36
|
+
imageDir?: string;
|
|
37
|
+
remarkImageOptions?: Plugins.RemarkImageOptions | false;
|
|
38
|
+
};
|
|
39
|
+
type CompilerOptions = (CompileOptions & {
|
|
40
|
+
preset: 'minimal';
|
|
41
|
+
}) | FumadocsPresetOptions;
|
|
42
|
+
interface CompileMDXOptions {
|
|
43
|
+
source: string;
|
|
44
|
+
/**
|
|
45
|
+
* File path of source content
|
|
46
|
+
*/
|
|
47
|
+
filePath?: string;
|
|
48
|
+
components?: MDXComponents;
|
|
49
|
+
scope?: Record<string, unknown>;
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated Use `compiler.compileFile` instead if you doesn't need to execute output JavaScript code.
|
|
52
|
+
*/
|
|
53
|
+
skipRender?: boolean;
|
|
54
|
+
}
|
|
55
|
+
interface CompileMDXResult<TFrontmatter = Record<string, unknown>> {
|
|
56
|
+
body: MdxContent;
|
|
57
|
+
frontmatter: TFrontmatter;
|
|
58
|
+
toc: TableOfContents;
|
|
59
|
+
vfile: VFile;
|
|
60
|
+
compiled: string;
|
|
61
|
+
exports: Record<string, unknown> | null;
|
|
62
|
+
}
|
|
63
|
+
declare function createCompiler(mdxOptions?: CompilerOptions): {
|
|
64
|
+
render(compiled: string, scope?: Record<string, unknown>, filePath?: string): Promise<{
|
|
65
|
+
default: MdxContent;
|
|
66
|
+
toc?: TableOfContents;
|
|
67
|
+
}>;
|
|
68
|
+
/**
|
|
69
|
+
* Compile VFile
|
|
70
|
+
*/
|
|
71
|
+
compileFile(from: Compatible): Promise<VFile>;
|
|
72
|
+
compile<Frontmatter extends object = Record<string, unknown>>(options: CompileMDXOptions): Promise<CompileMDXResult<Frontmatter>>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* @deprecated Use `createCompiler()` API instead, this function will always create a new compiler instance.
|
|
76
|
+
*/
|
|
77
|
+
declare function compileMDX<Frontmatter extends object = Record<string, unknown>>(options: CompileMDXOptions & {
|
|
78
|
+
mdxOptions?: CompilerOptions;
|
|
79
|
+
}): Promise<CompileMDXResult<Frontmatter>>;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated Use `compiler.render` instead
|
|
83
|
+
*/
|
|
84
|
+
declare function executeMdx(compiled: string, scope: object, baseUrl?: string | URL): Promise<{
|
|
85
|
+
default: MdxContent;
|
|
86
|
+
toc?: TableOfContents;
|
|
87
|
+
}>;
|
|
88
|
+
|
|
89
|
+
export { type CompileMDXOptions, type CompileMDXResult, type CompilerOptions, type FumadocsPresetOptions, type CompilerOptions as MDXOptions, compileMDX, createCompiler, executeMdx, parseFrontmatter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import {
|
|
2
|
+
executeMdx
|
|
3
|
+
} from "./chunk-5V3MNLHP.js";
|
|
4
|
+
|
|
5
|
+
// src/compile.ts
|
|
6
|
+
import * as Plugins from "@hanzo/docs-core/mdx-plugins";
|
|
7
|
+
import { createProcessor } from "@mdx-js/mdx";
|
|
8
|
+
|
|
9
|
+
// src/utils.ts
|
|
10
|
+
import matter from "gray-matter";
|
|
11
|
+
function pluginOption(def, options = []) {
|
|
12
|
+
const list = def(Array.isArray(options) ? options : []).filter(
|
|
13
|
+
Boolean
|
|
14
|
+
);
|
|
15
|
+
if (typeof options === "function") {
|
|
16
|
+
return options(list);
|
|
17
|
+
}
|
|
18
|
+
return list;
|
|
19
|
+
}
|
|
20
|
+
function parseFrontmatter(content) {
|
|
21
|
+
const out = matter(content);
|
|
22
|
+
return {
|
|
23
|
+
frontmatter: out.data,
|
|
24
|
+
content: out.content
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/compile.ts
|
|
29
|
+
import { pathToFileURL } from "url";
|
|
30
|
+
function createCompiler(mdxOptions) {
|
|
31
|
+
let instance;
|
|
32
|
+
function getProcessor() {
|
|
33
|
+
if (instance) return instance;
|
|
34
|
+
let format = mdxOptions?.format;
|
|
35
|
+
if (!format || format === "detect") format = "mdx";
|
|
36
|
+
return instance = createProcessor({
|
|
37
|
+
...mdxOptions?.preset === "minimal" ? mdxOptions : getCompileOptions(mdxOptions),
|
|
38
|
+
format
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
async render(compiled, scope, filePath) {
|
|
43
|
+
return executeMdx(compiled, {
|
|
44
|
+
scope,
|
|
45
|
+
baseUrl: filePath ? pathToFileURL(filePath) : void 0,
|
|
46
|
+
jsxRuntime: mdxOptions?.development ? await import("react/jsx-dev-runtime") : void 0
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
/**
|
|
50
|
+
* Compile VFile
|
|
51
|
+
*/
|
|
52
|
+
async compileFile(from) {
|
|
53
|
+
return getProcessor().process(from);
|
|
54
|
+
},
|
|
55
|
+
async compile(options) {
|
|
56
|
+
const { scope = {}, skipRender } = options;
|
|
57
|
+
const { frontmatter, content } = parseFrontmatter(options.source);
|
|
58
|
+
const file = await this.compileFile({
|
|
59
|
+
value: content,
|
|
60
|
+
path: options.filePath,
|
|
61
|
+
data: {
|
|
62
|
+
frontmatter
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
const compiled = String(file);
|
|
66
|
+
const exports = !skipRender ? await this.render(compiled, scope, options.filePath) : null;
|
|
67
|
+
return {
|
|
68
|
+
vfile: file,
|
|
69
|
+
compiled,
|
|
70
|
+
frontmatter,
|
|
71
|
+
async body(props) {
|
|
72
|
+
if (!exports)
|
|
73
|
+
throw new Error(
|
|
74
|
+
"Body cannot be rendered when `skipRender` is set to true"
|
|
75
|
+
);
|
|
76
|
+
return exports.default({
|
|
77
|
+
components: { ...options.components, ...props.components }
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
toc: exports?.toc ?? file.data.toc,
|
|
81
|
+
exports
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async function compileMDX(options) {
|
|
87
|
+
const compiler = createCompiler(options.mdxOptions);
|
|
88
|
+
return compiler.compile(options);
|
|
89
|
+
}
|
|
90
|
+
function getCompileOptions({
|
|
91
|
+
preset: _,
|
|
92
|
+
rehypeCodeOptions,
|
|
93
|
+
remarkImageOptions,
|
|
94
|
+
rehypeTocOptions,
|
|
95
|
+
remarkHeadingOptions,
|
|
96
|
+
remarkCodeTabOptions,
|
|
97
|
+
remarkNpmOptions,
|
|
98
|
+
imageDir = "./public",
|
|
99
|
+
...options
|
|
100
|
+
} = {}) {
|
|
101
|
+
function getPlugin(name) {
|
|
102
|
+
return name in Plugins ? Plugins[name] : null;
|
|
103
|
+
}
|
|
104
|
+
const remarkGfm = getPlugin("remarkGfm");
|
|
105
|
+
const remarkHeading = getPlugin("remarkHeading");
|
|
106
|
+
const remarkCodeTab = getPlugin("remarkCodeTab");
|
|
107
|
+
const remarkNpm = getPlugin("remarkNpm");
|
|
108
|
+
const remarkImage = getPlugin("remarkImage");
|
|
109
|
+
const rehypeCode = getPlugin("rehypeCode");
|
|
110
|
+
const rehypeToc = getPlugin("rehypeToc");
|
|
111
|
+
return {
|
|
112
|
+
...options,
|
|
113
|
+
outputFormat: "function-body",
|
|
114
|
+
remarkPlugins: pluginOption(
|
|
115
|
+
(v) => [
|
|
116
|
+
remarkGfm,
|
|
117
|
+
remarkHeading && remarkHeadingOptions !== false ? [remarkHeading, remarkHeadingOptions] : null,
|
|
118
|
+
remarkImage && remarkImageOptions !== false ? [
|
|
119
|
+
remarkImage,
|
|
120
|
+
{
|
|
121
|
+
useImport: false,
|
|
122
|
+
publicDir: imageDir,
|
|
123
|
+
...remarkImageOptions
|
|
124
|
+
}
|
|
125
|
+
] : null,
|
|
126
|
+
remarkCodeTab && remarkCodeTabOptions !== false ? [remarkCodeTab, remarkCodeTabOptions] : null,
|
|
127
|
+
remarkNpm && remarkNpmOptions !== false ? [remarkNpm, remarkNpmOptions] : null,
|
|
128
|
+
...v
|
|
129
|
+
],
|
|
130
|
+
options.remarkPlugins
|
|
131
|
+
),
|
|
132
|
+
rehypePlugins: pluginOption(
|
|
133
|
+
(v) => [
|
|
134
|
+
rehypeCode && rehypeCodeOptions !== false ? [rehypeCode, rehypeCodeOptions] : null,
|
|
135
|
+
rehypeToc && rehypeTocOptions !== false ? [rehypeToc, rehypeTocOptions] : null,
|
|
136
|
+
...v
|
|
137
|
+
],
|
|
138
|
+
options.rehypePlugins
|
|
139
|
+
)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/index.ts
|
|
144
|
+
async function executeMdx2(compiled, scope, baseUrl) {
|
|
145
|
+
let jsxRuntime;
|
|
146
|
+
if (process.env.NODE_ENV === "production") {
|
|
147
|
+
jsxRuntime = await import("react/jsx-runtime");
|
|
148
|
+
} else {
|
|
149
|
+
jsxRuntime = await import("react/jsx-dev-runtime");
|
|
150
|
+
}
|
|
151
|
+
const fullScope = {
|
|
152
|
+
opts: {
|
|
153
|
+
...jsxRuntime,
|
|
154
|
+
baseUrl
|
|
155
|
+
},
|
|
156
|
+
...scope
|
|
157
|
+
};
|
|
158
|
+
const values = Object.values(fullScope);
|
|
159
|
+
const params = Object.keys(fullScope);
|
|
160
|
+
params.push(`return (async () => { ${compiled} })()`);
|
|
161
|
+
const hydrateFn = new Function(...params);
|
|
162
|
+
return await hydrateFn.apply(hydrateFn, values);
|
|
163
|
+
}
|
|
164
|
+
export {
|
|
165
|
+
compileMDX,
|
|
166
|
+
createCompiler,
|
|
167
|
+
executeMdx2 as executeMdx,
|
|
168
|
+
parseFrontmatter
|
|
169
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hanzo/mdx-runtime",
|
|
3
|
+
"version": "1.5.0",
|
|
4
|
+
"description": "Runtime MDX compilation and execution for Hanzo Docs",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Hanzo",
|
|
7
|
+
"Docs",
|
|
8
|
+
"MDX",
|
|
9
|
+
"Runtime"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://hanzo.ai/docs",
|
|
12
|
+
"repository": "github:hanzoai/docs",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Fuma Nama",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./client": {
|
|
22
|
+
"import": "./dist/client/index.js",
|
|
23
|
+
"types": "./dist/client/index.d.ts"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"module": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist/*"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup",
|
|
33
|
+
"clean": "rimraf dist",
|
|
34
|
+
"dev": "tsup --watch",
|
|
35
|
+
"lint": "eslint .",
|
|
36
|
+
"types:check": "tsc --noEmit"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@mdx-js/mdx": "^3.1.1",
|
|
40
|
+
"gray-matter": "^4.0.3",
|
|
41
|
+
"zod": "^4.1.13"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/mdx": "^2.0.13",
|
|
45
|
+
"@types/node": "24.10.2",
|
|
46
|
+
"@types/react": "^19.2.7",
|
|
47
|
+
"eslint-config-custom": "workspace:*",
|
|
48
|
+
"@hanzo/docs-core": "workspace:*",
|
|
49
|
+
"react": "^19.2.3",
|
|
50
|
+
"tinyglobby": "^0.2.15",
|
|
51
|
+
"tsconfig": "workspace:*",
|
|
52
|
+
"unified": "^11.0.5",
|
|
53
|
+
"vfile": "^6.0.3"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@types/react": "*",
|
|
57
|
+
"@hanzo/docs-core": "^15.0.0 || ^16.0.0",
|
|
58
|
+
"react": "18.x.x || 19.x.x"
|
|
59
|
+
},
|
|
60
|
+
"peerDependenciesMeta": {
|
|
61
|
+
"@types/react": {
|
|
62
|
+
"optional": true
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"access": "public"
|
|
67
|
+
}
|
|
68
|
+
}
|