Skip to main content

gfm-toc

Simple and customizable way to generate TOC for github markdown files.

中文文档 | README

Table of Contents

Overview

Easy and customizable way to generate TOC for README.md on GitHub.

  • Automatically generate table of contents in accordance with Github Favored Markdown format
  • Just a single .py file can be done with no other dependencies
  • Support for text content in both Chinese and English languages
  • Support for customizing the title level of the table of contents
  • Supports automatic write results to files, and also can print results to standard output for further processing
  • Supports generating table of contents for multiple files at the same time
  • Support for repeating headers, filtering headers in code blocks

If you don't want to download any files or read the command instructions below, you can try my online build tool.

Installation

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 ...]

Usage

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.

Single file

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.

Multiple files

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:

Configuration

customize the title level

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 title level cannot be greater than the end title 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 title 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 title 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.

Write results to a file or print to standard output

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

Add a title to the generated TOC

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 title 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.

Dependency

  • Python3

Tested on Ubuntu 18.04 in bash with Python 3.6.7.

License

MIT

Release files for gfm-toc 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gfm-toc 0.0.4
File Size Uploaded
gfm-toc-0.0.4.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gfm-toc 0.0.4
File Interpreter ABI Platform
gfm_toc-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / gfm-toc-0.0.4.tar.gz

Download URL gfm-toc-0.0.4.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d9dbc75fb58a51eccfa983afd56f11ba68ff8a7b1e53c58d41501ddb3e92ff7c
BLAKE2b-256 checksum
How to use checksums
29d3fdebd04673663e5a750500c4b92d07af251345e22eafe4d71e5da2c7b667
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release files / gfm_toc-0.0.4-py3-none-any.whl

Download URL gfm_toc-0.0.4-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d1f9f93cc4f391ccc830e747c5c182502ead154417d914d2b4b51eb7d77d2df
BLAKE2b-256 checksum
How to use checksums
90fbe310576c0254a1262facd1dd6b74154b28575d80117601133fb54ebccaee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release history Release notifications | RSS feed

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page