Skip to main content

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

Project description

# gfm-toc

[中文文档](<https://github.com/waynerv/github-markdown-toc/blob/master/README_CHS.md>) | [Readme](<https://github.com/waynerv/github-markdown-toc/blob/master/README.md>)

## Table of Contents

- [Overview](#overview)
- [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)
- [Add a title to the generated TOC](#add-a-title-to-the-generated-toc)
- [Dependency](#dependency)

## 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**

```bash
$ pip3 install gfm-toc --upgrade
```

**Manually**

```bash
$ 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:

```bash
$ 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:

```bash
$ 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`).:

```bash
$ 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:

```bash
$ 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:

```bash
$ gfm-toc file01.md file02.md file03.md
Table of contents generated.
```

here's an example:

- [File_without_TOC.md](https://github.com/waynerv/github-markdown-toc/blob/master/examples/File_without_TOC.md)

After generating **TOC** and writing to the file:

- [File_with_TOC.md](https://github.com/waynerv/github-markdown-toc/blob/master/examples/File_with_TOC.md)

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

```bash
-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):

```bash
$ 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:

```bash
$ 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.

```bash
-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:

```bash
$ 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:

```bash
$ 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.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gfm-toc-0.0.2.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

gfm_toc-0.0.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file gfm-toc-0.0.2.tar.gz.

File metadata

  • Download URL: gfm-toc-0.0.2.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for gfm-toc-0.0.2.tar.gz
Algorithm Hash digest
SHA256 009330a1c6b246a528efdb1af66b5d9d0e334207059911791b9c7e61101ee566
MD5 83a5b7d25f5cf754995df9935d0709a2
BLAKE2b-256 690d1458a53ce37b3f8cc2eb67f5c0b9bd44f60a4339922622500c4e54eb2738

See more details on using hashes here.

File details

Details for the file gfm_toc-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: gfm_toc-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for gfm_toc-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 28d197333b47036558816806b8a866138e617c01d6b07c12299fd6171b09c4b2
MD5 710d58b757aa2efbe6fa19e11530ae64
BLAKE2b-256 c90bffc8b9d3bdb13cc6451409cfc517b1eb251e7859d2a29af03c534a317643

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page