UNPKG

5.61 kBMarkdownView Raw
1# DocToc [![Node.js CI](https://github.com/thlorenz/doctoc/actions/workflows/node.js.yml/badge.svg)](https://github.com/thlorenz/doctoc/actions/workflows/node.js.yml)
2
3Generates table of contents for markdown files inside local git repository. Links are compatible with anchors generated
4by github or other sites via a command line flag.
5
6<!-- START doctoc generated TOC please keep comment here to allow auto update -->
7<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
8**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
9
10- [Installation](#installation)
11- [Usage](#usage)
12 - [Adding toc to all files in a directory and sub directories](#adding-toc-to-all-files-in-a-directory-and-sub-directories)
13 - [Update existing doctoc TOCs effortlessly](#update-existing-doctoc-tocs-effortlessly)
14 - [Adding toc to individual files](#adding-toc-to-individual-files)
15 - [Examples](#examples)
16 - [Using doctoc to generate links compatible with other sites](#using-doctoc-to-generate-links-compatible-with-other-sites)
17 - [Example](#example)
18 - [Specifying location of toc](#specifying-location-of-toc)
19 - [Specifying a custom TOC title](#specifying-a-custom-toc-title)
20 - [Specifying a maximum heading level for TOC entries](#specifying-a-maximum-heading-level-for-toc-entries)
21 - [Printing to stdout](#printing-to-stdout)
22 - [Usage as a `git` hook](#usage-as-a-git-hook)
23 - [Docker image](#docker-image)
24
25<!-- END doctoc generated TOC please keep comment here to allow auto update -->
26
27
28## Installation
29
30 npm install -g doctoc
31
32## Usage
33
34In its simplest usage, you can pass one or more files or folders to the
35`doctoc` command. This will update the TOCs of each file specified as well as of
36each markdown file found by recursively searching each folder. Below are some
37examples.
38
39### Adding toc to all files in a directory and sub directories
40
41Go into the directory that contains you local git project and type:
42
43 doctoc .
44
45This will update all markdown files in the current directory and all its
46subdirectories with a table of content that will point at the anchors generated
47by the markdown parser. Doctoc defaults to using the GitHub parser, but other
48[modes can be
49specified](#using-doctoc-to-generate-links-compatible-with-other-sites).
50
51### Ignoring individual files
52In order to ignore a specific file when running `doctoc` on an entire directory, just add `<!-- DOCTOC SKIP -->` to the top of the file you wish to ignore.
53
54### Update existing doctoc TOCs effortlessly
55
56If you already have a TOC inserted by doctoc, it will automatically be updated by running the command (rather than inserting a duplicate toc). Doctoc locates the TOC by the `<!-- START doctoc -->` and `<!-- END doctoc -->` comments, so you can also move a generated TOC to any other portion of your document and it will be updated there.
57
58### Adding toc to individual files
59
60If you want to convert only specific files, do:
61
62 doctoc /path/to/file [...]
63
64#### Examples
65
66 doctoc README.md
67
68 doctoc CONTRIBUTING.md LICENSE.md
69
70### Using doctoc to generate links compatible with other sites
71
72In order to add a table of contents whose links are compatible other sites add the appropriate mode flag:
73
74Available modes are:
75
76```
77--bitbucket bitbucket.org
78--nodejs nodejs.org
79--github github.com
80--gitlab gitlab.com
81--ghost ghost.org
82```
83
84#### Example
85
86 doctoc README.md --bitbucket
87
88### Specifying location of toc
89
90By default, doctoc places the toc at the top of the file. You can indicate to have it placed elsewhere with the following format:
91
92```
93<!-- START doctoc -->
94<!-- END doctoc -->
95```
96
97You place this code directly in your .md file. For example:
98
99```
100// my_new_post.md
101Here we are, introducing the post. It's going to be great!
102But first: a TOC for easy reference.
103
104<!-- START doctoc -->
105<!-- END doctoc -->
106
107# Section One
108
109Here we'll discuss...
110
111```
112
113Running doctoc will insert the toc at that location.
114
115### Specifying a custom TOC title
116
117Use the `--title` option to specify a (Markdown-formatted) custom TOC title; e.g., `doctoc --title '**Contents**' .` From then on, you can simply run `doctoc <file>` and doctoc will will keep the title you specified.
118
119Alternatively, to blank out the title, use the `--notitle` option. This will simply remove the title from the TOC.
120
121### Specifying a maximum heading level for TOC entries
122
123Use the `--maxlevel` option to limit TOC entries to headings only up to the specified level; e.g., `doctoc --maxlevel 3 .`
124
125By default,
126
127- no limit is placed on Markdown-formatted headings,
128- whereas headings from embedded HTML are limited to 4 levels.
129
130### Printing to stdout
131
132You can print to stdout by using the `-s` or `--stdout` option.
133
134[ack]: http://beyondgrep.com/
135
136### Only update existing ToC
137
138Use `--update-only` or `-u` to only update the existing ToC. That is, the Markdown files without ToC will be left untouched. It is good if you want to use `doctoc` with `lint-staged`.
139
140### Usage as a `git` hook
141
142doctoc can be used as a [pre-commit](http://pre-commit.com) hook by using the
143following configuration:
144
145```yaml
146repos:
147- repo: https://github.com/thlorenz/doctoc
148 rev: ... # substitute a tagged version
149 hooks:
150 - id: doctoc
151```
152
153This will run `doctoc` against markdown files when committing to ensure the
154TOC stays up-to-date.
155
156### Docker image
157
158There's an unofficial Docker image project for doctoc, if you'd like to use doctoc via Docker or other container based CI/CD pipeline, you can take a look at [PeterDaveHello/docker-doctoc](https://github.com/PeterDaveHello/docker-doctoc)