TypeScript方法自動(dòng)生成說(shuō)明文檔-typedoc實(shí)戰(zhàn)

一、準(zhǔn)備項(xiàng)目

1,可以是現(xiàn)有的項(xiàng)目

需要有typescript 插件

2、或者初始化一個(gè)項(xiàng)目

首先通過(guò) npm init -y 生成 package.json 文件,并按照我們的需要進(jìn)行修改:

// E:\www\front-utils\package.json
{
  "name": "learning-typedoc",
  "version": "1.0.0",
  "scripts": {}
}

之后通過(guò) npm install -D typescript 安裝 TypeScript,并創(chuàng)建 tsconfig.json 提供語(yǔ)言服務(wù):

// E:\www\front-utils\tsconfig.json
{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "sourceMap": false,
    "noEmit": true,
    "strict": true,
    "resolveJsonModule": true,
    "jsx": "preserve"
  },
  "include": ["./src/utils/*"]
}

二、新建導(dǎo)出方法

按照 tsconfig.json 中的 include 字段創(chuàng)建 ./src/utils/* 目錄,在其中編寫(xiě)自己的代碼。這里的代碼用于演示 TypeDoc 文檔生成,大家可以按照自己的想法自行修改。

// E:\www\front-utils\src\utils\index.ts
// 示例代碼,大家可以按照自己的想法自行修改
/** 接口 */
export interface MyInterface {
  /** 屬性1 */
  key1: number;

  /** 屬性2 */
  key2: string;
}

/** 類(lèi) */
export class MyClass {
  /** 類(lèi)的屬性 */
  prop1: number

  /** 構(gòu)造函數(shù) */
  constructor() {
    this.prop1 = this.privateMethod1(1, 2)
  }

  /**
   * 靜態(tài)方法
   * @param param 參數(shù),字符串列表
   * @returns 返回 Promise 對(duì)象
   */
  static staticMethod1(param: string[]) {
    return Promise.resolve(param)
  }

  /**
   * 私有方法
   * @param param1 第一個(gè)參數(shù)
   * @param param2 第二個(gè)參數(shù)
   * @returns 兩數(shù)之和
   */
  private privateMethod1(param1: number, param2: number) {
    return param1 + param2
  }
  
  /** 公共方法 */
  publicMethod(param1: number, param2: number) {
    return this.prop1 + param1 + param2
  }
}

/** 類(lèi)型 */
export type MyType = 1 | 2 | 3 | 4

/**
 * 函數(shù)
 * @param param 參數(shù)
 */
export function myFunction(param: MyInterface) {
  return param
}

/** 沒(méi)有導(dǎo)出的成員,不會(huì)出現(xiàn)在文檔中 */
class MyClassNotExport {}

三、創(chuàng)建文檔導(dǎo)出項(xiàng)目

1,創(chuàng)建 doc 目錄,

在此目錄中放置所有與文檔相關(guān)的內(nèi)容,在此目錄下也創(chuàng)建 package.json 文件。

E:\www\front-utils> cd doc
E:E:\www\front-utils\doc> npm init -y
// E:\www\front-utils\doc\package.json
{
  "name": "doc",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

目前的目錄結(jié)構(gòu)如下:

learning-typedoc
├─ doc
│  └─ package.json
├─ src
│  └─ utils
│      └─ index.ts
├─ package.json
└─ tsconfig.json

四、TypeDoc 基本使用

1,在 doc 目錄下安裝 TypeDoc:
E:\www\front-utils\doc> npm install -D typedoc
2,TypeDoc 配置

創(chuàng)建TypeDoc 配置文件:typedoc.json

// E:\www\front-utils\doc\typedoc.json
{
    "$schema": "https://typedoc.org/schema.json",
    "entryPoints": ["../src/utils/*"], // 方法文件入口
    "out": "./dist" // 指定的導(dǎo)出文檔目錄
}
3,生成html文檔

通過(guò)命令 typedoc --options <filename>,就可以以配置文件中指定的選項(xiàng)運(yùn)行。

E:\www\front-utils\doc> npx typedoc --options ./typedoc.json

之后就生成文檔doc\dist,打開(kāi)index.html文件即可查看生成的文檔

五、生成markdown文檔

1,安裝typedoc-plugin-markdown插件
E:\www\front-utils\doc> npm i -D typedoc-plugin-markdown
2,配置markdown配置
// E:\www\front-utils\doc\typedoc.json
{
    "$schema": "https://typedoc.org/schema.json",
    "entryPoints": ["../src/utils/*"],
    "plugin": ["typedoc-plugin-markdown"], // 新增
    "out": "./dist"
}
3,生成markdown文檔
E:\www\front-utils\doc> npx typedoc --options ./typedoc.json

生成的markdown文檔在E:\www\front-utils\doc目錄

參考:https://juejin.cn/post/7310980511454232591

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容