Coder Social home page Coder Social logo

kanglicheng / generate-ts-docs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yuanqing/generate-ts-docs

0.0 0.0 0.0 410 KB

:blue_book: Utilities to parse type information and JSDoc annotations from TypeScript source files, and render Markdown documentation

License: MIT License

TypeScript 100.00%

generate-ts-docs's Introduction

generate-ts-docs npm Version build

Utilities to parse type information and JSDoc annotations from TypeScript source files, and render Markdown documentation

Usage

First:

$ npm install --save-dev generate-ts-docs

Suppose that we have the following (highly contrived) toy example.ts source file:

/**
 * Adds two numbers.
 *
 * @param x  First number to add.
 * @param y  Second number to add.
 * @return The sum of `x` and `y`.
 */
export function add(x: number, y: number): number {
  return x + y
}

…and the following generate-ts-docs.ts script:

import {
  parseExportedFunctionsAsync,
  renderFunctionDataToMarkdown
} from 'generate-ts-docs'

async function main() {
  const functionsData = await parseExportedFunctionsAsync(['./example.ts'])
  for (const functionData of functionsData) {
    console.log(renderFunctionDataToMarkdown(functionData))
  }
}
main()

parseExportedFunctionsAsync receives an array of globs of TypeScript source files, and parses the functions in these files that have the export keyword. It returns an array of FunctionData objects with the following shape:

[
  {
    description: 'Adds two numbers.',
    name: 'add',
    parameters: [
      {
        description: 'First number to add.',
        name: 'x',
        optional: false,
        type: 'number'
      },
      {
        description: 'Second number to add.',
        name: 'y',
        optional: false,
        type: 'number'
      }
    ],
    returnType: { description: 'The sum of `x` and `y`.', type: 'number' },
    tags: null
  }
]

renderFunctionDataToMarkdown renders the given array of FunctionData objects to a Markdown string.

Now, let’s run the generate-ts-docs.ts script, piping its output to a file:

$ npx ts-node generate-ts-docs.ts > README.md

The output README.md will be as follows:

# add(x, y)

Adds two numbers.

## *Parameters*

- **`x`** (`number`) – First number to add.
- **`y`** (`number`) – Second number to add.

## *Return type*

The sum of `x` and `y`.

```
number
```

API

Types

export type FunctionData = {
  description: null | string
  name: string
  parameters: Array<ParameterData>
  returnType: null | ReturnTypeData
  tags: null | TagsData
}

export type ParameterData = {
  description: null | string
  name: string
  optional: boolean
  type: string | ObjectData
}

export type ObjectData = {
  keys: Array<ParameterData>
  type: 'object'
}

export type ReturnTypeData = {
  description: null | string
  type: string
}

export type TagsData = { [key: string]: null | string }

Functions

Functions data

parseExportedFunctionsAsync(globs [, options])

Parses the exported functions defined in the given globs of TypeScript files.

  • Functions with the @ignore JSDoc tag will be skipped.
  • Functions will be sorted in ascending order of their @weight JSDoc tag. A function with the @weight tag will be ranked before a function without the @weight tag.
Parameters
  • globs (Array<string>) – One or more globs of TypeScript files.
  • options (object) – Optional.
    • tsconfigFilePath (string) – Path to a TypeScript configuration file. Defaults to ./tsconfig.json.
Return type
Promise<Array<FunctionData>>
createCategories(functionsData)

Groups each object in functionsData by the value of each function’s tags.category key.

Parameters
  • functionsData (Array<FunctionData>)
Return type
Array<{
  name: string;
  functionsData: Array<FunctionData>;
}>

Markdown

renderCategoryToMarkdown(category [, options])
Parameters
  • category (object)
    • name (string)
    • functionsData (Array<FunctionData>)
  • options (object) – Optional.
    • headerLevel (number) – Header level to be used for rendering the category name. Defaults to 1 (ie. #).
Return type
string
renderFunctionDataToMarkdown(functionData [, options])
Parameters
  • functionData (FunctionData)
  • options (object) – Optional.
    • headerLevel (number) – Header level to be used for rendering the function name. Defaults to 1 (ie. #).
Return type
string

Markdown table of contents

renderCategoriesToMarkdownToc(categories)

Generate a Markdown table of contents for the given categories.

Parameters
  • categories (Array<{ name: string; functionsData: Array<FunctionData>; }>)
Return type
string
renderFunctionsDataToMarkdownToc(functionsData)

Generate a Markdown table of contents for the given functionsData.

Parameters
  • functionsData (Array<FunctionData>)
Return type
string

Installation

$ npm install --save-dev generate-ts-docs

Implementation details

The parseExportedFunctionsAsync function works via the following two-step process:

  1. Generate type declarations for the given TypeScript source files.
  2. Traverse and extract relevant information from the AST of the generated type declarations.

See also

License

MIT

generate-ts-docs's People

Contributors

yuanqing avatar

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.