Simple and customizable way to generate TOC for Github Markdown files.(简单、可自定义的方式为 Github Markdown 文件自动生成目录。)
MIT License
Easy and customizable way to generate TOC for README.md on GitHub.
If you don't want to download any files or read the command instructions below, you can try my online build tool.
Pypi
$ pip3 install gfm-toc --upgrade
Manually
$ git clone https://github.com/waynerv/github-markdown-toc
$ cd github-markdown-toc
$ python3 setup.py install
You can also directly download the file gfm_toc/md_toc.py
in the repository and run this script manually like this:
$ python3 md-toc.py [-h] [-s {1,2,3,4,5,6}] [-e {1,2,3,4,5,6}] [-o] file [file ...]
The command syntax is as follows:
$ gfm-toc [-h] [-s {1,2,3,4,5,6}] [-e {1,2,3,4,5,6}] [-o] file [file ...]
Run the command as follows to get help information for the command options(you can replace -h
with --help
).:
$ gfm-toc -h
Note: Make sure your device has successfully installed Python3 before run it.
Automatically generate TOC for a single markdown file and print result to standard output with only 2-4 title levels:
$ gfm-toc -s 2 -e 4 -o README_eng.md
Generate from file: README_eng.md
- [Table of Contents](#table-of-contents)
- [md-toc](#md-toc)
- [Installation](#installation)
- [Usage](#usage)
- [Single file](#single-file)
- [Multiple files](#multiple-files)
- [Configuration](#configuration)
- [customize the title level](#customize--the-title-level)
- [Write results to a file or print to standard output](#write-results-to-a-file-or-print-to-standard-output)
- [Dependency](#dependency)
Table of contents generated.
Then copy/paste result between prompt sentence from console into original README.md.
Use the default configuration to generate TOC for multiple markdown files and write them to a file separately:
$ gfm-toc file01.md file02.md file03.md
Table of contents generated.
Here's an example:
After generating TOC and writing to the file:
Use the command line options -s
or --start
and add parameters to set the start header level of TOC. The default value of the parameter is 1.
Use the command line options -e
or --end
and add parameters to set the end header level of TOC. The default value of the parameter is 6.
The title level parameter must be an integer between 1 and 6, and the start header level cannot be greater than the end header level.
-s {1,2,3,4,5,6}, --start {1,2,3,4,5,6} choose the start level of TOC, default value is 1
-e {1,2,3,4,5,6}, --end {1,2,3,4,5,6} choose the end level of TOC, default value is 6
Generate TOC of 1-6 header levels (default option):
$ gfm-toc test/Mastering_Markdown.md -o
Generate from file: test/Mastering_Markdown.md
- [Mastering Markdown](#mastering-markdown)
- [What is Markdown?](#what-is-markdown)
- [Examples](#examples)
- [Syntax guide](#syntax-guide)
- [Headers](#headers)
- [Emphasis](#emphasis)
- [Lists](#lists)
- [Unordered](#unordered)
- [Ordered](#ordered)
- [Images](#images)
- [Links](#links)
- [Blockquotes](#blockquotes)
- [Inline code](#inline-code)
Table of contents generated.
Only generate 2-3 header levels:
$ gfm-toc examples/Mastering_Markdown.md -o -s 2 -e 3
Generate from file: examples/Mastering_Markdown.md
- [What is Markdown?](#what-is-markdown)
- [Examples](#examples)
- [Syntax guide](#syntax-guide)
- [Headers](#headers)
- [Emphasis](#emphasis)
- [Lists](#lists)
- [Images](#images)
- [Links](#links)
- [Blockquotes](#blockquotes)
- [Inline code](#inline-code)
Table of contents generated.
By default, the program automatically writes the generated TOC to the beginning of the original file.
Add the option -o
or --output
when you want to print the results to standard output for copying or other processing.
-o, --output print toc to stdout instead of writing to file
Use the >
on the command line to export the generated directory to a separate file:
$ gfm-toc -o README.md > table_of_content.md
This option is not very common, because in many cases, people write Markdown documents according to different specifications or their own habits. But if you need, you can add -t
or --title
options when executing commands. This will add a level 2 header called Table of contens
to the generated TOC, as follows:
$ gfm-toc examples/Mastering_Markdown.md -o -s 2 -e 3 -t
Generate from file: examples/Mastering_Markdown.md
## Table of contents
- [What is Markdown?](#what-is-markdown)
- [Examples](#examples)
- [Syntax guide](#syntax-guide)
- [Headers](#headers)
- [Emphasis](#emphasis)
- [Lists](#lists)
- [Images](#images)
- [Links](#links)
- [Blockquotes](#blockquotes)
- [Inline code](#inline-code)
Table of contents generated.
Tested on Ubuntu 18.04 in bash with Python 3.6.7.
MIT