@hanzo/docs-docgen 3.0.4

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Fuma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Fumadocs Doc Gen
2
+
3
+ Remark plugins & Docs Generator utilities.
@@ -0,0 +1,24 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ export {
23
+ __async
24
+ };
@@ -0,0 +1,89 @@
1
+ import { Code, Root } from 'mdast';
2
+ import { Transformer } from 'unified';
3
+ import { VFile } from 'vfile';
4
+ import { Expression } from 'estree';
5
+ import { MdxJsxAttribute } from 'mdast-util-mdx-jsx';
6
+ import { z } from 'zod';
7
+
8
+ type Awaitable<T> = T | Promise<T>;
9
+ interface DocGenerator {
10
+ name: string;
11
+ /**
12
+ * Transform codeblocks to another mdast element
13
+ */
14
+ run: (input: unknown, context: Context) => Awaitable<object | object[] | undefined>;
15
+ onFile?: (tree: Root, file: VFile) => void;
16
+ }
17
+ interface Context {
18
+ node: Code;
19
+ path: string;
20
+ cwd: string;
21
+ }
22
+ interface RemarkDocGenOptions {
23
+ generators?: DocGenerator[];
24
+ }
25
+ declare function remarkDocGen({ generators, }: RemarkDocGenOptions): Transformer<Root, Root>;
26
+
27
+ declare function createElement(name: string, attributes: object[], children?: unknown): object;
28
+ declare function expressionToAttribute(key: string, value: Expression): MdxJsxAttribute;
29
+
30
+ interface FileGeneratorOptions {
31
+ /** @defaultValue true */
32
+ trim?: boolean;
33
+ /**
34
+ * Resolve reference files relative to `vfile.path`
35
+ *
36
+ * @defaultValue false
37
+ */
38
+ relative?: boolean;
39
+ }
40
+ type FileGeneratorInput = z.output<typeof fileGeneratorSchema>;
41
+ declare const fileGeneratorSchema: z.ZodObject<{
42
+ file: z.ZodString;
43
+ codeblock: z.ZodDefault<z.ZodUnion<readonly [z.ZodObject<{
44
+ lang: z.ZodOptional<z.ZodString>;
45
+ meta: z.ZodOptional<z.ZodString>;
46
+ }, z.core.$strip>, z.ZodBoolean]>>;
47
+ }, z.core.$strip>;
48
+ declare function fileGenerator({ relative, trim, }?: FileGeneratorOptions): DocGenerator;
49
+
50
+ interface PackageManager {
51
+ name: string;
52
+ /**
53
+ * Convert from npm to another package manager
54
+ */
55
+ command: (command: string) => string;
56
+ }
57
+ type RemarkInstallOptions = {
58
+ Tabs?: string;
59
+ Tab?: string;
60
+ /**
61
+ * Persist Tab value (Fumadocs UI only)
62
+ *
63
+ * @defaultValue false
64
+ */
65
+ persist?: {
66
+ id: string;
67
+ } | false;
68
+ packageManagers?: PackageManager[];
69
+ };
70
+ /**
71
+ * It generates the following structure from a code block with `package-install` as language
72
+ *
73
+ * @example
74
+ * ```tsx
75
+ * <Tabs items={["npm", "pnpm", "yarn", "bun"]}>
76
+ * <Tab value="pnpm">...</Tab>
77
+ * ...
78
+ * </Tabs>
79
+ * ```
80
+ *
81
+ * @deprecated Use `remarkNpm` from `@hanzo/docs-core/mdx-plugins` instead, it's a drop-in replacement.
82
+ */
83
+ declare function remarkInstall({ Tab, Tabs, persist, packageManagers, }?: RemarkInstallOptions): Transformer<Root, Root>;
84
+
85
+ declare function remarkShow(options?: {
86
+ variables?: Record<string, unknown>;
87
+ }): Transformer<Root, Root>;
88
+
89
+ export { type DocGenerator, type FileGeneratorInput, type FileGeneratorOptions, type RemarkDocGenOptions, type RemarkInstallOptions, createElement, expressionToAttribute, fileGenerator, fileGeneratorSchema, remarkDocGen, remarkInstall, remarkShow };
package/dist/index.js ADDED
@@ -0,0 +1,228 @@
1
+ import {
2
+ __async
3
+ } from "./chunk-4AFQP74Z.js";
4
+
5
+ // src/remark-docgen.ts
6
+ import { visit } from "unist-util-visit";
7
+ var metaRegex = new RegExp("doc-gen:(?<name>.+)");
8
+ function remarkDocGen({
9
+ generators = []
10
+ }) {
11
+ return (tree, file) => __async(null, null, function* () {
12
+ generators.forEach((gen) => {
13
+ var _a;
14
+ return (_a = gen.onFile) == null ? void 0 : _a.call(gen, tree, file);
15
+ });
16
+ const queue = [];
17
+ visit(tree, "code", (code, _, parent) => {
18
+ if (code.lang !== "json" || !code.meta || !parent) return;
19
+ const matches = metaRegex.exec(code.meta);
20
+ if (!matches) return;
21
+ const name = matches[1];
22
+ const gen = generators.find((g) => g.name === name);
23
+ const run = () => __async(null, null, function* () {
24
+ const result = yield gen == null ? void 0 : gen.run(JSON.parse(code.value), {
25
+ cwd: file.cwd,
26
+ path: file.path,
27
+ node: code
28
+ });
29
+ const index = parent.children.findIndex((c) => c === code);
30
+ if (result && index !== -1) {
31
+ const items = Array.isArray(result) ? result : [result];
32
+ parent.children.splice(index, 1, ...items);
33
+ }
34
+ });
35
+ queue.push(run());
36
+ });
37
+ yield Promise.all(queue);
38
+ });
39
+ }
40
+
41
+ // src/utils.ts
42
+ function createElement(name, attributes, children) {
43
+ const element = {
44
+ type: "mdxJsxFlowElement",
45
+ name,
46
+ attributes
47
+ };
48
+ if (children) element.children = children;
49
+ return element;
50
+ }
51
+ function expressionToAttribute(key, value) {
52
+ return {
53
+ type: "mdxJsxAttribute",
54
+ name: key,
55
+ value: {
56
+ type: "mdxJsxAttributeValueExpression",
57
+ value: "",
58
+ data: {
59
+ estree: {
60
+ type: "Program",
61
+ body: [
62
+ {
63
+ type: "ExpressionStatement",
64
+ expression: value
65
+ }
66
+ ]
67
+ }
68
+ }
69
+ }
70
+ };
71
+ }
72
+
73
+ // src/file-generator.ts
74
+ import * as fs from "fs/promises";
75
+ import * as path from "path";
76
+ import { z } from "zod";
77
+ var fileGeneratorSchema = z.object({
78
+ file: z.string(),
79
+ /**
80
+ * Turn file content into a code block
81
+ *
82
+ * @defaultValue false
83
+ */
84
+ codeblock: z.union([
85
+ z.object({
86
+ lang: z.string().optional(),
87
+ meta: z.string().optional()
88
+ }),
89
+ z.boolean()
90
+ ]).default(false)
91
+ });
92
+ function fileGenerator({
93
+ relative = false,
94
+ trim = true
95
+ } = {}) {
96
+ return {
97
+ name: "file",
98
+ run(input, ctx) {
99
+ return __async(this, null, function* () {
100
+ var _a2;
101
+ const { file, codeblock = false } = fileGeneratorSchema.parse(input);
102
+ const dest = relative ? path.resolve(ctx.cwd, path.dirname(ctx.path), file) : path.resolve(ctx.cwd, file);
103
+ let value = yield fs.readFile(dest).then((res) => res.toString());
104
+ if (trim) value = value.trim();
105
+ if (codeblock === false) {
106
+ return {
107
+ type: "paragraph",
108
+ children: [{ type: "text", value }]
109
+ };
110
+ }
111
+ const codeOptions = codeblock === true ? {} : codeblock;
112
+ return {
113
+ type: "code",
114
+ lang: (_a2 = codeOptions.lang) != null ? _a2 : path.extname(dest).slice(1),
115
+ meta: codeOptions.meta,
116
+ value
117
+ };
118
+ });
119
+ }
120
+ };
121
+ }
122
+
123
+ // src/remark-install.ts
124
+ import { visit as visit2 } from "unist-util-visit";
125
+ import convert from "npm-to-yarn";
126
+ function remarkInstall({
127
+ Tab = "Tab",
128
+ Tabs = "Tabs",
129
+ persist = false,
130
+ packageManagers = [
131
+ { command: (cmd) => convert(cmd, "npm"), name: "npm" },
132
+ { command: (cmd) => convert(cmd, "pnpm"), name: "pnpm" },
133
+ { command: (cmd) => convert(cmd, "yarn"), name: "yarn" },
134
+ { command: (cmd) => convert(cmd, "bun"), name: "bun" }
135
+ ]
136
+ } = {}) {
137
+ return (tree) => {
138
+ visit2(tree, "code", (node) => {
139
+ if (node.lang !== "package-install") return "skip";
140
+ const value = node.value.startsWith("npm") || node.value.startsWith("npx") ? node.value : `npm install ${node.value}`;
141
+ const insert = createElement(
142
+ Tabs,
143
+ [
144
+ ...typeof persist === "object" ? [
145
+ {
146
+ type: "mdxJsxAttribute",
147
+ name: "groupId",
148
+ value: persist.id
149
+ },
150
+ {
151
+ type: "mdxJsxAttribute",
152
+ name: "persist",
153
+ value: null
154
+ }
155
+ ] : [],
156
+ expressionToAttribute("items", {
157
+ type: "ArrayExpression",
158
+ elements: packageManagers.map(({ name }) => ({
159
+ type: "Literal",
160
+ value: name
161
+ }))
162
+ })
163
+ ],
164
+ packageManagers.map(({ command, name }) => ({
165
+ type: "mdxJsxFlowElement",
166
+ name: Tab,
167
+ attributes: [{ type: "mdxJsxAttribute", name: "value", value: name }],
168
+ children: [
169
+ {
170
+ type: "code",
171
+ lang: "bash",
172
+ meta: node.meta,
173
+ value: command(value)
174
+ }
175
+ ]
176
+ }))
177
+ );
178
+ Object.assign(node, insert);
179
+ });
180
+ };
181
+ }
182
+
183
+ // src/remark-show.ts
184
+ import { visit as visit3 } from "unist-util-visit";
185
+ function remarkShow(options) {
186
+ var _a;
187
+ const variables = (_a = options == null ? void 0 : options.variables) != null ? _a : {};
188
+ return (tree, file) => __async(null, null, function* () {
189
+ const { toJs } = yield import("estree-util-to-js");
190
+ const tasks = [];
191
+ visit3(tree, "mdxJsxFlowElement", (node) => {
192
+ var _a2;
193
+ if (node.name !== "show") return;
194
+ for (const attr of node.attributes) {
195
+ if (attr.type !== "mdxJsxAttribute" || attr.name !== "on") continue;
196
+ if (!attr.value || typeof attr.value !== "object" || !((_a2 = attr.value.data) == null ? void 0 : _a2.estree))
197
+ return "skip";
198
+ const js = toJs(attr.value.data.estree);
199
+ const callback = new Function(
200
+ ...Object.keys(variables),
201
+ `return ${js.value}`
202
+ )(...Object.values(variables));
203
+ tasks.push(
204
+ (() => __async(null, null, function* () {
205
+ const value = typeof callback === "function" ? yield callback(file) : callback;
206
+ Object.assign(node, {
207
+ type: "mdxJsxFlowElement",
208
+ name: null,
209
+ attributes: [],
210
+ children: value === true ? node.children : []
211
+ });
212
+ }))()
213
+ );
214
+ return "skip";
215
+ }
216
+ });
217
+ yield Promise.all(tasks);
218
+ });
219
+ }
220
+ export {
221
+ createElement,
222
+ expressionToAttribute,
223
+ fileGenerator,
224
+ fileGeneratorSchema,
225
+ remarkDocGen,
226
+ remarkInstall,
227
+ remarkShow
228
+ };
@@ -0,0 +1,35 @@
1
+ import { Transformer } from 'unified';
2
+ import { Root } from 'mdast';
3
+
4
+ interface TypeScriptToJavaScriptOptions {
5
+ /**
6
+ * Persist Tab value (Fumadocs UI only)
7
+ *
8
+ * @defaultValue false
9
+ */
10
+ persist?: {
11
+ id: string;
12
+ } | false;
13
+ defaultValue?: 'js' | 'ts';
14
+ /**
15
+ * Transform all TypeScript codeblocks by default, without a trigger
16
+ */
17
+ disableTrigger?: boolean;
18
+ }
19
+ /**
20
+ * A remark plugin to transform TypeScript codeblocks into two tabs of codeblocks with its JS variant.
21
+ *
22
+ * Add `ts2js` to enable transformation:
23
+ * ````md
24
+ * ```tsx ts2js
25
+ * import { ReactNode } from "react";
26
+ *
27
+ * export default function Layout({ children }: { children: ReactNode }) {
28
+ * return <div>{children}</div>
29
+ * }
30
+ * ```
31
+ * ````
32
+ */
33
+ declare function remarkTypeScriptToJavaScript({ persist, defaultValue, disableTrigger, }?: TypeScriptToJavaScriptOptions): Transformer<Root>;
34
+
35
+ export { type TypeScriptToJavaScriptOptions, remarkTypeScriptToJavaScript };
@@ -0,0 +1,84 @@
1
+ import {
2
+ __async
3
+ } from "./chunk-4AFQP74Z.js";
4
+
5
+ // src/remark-ts2js.ts
6
+ import { visit } from "unist-util-visit";
7
+ import {
8
+ generateCodeBlockTabs,
9
+ parseCodeBlockAttributes
10
+ } from "@hanzo/docs-core/mdx-plugins/codeblock-utils";
11
+ function remarkTypeScriptToJavaScript({
12
+ persist = false,
13
+ defaultValue = "ts",
14
+ disableTrigger = false
15
+ } = {}) {
16
+ return (tree, file) => __async(null, null, function* () {
17
+ const oxc = yield import("oxc-transform");
18
+ const tasks = [];
19
+ visit(tree, "code", (node) => {
20
+ var _a;
21
+ const lang = node.lang;
22
+ if (lang !== "ts" && lang !== "tsx") return;
23
+ const meta = parseCodeBlockAttributes((_a = node.meta) != null ? _a : "", ["ts2js"]);
24
+ if (!disableTrigger && !("ts2js" in meta.attributes)) return;
25
+ tasks.push(
26
+ (() => __async(null, null, function* () {
27
+ var _a2, _b;
28
+ const result = yield oxc.transform(
29
+ `${(_a2 = file.path) != null ? _a2 : "test"}.${lang}`,
30
+ node.value,
31
+ {
32
+ sourcemap: false,
33
+ jsx: "preserve"
34
+ }
35
+ );
36
+ const replacement = generateCodeBlockTabs({
37
+ persist,
38
+ defaultValue,
39
+ triggers: [
40
+ {
41
+ value: "ts",
42
+ children: [{ type: "text", value: "TypeScript" }]
43
+ },
44
+ {
45
+ value: "js",
46
+ children: [{ type: "text", value: "JavaScript" }]
47
+ }
48
+ ],
49
+ tabs: [
50
+ {
51
+ value: "ts",
52
+ children: [
53
+ {
54
+ type: "code",
55
+ lang: node.lang,
56
+ meta: meta.rest,
57
+ value: node.value
58
+ }
59
+ ]
60
+ },
61
+ {
62
+ value: "js",
63
+ children: [
64
+ {
65
+ type: "code",
66
+ lang: lang === "tsx" ? "jsx" : "js",
67
+ meta: (_b = meta.attributes.ts2js) != null ? _b : meta.rest,
68
+ value: result.code
69
+ }
70
+ ]
71
+ }
72
+ ]
73
+ });
74
+ Object.assign(node, replacement);
75
+ }))()
76
+ );
77
+ return "skip";
78
+ });
79
+ yield Promise.all(tasks);
80
+ });
81
+ }
82
+ export {
83
+ remarkTypeScriptToJavaScript
84
+ };
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@hanzo/docs-docgen",
3
+ "version": "3.0.4",
4
+ "description": "Useful remark utilities and plugins for Hanzo Docs",
5
+ "keywords": [
6
+ "Hanzo",
7
+ "Docs",
8
+ "remark",
9
+ "mdx",
10
+ "Docgen"
11
+ ],
12
+ "homepage": "https://hanzo.ai/docs",
13
+ "repository": "github:hanzoai/docs",
14
+ "license": "MIT",
15
+ "author": "Fuma Nama",
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "files": [
20
+ "dist/*"
21
+ ],
22
+ "exports": {
23
+ ".": {
24
+ "import": "./dist/index.js",
25
+ "types": "./dist/index.d.ts"
26
+ },
27
+ "./remark-ts2js": {
28
+ "import": "./dist/remark-ts2js.js",
29
+ "types": "./dist/remark-ts2js.d.ts"
30
+ }
31
+ },
32
+ "dependencies": {
33
+ "estree-util-to-js": "^2.0.0",
34
+ "estree-util-value-to-estree": "^3.5.0",
35
+ "npm-to-yarn": "^3.0.1",
36
+ "oxc-transform": "^0.102.0",
37
+ "unist-util-visit": "^5.0.0",
38
+ "zod": "^4.1.13"
39
+ },
40
+ "peerDependencies": {
41
+ "@hanzo/docs-core": "^15.7.2 || ^16.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@mdx-js/mdx": "^3.1.1",
45
+ "@types/estree": "^1.0.8",
46
+ "@types/hast": "^3.0.4",
47
+ "@types/mdast": "^4.0.4",
48
+ "@types/node": "24.10.2",
49
+ "mdast-util-mdx-jsx": "^3.2.0",
50
+ "remark": "^15.0.1",
51
+ "typescript": "^5.9.3",
52
+ "unified": "^11.0.5",
53
+ "vfile": "^6.0.3",
54
+ "eslint-config-custom": "0.0.0",
55
+ "@hanzo/docs-core": "16.2.6",
56
+ "tsconfig": "0.0.0"
57
+ },
58
+ "publishConfig": {
59
+ "access": "public"
60
+ },
61
+ "scripts": {
62
+ "build": "tsup",
63
+ "clean": "rimraf dist",
64
+ "dev": "tsup --watch",
65
+ "lint": "eslint .",
66
+ "types:check": "tsc --noEmit"
67
+ }
68
+ }