Git Product home page Git Product logo

ts-document's Introduction

ts-document

Auto generate ts document schema by ts interface conform to the TSDoc.

Highlight

  • Controllable parameter extraction, only the specified interface is extracted.
  • Automatically analyze extends relationships.
  • Support extract English and Chinese in one ts file.
  • Support generate markdown string directly.

Usage

npm i ts-document -D
const { generate, generateMarkdown } = require('ts-document');

generate('interface.ts', config);

generateMarkdown('interface.ts', config);

interface.ts

ts-document will only extract interface and type with jsDoc tag title

import { ReactNode } from 'react';

/**
 * @title Alert
 *
 * @zh
 *
 * 向用户显示警告的信息时,通过警告提示,展现需要关注的信息。
 *
 * @en
 *
 * Display warning information to the user. the Alert is used to display the information that needs attention.
 */
export interface AlertProps {
  /**
   * @zh 自定义操作项
   * @en this is action
   * @version 2.15.0
   */
  action?: ReactNode;
  /**
   * @zh 是否可以关闭
   * @en Whether Alert can be closed
   * @defaultValue false
   */
  closable?: InnerProps;
}

interface InnerProps {
  /**
   * @zh 位置
   * @en position
   */
  position?: string;
  /**
   * @zh 尺寸
   * @en Size
   */
  size?: string;
}

Generate jsDoc schema

const { generate } = require('ts-document');

generate('interface.ts');

output

{
  "Alert": {
    "data": [
      {
        "name": "action",
        "type": "ReactNode",
        "isOptional": true,
        "tags": [
          {
            "name": "zh",
            "value": "自定义操作项"
          },
          {
            "name": "en",
            "value": "this is action"
          },
          {
            "name": "version",
            "value": "2.15.0"
          }
        ]
      },
      {
        "name": "closable",
        "type": "InnerProps",
        "isOptional": true,
        "tags": [
          {
            "name": "zh",
            "value": "是否可以关闭"
          },
          {
            "name": "en",
            "value": "Whether Alert can be closed"
          },
          {
            "name": "defaultValue",
            "value": "false"
          }
        ]
      }
    ],
    "tags": [
      {
        "name": "title",
        "value": "Alert"
      },
      {
        "name": "zh",
        "value": "向用户显示警告的信息时,通过警告提示,展现需要关注的信息。"
      },
      {
        "name": "en",
        "value": "Display warning information to the user. the Alert is used to display the information that needs attention."
      }
    ]
  }
}

Generate markdown document

const { generateMarkdown } = require('ts-document');

generateMarkdown('interface.ts');

output

{
  "Alert": "### Alert\n\nDisplay warning information to the user. the Alert is used to display the information that needs attention.\n\n|Property|Description|Type|DefaultValue|Version|\n|---|---|---|---|---|\n|action|this is action|`ReactNode`|`-`|2.15.0|\n|closable|Whether Alert can be closed|`InnerProps`|`false`|-|"
}

Config

defaultTypeMap

Record<string, { type: string, tags?: TagType[] }>

If no comments are extracted, will extracted from the defaultTypeMap automatically.

sourceFilesPaths

string | string[]

See ts-morph

strictComment

boolean

Whether to skip parsing documentation comment like /** Some comment **/ as property description if there is no tag like @en or @zh.

strictDeclarationOrder

boolean

Generate schema in the order their declarations appear in the document. When it's true, generate/generateMarkdown will return an array list(Array<{ title: string; schema: Schema }>).

propertySorter

(a: { name: string; type: string; isOptional: boolean; tags: Array<{ name: string; value: string }>; }, b: typeof a) => number

The compare function to sort properties/arguments of schema generated.

lang

string

Only work in generateMarkdown, specify output language.

project

Project

Custom project to use in generate/generateMarkdown function. See ts-morph

linkFormatter

({ typeName: string, jsDocTitle?: string, fullPath: string }) => string

Format function to generate link of nested types.

ignoreNestedType

When parsing nested types, whether to ignore these nested types if they are defined in some files. When returning true, nested types must not be exported, but when false is returned, nested types may not be exported due to other reasons (such as the nested type has jsdoc @title, which needs to be manually exported)

escapeChars

boolean: default to true

Whether to escape characters for extracted type text. E.g. | will be escaped to \|, <Promise> will be escaped to &lt;Promise&gt;.

Who's using?

Arco Design - A comprehensive React UI components library based on Arco Design.

LICENSE

MIT © PengJiyuan

ts-document's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ts-document's Issues

Support @returns i18n

I have tried

 * @returns {en} A tuple with the following elements:
 * - width
 * - height
 * @returns {zh} 包含以下元素的元组:
 * - 元素宽度。
 * - 元素高度。

but it seems don't work, it always choose the last @returns description

Enum template tranform exception

raw code

enum ExperimentType {
  a: 1,
  b: 2
}

interface Test {
  manageType: `${ExperimentType}`;
}

after ts-document transformed

the ${ExperimentType} will be transformed to

${ExperimentType} (必填)

expected

the ${ExperimentType} will be transformed to

1 | 2

Support for more type extraction

Now only support function or interface comment extract, but const and class also commonly used

const bind = () => {}

class Event {

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.