doc-render-sdk 0.0.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.
package/README.md ADDED
@@ -0,0 +1,321 @@
1
+ # 文档渲染SDK
2
+
3
+ 一个专门用于渲染组件文档站点的SDK,从原有的doc目录抽象而来。
4
+
5
+ ## 特性
6
+
7
+ - 🎨 **主题化设计** - 支持自定义主题和样式
8
+ - 📱 **响应式布局** - 适配各种屏幕尺寸
9
+ - 🔧 **组件化架构** - 高度可复用的文档组件
10
+ - 📖 **Demo展示** - 支持代码预览、实时运行、代码折叠
11
+ - 📋 **API文档** - 结构化的API参数文档
12
+ - 🚀 **热更新** - 开发时支持热更新
13
+ - 🎯 **插件系统** - 支持自定义插件扩展功能
14
+ - 📦 **零配置** - 开箱即用,同时支持深度定制
15
+
16
+ ## 快速开始
17
+
18
+ ### 安装
19
+
20
+ ```bash
21
+ npm install doc-sdk
22
+ ```
23
+
24
+ ### 基本使用
25
+
26
+ ```javascript
27
+ import DocSDK from 'doc-sdk';
28
+
29
+ const docSdk = new DocSDK({
30
+ title: '我的组件库文档',
31
+ theme: 'default',
32
+ components: {
33
+ 'my-component': {
34
+ label: '我的组件',
35
+ demos: [
36
+ {
37
+ title: '基础用法',
38
+ desc: '组件的基础使用方式',
39
+ source: 'basic'
40
+ }
41
+ ],
42
+ apis: [
43
+ {
44
+ title: 'MyComponent',
45
+ apiKey: 'my-component'
46
+ }
47
+ ]
48
+ }
49
+ }
50
+ });
51
+
52
+ docSdk.render('#app');
53
+ ```
54
+
55
+ ## 架构设计
56
+
57
+ ### 核心模块
58
+
59
+ - **DocRenderer** - 文档渲染器核心
60
+ - **ConfigManager** - 配置管理器
61
+ - **ComponentRegistry** - 组件注册表
62
+ - **ThemeManager** - 主题管理器
63
+ - **PluginManager** - 插件管理器
64
+ - **RouterManager** - 路由管理器
65
+
66
+ ### 组件系统
67
+
68
+ - **Layout** - 布局组件
69
+ - **Navigation** - 导航组件
70
+ - **Demo** - 演示组件
71
+ - **ApiDoc** - API文档组件
72
+ - **CodeBlock** - 代码块组件
73
+ - **Search** - 搜索组件
74
+
75
+ ## 目录结构
76
+
77
+ ```
78
+ doc-sdk/
79
+ ├── src/
80
+ │ ├── core/ # 核心模块
81
+ │ │ ├── DocRenderer.js # 文档渲染器
82
+ │ │ ├── ConfigManager.js # 配置管理器
83
+ │ │ ├── ComponentRegistry.js # 组件注册表
84
+ │ │ ├── ThemeManager.js # 主题管理器
85
+ │ │ ├── PluginManager.js # 插件管理器
86
+ │ │ └── RouterManager.js # 路由管理器
87
+ │ ├── components/ # 文档组件
88
+ │ │ ├── Layout.jsx # 主布局组件
89
+ │ │ ├── Navigation.jsx # 导航组件
90
+ │ │ ├── Header.jsx # 头部组件
91
+ │ │ ├── Content.jsx # 内容组件
92
+ │ │ ├── Demo.jsx # Demo展示组件
93
+ │ │ ├── ApiDoc.jsx # API文档组件
94
+ │ │ ├── CodeBlock.jsx # 代码块组件
95
+ │ │ ├── Search.jsx # 搜索组件
96
+ │ │ ├── Home.jsx # 首页组件
97
+ │ │ └── Footer.jsx # 页脚组件
98
+ │ ├── themes/ # 主题文件
99
+ │ │ └── default.js # 默认主题
100
+ │ ├── config/ # 配置文件
101
+ │ │ └── default.js # 默认配置
102
+ │ ├── utils/ # 工具函数
103
+ │ │ └── index.js # 工具函数集合
104
+ │ └── index.js # 入口文件
105
+ ├── bin/ # CLI工具
106
+ │ └── doc-sdk.js # 命令行工具
107
+ ├── tools/ # 构建工具
108
+ │ └── migrate.js # 迁移工具
109
+ ├── examples/ # 示例项目
110
+ │ ├── index.html # 示例HTML
111
+ │ └── index.js # 示例配置
112
+ ├── lib/ # CommonJS构建输出
113
+ ├── es/ # ES模块构建输出
114
+ ├── docs-new/ # 迁移示例
115
+ └── README.md # 说明文档
116
+ ```
117
+
118
+ ## 🎯 设计理念
119
+
120
+ ### 核心原则
121
+
122
+ 1. **组件化架构** - 高度模块化,每个功能都是独立的组件
123
+ 2. **配置驱动** - 通过配置文件控制所有行为,减少硬编码
124
+ 3. **主题化设计** - 支持深度定制,满足不同项目需求
125
+ 4. **插件扩展** - 提供插件系统,支持功能扩展
126
+ 5. **开发友好** - 提供完整的开发工具链和迁移工具
127
+
128
+ ### 技术栈
129
+
130
+ - **React** - 组件渲染
131
+ - **Webpack** - 模块打包
132
+ - **Babel** - 代码转换
133
+ - **Less/CSS** - 样式处理
134
+ - **Highlight.js** - 代码高亮
135
+ - **Mini-event** - 事件系统
136
+
137
+ ## 🚀 快速开始
138
+
139
+ ### 安装
140
+
141
+ ```bash
142
+ npm install doc-sdk
143
+ ```
144
+
145
+ ### 基本使用
146
+
147
+ ```javascript
148
+ import DocSDK from 'doc-sdk';
149
+
150
+ const docSdk = new DocSDK({
151
+ title: '我的组件库',
152
+ components: {
153
+ 'button': {
154
+ label: 'Button 按钮',
155
+ demos: [
156
+ {
157
+ title: '基础用法',
158
+ desc: '最简单的用法',
159
+ source: 'basic'
160
+ }
161
+ ],
162
+ apis: [
163
+ {
164
+ title: 'Button',
165
+ apiKey: 'Button'
166
+ }
167
+ ]
168
+ }
169
+ }
170
+ });
171
+
172
+ docSdk.render('#app');
173
+ ```
174
+
175
+ ### CLI 工具
176
+
177
+ ```bash
178
+ # 创建新项目
179
+ npx doc-sdk create my-docs
180
+
181
+ # 启动开发服务器
182
+ npx doc-sdk dev
183
+
184
+ # 构建项目
185
+ npx doc-sdk build
186
+
187
+ # 迁移旧项目
188
+ npx doc-sdk migrate --source ./doc --target ./docs-new
189
+ ```
190
+
191
+ ## 📚 详细文档
192
+
193
+ ### 配置选项
194
+
195
+ ```javascript
196
+ const config = {
197
+ // 基本信息
198
+ title: 'Documentation',
199
+ description: 'Component Documentation Site',
200
+ version: '1.0.0',
201
+
202
+ // 主题配置
203
+ theme: {
204
+ name: 'default',
205
+ colors: {
206
+ primary: '#1890ff'
207
+ }
208
+ },
209
+
210
+ // 布局配置
211
+ layout: {
212
+ type: 'sidebar', // sidebar, top, mixed
213
+ sidebar: {
214
+ width: 280,
215
+ collapsible: true
216
+ }
217
+ },
218
+
219
+ // 组件配置
220
+ components: {
221
+ 'component-name': {
222
+ label: '组件名称',
223
+ description: '组件描述',
224
+ demos: [...],
225
+ apis: [...]
226
+ }
227
+ },
228
+
229
+ // 搜索配置
230
+ search: {
231
+ enabled: true,
232
+ placeholder: '搜索...'
233
+ }
234
+ };
235
+ ```
236
+
237
+ ### 组件注册
238
+
239
+ ```javascript
240
+ // 注册Demo组件
241
+ window.__DOC_SDK_DEMOS__ = {
242
+ 'component-name': {
243
+ 'demo-name': DemoComponent
244
+ }
245
+ };
246
+
247
+ // 注册Demo代码
248
+ window.__DOC_SDK_DEMO_CODES__ = {
249
+ 'component-name': {
250
+ 'demo-name': 'const Demo = () => <div>Hello</div>;'
251
+ }
252
+ };
253
+
254
+ // 注册API文档
255
+ window.__DOC_SDK_APIS__ = {
256
+ 'component-name': {
257
+ 'api-name': [
258
+ {
259
+ param: 'prop',
260
+ type: 'string',
261
+ desc: '属性描述',
262
+ default: '',
263
+ required: false
264
+ }
265
+ ]
266
+ }
267
+ };
268
+ ```
269
+
270
+ ## 🔧 高级功能
271
+
272
+ ### 自定义主题
273
+
274
+ ```javascript
275
+ const customTheme = {
276
+ name: 'custom',
277
+ colors: {
278
+ primary: '#ff6b6b',
279
+ success: '#51cf66',
280
+ warning: '#ffd43b',
281
+ error: '#ff6b6b'
282
+ },
283
+ typography: {
284
+ fontFamily: 'Inter, sans-serif'
285
+ },
286
+ components: {
287
+ demo: `
288
+ .doc-demo {
289
+ border-radius: 12px;
290
+ box-shadow: 0 4px 12px rgba(0,0,0,0.1);
291
+ }
292
+ `
293
+ }
294
+ };
295
+
296
+ const docSdk = new DocSDK({
297
+ theme: customTheme
298
+ });
299
+ ```
300
+
301
+ ### 插件开发
302
+
303
+ ```javascript
304
+ const myPlugin = {
305
+ name: 'my-plugin',
306
+ version: '1.0.0',
307
+ install(context) {
308
+ // 添加钩子
309
+ context.hooks.add('beforeRender', () => {
310
+ console.log('Before render');
311
+ });
312
+
313
+ // 监听事件
314
+ context.events.on('routeChange', (route) => {
315
+ console.log('Route changed:', route);
316
+ });
317
+ }
318
+ };
319
+
320
+ docSdk.use(myPlugin);
321
+ ```
package/bin/doc-sdk.js ADDED
@@ -0,0 +1,348 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Doc SDK CLI 工具
5
+ */
6
+
7
+ const { program } = require('commander');
8
+ const path = require('path');
9
+ const fs = require('fs');
10
+ const { spawn } = require('child_process');
11
+
12
+ program
13
+ .name('doc-sdk')
14
+ .description('Doc SDK CLI - 文档站点构建工具')
15
+ .version('1.0.0');
16
+
17
+ // 创建新项目
18
+ program
19
+ .command('create <project-name>')
20
+ .description('创建新的文档项目')
21
+ .option('-t, --template <template>', '使用模板', 'default')
22
+ .action(async (projectName, options) => {
23
+ console.log(`🚀 创建文档项目: ${projectName}`);
24
+
25
+ const projectDir = path.resolve(projectName);
26
+
27
+ if (fs.existsSync(projectDir)) {
28
+ console.error(`❌ 目录已存在: ${projectDir}`);
29
+ process.exit(1);
30
+ }
31
+
32
+ try {
33
+ await createProject(projectDir, options.template);
34
+ console.log('✅ 项目创建成功!');
35
+ console.log(`📁 项目目录: ${projectDir}`);
36
+ console.log('\n下一步:');
37
+ console.log(` cd ${projectName}`);
38
+ console.log(' npm install');
39
+ console.log(' npm run dev');
40
+ } catch (error) {
41
+ console.error('❌ 创建失败:', error.message);
42
+ process.exit(1);
43
+ }
44
+ });
45
+
46
+ // 开发服务器
47
+ program
48
+ .command('dev')
49
+ .description('启动开发服务器')
50
+ .option('-p, --port <port>', '端口号', '8080')
51
+ .option('-h, --host <host>', '主机地址', 'localhost')
52
+ .action((options) => {
53
+ console.log('🚀 启动开发服务器...');
54
+
55
+ const configPath = findConfig();
56
+ if (!configPath) {
57
+ console.error('❌ 未找到配置文件');
58
+ process.exit(1);
59
+ }
60
+
61
+ startDevServer(configPath, options);
62
+ });
63
+
64
+ // 构建项目
65
+ program
66
+ .command('build')
67
+ .description('构建文档站点')
68
+ .option('-o, --output <dir>', '输出目录', 'dist')
69
+ .action((options) => {
70
+ console.log('📦 构建文档站点...');
71
+
72
+ const configPath = findConfig();
73
+ if (!configPath) {
74
+ console.error('❌ 未找到配置文件');
75
+ process.exit(1);
76
+ }
77
+
78
+ buildProject(configPath, options);
79
+ });
80
+
81
+
82
+ // 预览构建结果
83
+ program
84
+ .command('preview')
85
+ .description('预览构建结果')
86
+ .option('-p, --port <port>', '端口号', '3000')
87
+ .option('-d, --dir <dir>', '构建目录', 'dist')
88
+ .action((options) => {
89
+ console.log('👀 预览构建结果...');
90
+ previewBuild(options);
91
+ });
92
+
93
+ program.parse();
94
+
95
+ /**
96
+ * 创建新项目
97
+ */
98
+ async function createProject(projectDir, template) {
99
+ // 创建项目目录
100
+ fs.mkdirSync(projectDir, { recursive: true });
101
+
102
+ // 生成package.json
103
+ const packageJson = {
104
+ name: path.basename(projectDir),
105
+ version: '1.0.0',
106
+ description: 'Documentation site built with Doc SDK',
107
+ main: 'index.js',
108
+ scripts: {
109
+ dev: 'doc-sdk dev',
110
+ build: 'doc-sdk build',
111
+ preview: 'doc-sdk preview'
112
+ },
113
+ dependencies: {
114
+ 'doc-sdk': '^1.0.0'
115
+ },
116
+ devDependencies: {
117
+ webpack: '^5.88.0',
118
+ 'webpack-cli': '^5.1.0',
119
+ 'webpack-dev-server': '^4.15.0'
120
+ }
121
+ };
122
+
123
+ fs.writeFileSync(
124
+ path.join(projectDir, 'package.json'),
125
+ JSON.stringify(packageJson, null, 2)
126
+ );
127
+
128
+ // 生成配置文件
129
+ const config = {
130
+ title: 'My Documentation',
131
+ description: 'Component documentation built with Doc SDK',
132
+ version: '1.0.0',
133
+ components: {
134
+ 'example': {
135
+ label: 'Example Component',
136
+ description: 'An example component to get you started',
137
+ demos: [
138
+ {
139
+ title: 'Basic Usage',
140
+ desc: 'Basic usage of the component',
141
+ source: 'basic'
142
+ }
143
+ ],
144
+ apis: [
145
+ {
146
+ title: 'Example',
147
+ apiKey: 'Example'
148
+ }
149
+ ]
150
+ }
151
+ }
152
+ };
153
+
154
+ fs.writeFileSync(
155
+ path.join(projectDir, 'doc.config.js'),
156
+ `export default ${JSON.stringify(config, null, 2)};`
157
+ );
158
+
159
+ // 生成入口文件
160
+ const indexJs = `import DocSDK from 'doc-sdk';
161
+ import config from './doc.config.js';
162
+
163
+ // 注册示例组件
164
+ const ExampleComponent = () => {
165
+ return (
166
+ <div style={{ padding: '20px' }}>
167
+ <h3>Example Component</h3>
168
+ <p>This is an example component.</p>
169
+ </div>
170
+ );
171
+ };
172
+
173
+ // 注册全局组件
174
+ window.__DOC_SDK_DEMOS__ = {
175
+ 'example': {
176
+ 'basic': ExampleComponent
177
+ }
178
+ };
179
+
180
+ window.__DOC_SDK_DEMO_CODES__ = {
181
+ 'example': {
182
+ 'basic': \`const ExampleComponent = () => {
183
+ return (
184
+ <div style={{ padding: '20px' }}>
185
+ <h3>Example Component</h3>
186
+ <p>This is an example component.</p>
187
+ </div>
188
+ );
189
+ };\`
190
+ }
191
+ };
192
+
193
+ window.__DOC_SDK_APIS__ = {
194
+ 'example': {
195
+ 'Example': [
196
+ {
197
+ param: 'children',
198
+ type: 'ReactNode',
199
+ desc: 'The content of the component',
200
+ option: '',
201
+ default: '',
202
+ required: false
203
+ }
204
+ ]
205
+ }
206
+ };
207
+
208
+ const docSdk = new DocSDK(config);
209
+ docSdk.render('#app');
210
+ `;
211
+
212
+ fs.writeFileSync(path.join(projectDir, 'index.js'), indexJs);
213
+
214
+ // 生成HTML文件
215
+ const indexHtml = `<!DOCTYPE html>
216
+ <html lang="zh-CN">
217
+ <head>
218
+ <meta charset="UTF-8">
219
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
220
+ <title>My Documentation</title>
221
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css">
222
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
223
+ </head>
224
+ <body>
225
+ <div id="app"></div>
226
+ </body>
227
+ </html>`;
228
+
229
+ fs.writeFileSync(path.join(projectDir, 'index.html'), indexHtml);
230
+
231
+ // 生成README
232
+ const readme = `# ${path.basename(projectDir)}
233
+
234
+ Documentation site built with Doc SDK.
235
+
236
+ ## Getting Started
237
+
238
+ \`\`\`bash
239
+ npm install
240
+ npm run dev
241
+ \`\`\`
242
+
243
+ ## Commands
244
+
245
+ - \`npm run dev\` - Start development server
246
+ - \`npm run build\` - Build for production
247
+ - \`npm run preview\` - Preview build
248
+
249
+ ## Documentation
250
+
251
+ - [Doc SDK Documentation](https://github.com/your-org/doc-sdk)
252
+ `;
253
+
254
+ fs.writeFileSync(path.join(projectDir, 'README.md'), readme);
255
+ }
256
+
257
+ /**
258
+ * 查找配置文件
259
+ */
260
+ function findConfig() {
261
+ const configFiles = [
262
+ 'doc.config.js',
263
+ 'doc.config.json',
264
+ 'docs.config.js',
265
+ 'docs.config.json'
266
+ ];
267
+
268
+ for (const file of configFiles) {
269
+ if (fs.existsSync(file)) {
270
+ return path.resolve(file);
271
+ }
272
+ }
273
+
274
+ return null;
275
+ }
276
+
277
+ /**
278
+ * 启动开发服务器
279
+ */
280
+ function startDevServer(configPath, options) {
281
+ const webpackConfig = generateWebpackConfig(configPath, 'development', options);
282
+ const configFile = path.join(__dirname, '../webpack.temp.js');
283
+
284
+ fs.writeFileSync(configFile, `module.exports = ${JSON.stringify(webpackConfig, null, 2)};`);
285
+
286
+ const child = spawn('npx', ['webpack', 'serve', '--config', configFile], {
287
+ stdio: 'inherit',
288
+ shell: true
289
+ });
290
+
291
+ child.on('close', (code) => {
292
+ fs.unlinkSync(configFile);
293
+ process.exit(code);
294
+ });
295
+ }
296
+
297
+ /**
298
+ * 构建项目
299
+ */
300
+ function buildProject(configPath, options) {
301
+ const webpackConfig = generateWebpackConfig(configPath, 'production', options);
302
+ const configFile = path.join(__dirname, '../webpack.temp.js');
303
+
304
+ fs.writeFileSync(configFile, `module.exports = ${JSON.stringify(webpackConfig, null, 2)};`);
305
+
306
+ const child = spawn('npx', ['webpack', '--config', configFile], {
307
+ stdio: 'inherit',
308
+ shell: true
309
+ });
310
+
311
+ child.on('close', (code) => {
312
+ fs.unlinkSync(configFile);
313
+ if (code === 0) {
314
+ console.log('✅ 构建完成!');
315
+ }
316
+ process.exit(code);
317
+ });
318
+ }
319
+
320
+ /**
321
+ * 预览构建结果
322
+ */
323
+ function previewBuild(options) {
324
+ const express = require('express');
325
+ const app = express();
326
+
327
+ app.use(express.static(options.dir));
328
+
329
+ app.listen(options.port, () => {
330
+ console.log(`📖 预览地址: http://localhost:${options.port}`);
331
+ });
332
+ }
333
+
334
+ /**
335
+ * 生成webpack配置
336
+ */
337
+ function generateWebpackConfig(configPath, mode, options) {
338
+ // 这里应该生成完整的webpack配置
339
+ // 简化版本,实际使用时需要完善
340
+ return {
341
+ mode,
342
+ entry: './index.js',
343
+ output: {
344
+ path: path.resolve(options.output || 'dist'),
345
+ filename: 'bundle.js'
346
+ }
347
+ };
348
+ }