npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-tsdoc

v2.0.6

Published

A simple cli to generate markdown documentations from *.d.ts files.

Downloads

37

Readme

Simple-tsdoc

API | Cli usage | API usage | Change log

Introduction

A simple tool to generate markdown documentations from *.d.ts files, support api and cli usage.

Features

  • Support custom markdown style.
  • Support custom output files.
  • Easy to use: It takes only 5 lines of code to generate the markdown file.

Cli usage

install: npm i simple-tsdoc -g

Your project folder should have tsconfig.json and package.json file, if you want use some custom tag,the tsdoc.json file is required.

Usage:
  $ simple-tsdoc [...input]

Commands:
  [...input]  Specify The d.ts file entries.

For more info, run any command with the `--help` flag:
  $ simple-tsdoc --help

Options:
  -v, --version          Display version number
  -o, --output [output]  Specify The output path. (default: out.md)
  -s, --silent           Silent mode. (default: false)
  -m, --multiple         Emit a markdown file for per API. (default: false)
  -b, --banner [banner]  Add banner for output markdown file. (default: )
  -f, --footer [footer]  Add footer for output markdown file. (default: )
  -h, --help             Display this message

Examples:
simple-tsdoc ./dist/index.d.ts -o ./docs/api.md -b "# simple-tsdoc"
simple-tsdoc ./dist/index.d.ts -s -m -o ./docs

API usage

Support esm or commonjs.

You can see examples folder for more info.

Basic

import { ApiItemKind } from "@microsoft/api-extractor-model";
import { resolve } from "path";
import { tsdoc, IRenderingContext } from "../../";

tsdoc({
  input: [resolve(__dirname, "index.d.ts")],
  output: resolve(__dirname, "out", "index.md"),
  banner: "# simple-tsdoc",
  RenderingContextConstructor: IRenderingContext,
  silent: true,
  excludeKinds: [ApiItemKind.Interface, ApiItemKind.TypeAlias],
}).catch((e) => {
  console.error(e);
});

Custom output

import { ensureDir } from "fs-extra";
import { writeFile } from "fs/promises";
import { dirname, relative, resolve } from "path";
import { getMarkdownInfoMap } from "../../";

async function main() {
  const entry = resolve(__dirname, "src", "index.d.ts");
  const outDir = resolve(__dirname, "out");
  const info = await getMarkdownInfoMap({ entry });

  const tasks = Array.from(info.entries()).map(
    async ([name, { md, apiDocItem }]) => {
      if (!apiDocItem.apiItem.fileUrlPath) {
        return;
      }

      // let the output files structure be like source files structure.
      const absolutePath = resolve(apiDocItem.apiItem.fileUrlPath);
      const rootDir = resolve(__dirname, "src");
      const relateRootDir = relative(rootDir, absolutePath);

      const outFile = resolve(outDir, relateRootDir);

      await ensureDir(dirname(outFile));
      await writeFile(resolve(dirname(outFile), `${name}.md`), md);
    }
  );

  await Promise.all(tasks);
}

main().catch((e) => {
  console.error(e);
});

Custom style

import { resolve } from "path";
import {
  IRenderingContext,
  StandardTagName,
  Annotation,
  emit,
  getMarkdownInfoMap,
} from "../../";

class CustomRenderingContext extends IRenderingContext {
  protected appendTag(
    tagName: StandardTagName,
    value: string | undefined
  ): void {
    if (tagName === "@see") {
      this.append(`👁️[${value?.trim()}](${value?.trim()})`);
      return;
    }

    this.append(`**${tagName}** ${value ?? ""}`);
  }

  protected appendApiName(name: string): void {
    if (this.titleLevel > 2) {
      // means this is a property api
      this.append(`${"#".repeat(this.titleLevel)} ${name}`);
      return;
    }

    this.append(`${"#".repeat(this.titleLevel)} ${name}`);
    this.append("---------------	");
  }

  protected appendParams(params: Annotation["params"]): void {
    if (params.length === 0) {
      return;
    }

    this.append("| param | description | isOptional | type |", 1);
    this.append("| ----- | ----------- | ---------- | ---- |", 1);

    params.forEach(({ name, description, isOptional, type }) => {
      this.append(`| ${name} | ${description} | ${isOptional} | ${type} |`, 1);
    });

    this.appendLf(1);
  }
}

getMarkdownInfoMap({
  entry: resolve(__dirname, "index.d.ts"),
  RenderingContextConstructor: CustomRenderingContext,
})
  .then((apiInfoMap) =>
    emit({
      multiple: true,
      apiInfoMap,
      output: resolve(__dirname, "out"),
    })
  )
  .catch((e) => {
    console.error(e);
  });

Custom tsdoc tag

The tsdoc.json file in your project folder is required, and then you should add the custom tag in tsdoc.json file.

tsdoc.json

{
  "$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",

  "extends": ["@microsoft/api-extractor/extends/tsdoc-base.json"],

  "tagDefinitions": [
    {
      "tagName": "@author",
      "syntaxKind": "block",
      "allowMultiple": true
    }
  ],

  "supportForTags": {
    "@author": true
  }
}

build.ts

import { resolve } from "path";
import { tsdoc, IRenderingContext } from "../../";

tsdoc({
  input: [resolve(__dirname, "index.d.ts")],
  output: resolve(__dirname, "out", "index.md"),
  banner: "# simple-tsdoc",
  RenderingContextConstructor: IRenderingContext,
  silent: true,
}).catch((e) => {
  console.error(e);
});