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 |
|
3 | Generates table of contents for markdown files inside local git repository. Links are compatible with anchors generated
|
4 | by github or other sites via a command line flag.
|
5 |
|
6 |
|
7 |
|
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 |
|
26 |
|
27 |
|
28 | ## Installation
|
29 |
|
30 | npm install -g doctoc
|
31 |
|
32 | ## Usage
|
33 |
|
34 | In 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
|
36 | each markdown file found by recursively searching each folder. Below are some
|
37 | examples.
|
38 |
|
39 | ### Adding toc to all files in a directory and sub directories
|
40 |
|
41 | Go into the directory that contains you local git project and type:
|
42 |
|
43 | doctoc .
|
44 |
|
45 | This will update all markdown files in the current directory and all its
|
46 | subdirectories with a table of content that will point at the anchors generated
|
47 | by the markdown parser. Doctoc defaults to using the GitHub parser, but other
|
48 | [modes can be
|
49 | specified](#using-doctoc-to-generate-links-compatible-with-other-sites).
|
50 |
|
51 | ### Ignoring individual files
|
52 | In 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 |
|
56 | If 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 |
|
60 | If 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 |
|
72 | In order to add a table of contents whose links are compatible other sites add the appropriate mode flag:
|
73 |
|
74 | Available 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 |
|
90 | By 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 |
|
97 | You place this code directly in your .md file. For example:
|
98 |
|
99 | ```
|
100 | // my_new_post.md
|
101 | Here we are, introducing the post. It's going to be great!
|
102 | But first: a TOC for easy reference.
|
103 |
|
104 | <!-- START doctoc -->
|
105 | <!-- END doctoc -->
|
106 |
|
107 | # Section One
|
108 |
|
109 | Here we'll discuss...
|
110 |
|
111 | ```
|
112 |
|
113 | Running doctoc will insert the toc at that location.
|
114 |
|
115 | ### Specifying a custom TOC title
|
116 |
|
117 | Use 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 |
|
119 | Alternatively, 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 |
|
123 | Use the `--maxlevel` option to limit TOC entries to headings only up to the specified level; e.g., `doctoc --maxlevel 3 .`
|
124 |
|
125 | By 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 |
|
132 | You can print to stdout by using the `-s` or `--stdout` option.
|
133 |
|
134 | [ack]: http://beyondgrep.com/
|
135 |
|
136 | ### Only update existing ToC
|
137 |
|
138 | Use `--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 |
|
142 | doctoc can be used as a [pre-commit](http://pre-commit.com) hook by using the
|
143 | following configuration:
|
144 |
|
145 | ```yaml
|
146 | repos:
|
147 | - repo: https://github.com/thlorenz/doctoc
|
148 | rev: ... # substitute a tagged version
|
149 | hooks:
|
150 | - id: doctoc
|
151 | ```
|
152 |
|
153 | This will run `doctoc` against markdown files when committing to ensure the
|
154 | TOC stays up-to-date.
|
155 |
|
156 | ### Docker image
|
157 |
|
158 | There'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)
|