SiYuan与Angular文档生成工具集成方案

SiYuan与Angular文档生成工具集成方案

【免费下载链接】siyuan A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. 【免费下载链接】siyuan 项目地址: https://gitcode.com/GitHub_Trending/si/siyuan

方案背景与目标

SiYuan作为一款隐私优先的个人知识管理软件,其前端采用TypeScript构建(app/src),核心功能模块包括编辑器(Editor)、文档对象模型(Doc)和布局管理(Layout)等关键组件。当需要为这类复杂TypeScript项目构建结构化API文档时,Angular生态的Compodoc工具展现出显著优势——它能自动解析TypeScript源码生成交互式文档,并支持与知识管理系统深度集成。本方案将实现"源码即文档"的开发模式,通过Compodoc生成API文档并自动同步至SiYuan知识库,形成"代码-文档-知识"整合管理闭环。

技术选型与环境准备

核心工具链组合

工具用途集成优势
CompodocTypeScript文档生成支持Angular风格注解,生成响应式文档站点
SiYuan API文档数据管理通过block API实现文档块自动化创建
Webpack构建流程集成可扩展现有构建链(app/webpack.config.js)

环境依赖检查

在开始集成前需确保开发环境已安装:

  • Node.js (v16+) 及pnpm包管理器
  • TypeScript 4.7+(项目已满足:app/package.json#L76
  • 最新版Compodoc:pnpm add -D @compodoc/compodoc

实现步骤

1. 源码结构分析与配置

通过分析app/src目录的顶层代码定义,核心业务模块包括:

// 关键类定义示例(源自[app/src/editor/index.ts](https://link.gitcode.com/i/625e7bbb0843b362ba8c8e9e9236d66e))
export class Editor {
  public protyle: Protyle;
  public doc: Doc;
  
  constructor(options: IEditorOptions) {
    this.initProtyle(options.element);
    this.bindEvents();
  }
  
  // 文档操作核心方法
  public async saveDoc(): Promise<boolean> {
    // 实现逻辑...
  }
}

创建compodoc.json配置文件,指定源码路径与输出目录:

{
  "tsconfig": "app/tsconfig.json",
  "outputDir": "docs/compodoc",
  "include": ["app/src/**/*.ts"],
  "exclude": ["**/*test.ts"],
  "theme": "gitbook",
  "hideGenerator": true
}

2. 集成Compodoc到构建流程

修改app/package.json的scripts部分,添加文档生成命令:

{
  "scripts": {
    "doc:generate": "compodoc -p app/tsconfig.json",
    "doc:serve": "compodoc -s -p app/tsconfig.json",
    "doc:sync": "node scripts/sync-docs.js" // 后续同步脚本
  }
}

执行pnpm run doc:generate将在项目根目录生成静态文档站点,典型输出日志如下:

√ Parse completed successfully
√ Generate documentation in docs/compodoc
√ Documentation generated in 23.45 seconds

3. 文档自动同步至SiYuan

创建同步脚本scripts/sync-docs.js,通过SiYuan的HTTP API将生成的HTML文档转换为知识块:

const { request } = require('https');
const fs = require('fs');
const path = require('path');

// 读取Compodoc生成的HTML文件
const docPath = path.resolve(__dirname, '../docs/compodoc/index.html');
const docContent = fs.readFileSync(docPath, 'utf-8');

// 调用SiYuan块创建API([kernel/api/block.go](https://link.gitcode.com/i/c8da02425ecfae19bb790a413591cd81))
const postData = JSON.stringify({
  notebook: '20230101120000-abcdef',
  path: '/开发文档/API参考',
  content: `::: document\n${docContent}\n:::`
});

const options = {
  hostname: 'localhost',
  port: 6806,
  path: '/api/block/createBlock',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Token YOUR_API_TOKEN'
  }
};

const req = request(options, (res) => {
  if (res.statusCode === 200) {
    console.log('文档块创建成功');
  }
});

req.write(postData);
req.end();

4. 可视化集成效果

配置完成后,执行pnpm run doc:serve可启动本地文档服务。通过SiYuan的Web视图功能嵌入文档站点,实现知识管理与API文档的无缝访问:

文档集成效果

高级配置与优化

自定义文档模板

通过扩展Compodoc主题模板,可将SiYuan特有的块语法(如属性视图)融入生成文档:

<!-- 自定义模板示例:添加SiYuan块引用按钮 -->
<div class="siyuan-actions">
  <button onclick="copyAsBlock('{{symbol.name}}')">
    <i class="icon siyuan-icon"></i> 复制为SiYuan块
  </button>
</div>

构建流程自动化

app/webpack.desktop.js中添加文档生成插件:

const { execSync } = require('child_process');

class DocGeneratorPlugin {
  apply(compiler) {
    compiler.hooks.afterEmit.tap('DocGeneratorPlugin', () => {
      if (process.env.NODE_ENV === 'production') {
        execSync('pnpm run doc:generate', { stdio: 'inherit' });
      }
    });
  }
}

// 在plugins数组中添加
module.exports = {
  plugins: [
    new DocGeneratorPlugin(),
    // 其他现有插件...
  ]
};

常见问题解决

类型定义解析不完整

现象:Compodoc无法识别某些接口定义
解决方案:确保tsconfig.json中启用declaration: true(app/tsconfig.json),并检查exclude配置是否过滤了必要文件。

文档体积优化

生成的文档站点初始体积可能超过50MB,可通过以下方式优化:

  • 启用Compodoc的minify选项
  • 排除测试文件和示例代码
  • 配置nginx gzip压缩静态资源

集成效果展示

文档浏览界面

通过SiYuan的iframe小部件嵌入Compodoc文档,实现知识关联与API查阅的无缝切换:

文档嵌入效果

代码与文档双向链接

在TypeScript源码中使用JSDoc注解关联SiYuan文档块ID:

/**
 * 文档块操作工具类
 * @see {@link https://siyuan.example.com/#20230518120000-abcdef | SiYuan文档块}
 */
export class BlockUtils {
  // 实现代码...
}

总结与扩展方向

本方案通过Compodoc与SiYuan的结合,实现了三个核心价值:

  1. 开发效率提升:源码注解驱动的文档生成减少重复劳动
  2. 知识管理闭环:API文档作为知识资产纳入SiYuan统一管理
  3. 团队协作优化:结构化文档降低新成员上手门槛

未来可探索的扩展方向:

  • 基于SiYuan AI功能(app/src/ai)实现文档智能问答
  • 开发Compodoc插件支持SiYuan专有语法高亮
  • 通过WebDAV协议实现文档自动同步(kernel/api/dav.go)

完整实现代码与配置示例已同步至项目scripts/doc-integration/目录,可直接参考使用。

【免费下载链接】siyuan A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. 【免费下载链接】siyuan 项目地址: https://gitcode.com/GitHub_Trending/si/siyuan

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值