Skip to content
4r7d3c0 edited this page Mar 19, 2020 · 7 revisions

The programmatic use of Documentary is intended for developers who want to use this software in their projects.

Toc Stream

Toc is a transform stream which can generate a table of contents for incoming markdown data. For every title that the transform sees, it will push the appropriate level of the table of contents.

TocConfig Type

When creating a new Toc instance, it will accept the following configuration object.

Property Type Description Example
skipLevelOne boolean Start the table of contents from level 2, i.e., excluding the # title. For example, the following code:
# Hello World

## Table Of Contents

## Introduction

will be compiled to

- [Table Of Contents](#table-of-contents)
- [Introduction](#introduction)

when skipLevelOne is not set (by default), and to

- [Hello World](#hello-world)
  * [Table Of Contents](#table-of-contents)
  * [Introduction](#introduction)

when skipLevelOne is set to false.

constructor(
  config?: {
    skipLevelOne?: boolean = true,
  },
): Toc

Create a new instance of a Toc stream.

/* yarn example/toc.js */
import { Toc } from 'documentary'
import Catchment from 'catchment'
import { createReadStream } from 'fs'

(async () => {
  try {
    const md = createReadStream('example/markdown.md')
    const rs = new Toc()
    md.pipe(rs)

    const { promise } = new Catchment({ rs })
    const res = await promise
    console.log(res)
  } catch ({ stack }) {
    console.log(stack)
  }
})()
- [Table Of Contents](#table-of-contents)
- [CLI](#cli)
  * [`-j`, `--jsdoc`: Add JSDoc](#-j---jsdoc-add-jsdoc)
- [API](#api)
- [Copyright](#copyright)
Clone this wiki locally