frnmst / md-toc

Automatically generate and add an accurate table of contents to markdown files.
https://docs.franco.net.eu.org/md-toc/
GNU General Public License v3.0
30 stars 5 forks source link
cmark cmark-gfm commonmark github gitlab inplace markdown python python3 redcarpet table-of-contents toc

Markdown Table Of Contents

md-toc logo

PyPI md-toc version Debian 12 package nixpkgs unstable package Anaconda.org Downloads Dependent repos (via libraries.io) pre-commit Buy me a coffee

Automatically generate and add an accurate table of contents to markdown files.

Video

image

Description

The table of contents (a.k.a: TOC) generated by this program is designed to work with several markdown parsers such as the ones used by GitHub and GitLab.

Rules for generating the TOC are determined by the selected markdown parser. md-toc aimes infact to be as conformant as possible in respect to each one of them. This was possible by studying the available documentations and by reverse engineering the source codes.

GitHub and GitLab have introduced their version of the markdown TOC after md-toc and similar tools were created:

Features

And more! See the feature comparison table

Examples

You can use md-toc in your blog, documentation based on markdown, GitHub pages, markdown files in Nextcloud, etc...

I use it in my Jekyll-based blog along with its pre-commit hook. I also use it in most repositories where README.md files are present.

Most markdown renderers do not provide a way to automatically generate a TOC so md-toc is useful for this purpose.

A very common use case is this:

$ cat foo.md

# Table of contents

<!--TOC-->

# this
## is
## a
### foo
#### booo
### foo
## file

## bye

# bye

$ md_toc --in-place github --header-levels 6 foo.md        # or: md_toc -p github foo.md
$ cat foo.md

# Table of contents

<!--TOC-->

- [Table of contents](#table-of-contents)
- [this](#this)
  - [is](#is)
  - [a](#a)
    - [foo](#foo)
      - [booo](#booo)
    - [foo](#foo-1)
  - [file](#file)
  - [bye](#bye)
- [bye](#bye-1)

<!--TOC-->

# this
## is
## a
### foo
#### booo
### foo
## file

## bye

# bye

API examples

md-toc has a public API. This means for example that you can you easily build a TOC within another Python program. The easiest way to build one for a markdown file is:

>>> import md_toc
>>> f = open('foo.md')
>>> print(f.read(), end='')
# this
## is
## a
### foo
#### booo
### foo
## file

## bye

# bye
>>> print(md_toc.build_toc('foo.md'), end='')
- [this](#this)
  - [is](#is)
  - [a](#a)
    - [foo](#foo)
      - [boo](#boo)
    - [foo](#foo-1)
  - [file](#file)
  - [bye](#bye)
- [bye](#bye-1)

You can also write the TOC in place:

>>> import md_toc
>>> f = open('foo.md')
>>> print(f.read(), end='')
# Table of contents

<!--TOC-->

# this
## is
## a
### foo
#### booo
### foo
## file

Test

## bye

# bye
>>> toc = md_toc.build_toc('foo.md')
>>> md_toc.write_string_on_file_between_markers('foo.md', toc, '<!--TOC-->')
>>> f = open('foo.md')
>>> print(f.read(), end='')
# Table of contents

<!--TOC-->

- [Table of contents](#table-of-contents)
- [this](#this)
  - [is](#is)
  - [a](#a)
    - [foo](#foo)
      - [boo](#boo)
    - [foo](#foo-1)
  - [file](#file)
  - [bye](#bye)
- [bye](#bye-1)

<!--TOC-->

# this
## is
## a
### foo
#### booo
### foo
## file

Test

## bye

# bye

Documentation

https://docs.franco.net.eu.org/md-toc/

Please read the Markdown specification section of the documentation to learn how this program parsers markdown files and builds a correct output.

CLI Helps

$ md_toc --help
$ md_toc cmark --help
$ md_toc commonmarker --help
$ md_toc github --help
$ md_toc gitlab --help
$ md_toc goldmark --help
$ md_toc redcarpet --help

Extras

HTML output

If you use Pandoc you can generate an HTML output starting from a markdown file. You can do something like:

pandoc --from=commonmark --to=html -o a.html README.md

License

Copyright (C) 2017-2024 Franco Masotti

md-toc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

md-toc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with md-toc. If not, see http://www.gnu.org/licenses/.

Changelog and trusted source

You can check the authenticity of new releases using my public key.

Changelogs, instructions, sources and keys can be found at blog.franco.net.eu.org/software/#md-toc.

Support this project