md-toc


Namemd-toc JSON
Version 9.0.0 PyPI version JSON
download
home_pagehttps://blog.franco.net.eu.org/software/#md-toc
SummaryAutomatically generate and add an accurate table of contents to markdown files
upload_time2024-04-10 22:12:39
maintainerNone
docs_urlNone
authorFranco Masotti
requires_python>=3.8
licenseGPLv3+,
keywords markdown toc text table-of-contents documentation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Markdown Table Of Contents

<img src="assets/md-toc_logo.png" alt="md-toc logo" width="160"/>

[![PyPI md-toc version](https://img.shields.io/pypi/v/md-toc.svg)](https://pypi.org/project/md-toc/)
[![Debian 12 package](https://repology.org/badge/version-for-repo/debian_12/md-toc.svg)](https://repology.org/project/md-toc/versions)
[![nixpkgs unstable package](https://repology.org/badge/version-for-repo/nix_unstable/python:md-toc.svg)](https://repology.org/project/python:md-toc/versions)
[![Anaconda.org](https://anaconda.org/conda-forge/md-toc/badges/version.svg)](https://anaconda.org/conda-forge/md-toc)
[![Downloads](https://pepy.tech/badge/md-toc)](https://pepy.tech/project/md-toc)
[![Dependent repos (via libraries.io)](https://img.shields.io/librariesio/dependent-repos/pypi/md-toc.svg)](https://libraries.io/pypi/md-toc/dependents)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
[![Buy me a coffee](assets/buy_me_a_coffee.svg)](https://buymeacoff.ee/frnmst)

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

<!--TOC-->

- [Markdown Table Of Contents](#markdown-table-of-contents)
  - [Video](#video)
  - [Description](#description)
  - [Features](#features)
  - [Examples](#examples)
  - [API examples](#api-examples)
  - [Documentation](#documentation)
  - [CLI Helps](#cli-helps)
  - [Extras](#extras)
    - [HTML output](#html-output)
  - [License](#license)
  - [Changelog and trusted source](#changelog-and-trusted-source)
  - [Support this project](#support-this-project)

<!--TOC-->

## Video

[![image](./assets/md-toc_youtube_video_thumbnail.png)](https://www.youtube.com/watch?v=guyVdPNmC0A&t=49s)

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

- in March 2021 GitHub added an
  [interactive TOC button](https://github.blog/changelog/2021-04-13-table-of-contents-support-in-markdown-files/)
  at the top-left of readme files. This system works for markdown and others
- GitLab added an [extension](https://docs.gitlab.com/ee/user/markdown.html#table-of-contents)
  called `Table of contents` to its Gitlab Flavored Mardown

## Features

- works offline
- edits file in place using a TOC marker (default `<!--TOC-->`) or
  output to standard output
- maximum heading level selection (1 to 6)
- list indentation based on heading, which can optionally be disabled
- outputs an ordered or unordered TOC list with list marker selection
- creates anchor links to markdown headings by default or a plain list
  as alternative
- checks if heading level is coherent: this avoid creating an
  erroneous TOC. This feature can be disabled if needed
- skip any number lines before generating the TOC
- can read content from standard input
- handles multiple files at once
- selection of newline string
- check if there is difference between existing TOC in file and newly generated
  one
- supports GitHub, GitLab, Commonmark, Redcarpet and others
- [pre-commit](https://pre-commit.com/) md-toc
  [hook](https://docs.franco.net.eu.org/md-toc/pre_commit_hook.html)

And more! See the
[feature comparison table](https://docs.franco.net.eu.org/md-toc/features.html)

## 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](https://blog.franco.net.eu.org/)
along with its
[pre-commit hook](https://software.franco.net.eu.org/frnmst/blog/src/branch/master/.pre-commit-config.yaml).
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:

```shell
$ 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](https://docs.franco.net.eu.org/md-toc/api.html).
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:

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

```python
>>> 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](https://docs.franco.net.eu.org/md-toc/markdown_specification.html)
section of the documentation to learn how this program parsers markdown
files and builds a correct output.

## CLI Helps

```shell
$ 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](https://pandoc.org/) you can
generate an HTML output starting from a markdown file. You can do something
like:

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

## License

Copyright (C) 2017-2024 [Franco Masotti](https://blog.franco.net.eu.org/about/#contacts)

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](https://blog.franco.net.eu.org/software/#md-toc).

## Support this project

- [Buy Me a Coffee](https://www.buymeacoffee.com/frnmst)
- [Liberapay](https://liberapay.com/frnmst)
- Bitcoin: `bc1qnkflazapw3hjupawj0lm39dh9xt88s7zal5mwu`
- Monero: `84KHWDTd9hbPyGwikk33Qp5GW7o7zRwPb8kJ6u93zs4sNMpDSnM5ZTWVnUp2cudRYNT6rNqctnMQ9NbUewbj7MzCBUcrQEY`
- Dogecoin: `DMB5h2GhHiTNW7EcmDnqkYpKs6Da2wK3zP`
- Vertcoin: `vtc1qd8n3jvkd2vwrr6cpejkd9wavp4ld6xfu9hkhh0`

            

Raw data

            {
    "_id": null,
    "home_page": "https://blog.franco.net.eu.org/software/#md-toc",
    "name": "md-toc",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "markdown, toc, text, table-of-contents, documentation",
    "author": "Franco Masotti",
    "author_email": "franco.masotti@tutanota.com",
    "download_url": "https://files.pythonhosted.org/packages/b5/45/3ad7b820f46daa3c0b95bb972d8962a136e452d19eea07125df958cf9055/md_toc-9.0.0.tar.gz",
    "platform": null,
    "description": "# Markdown Table Of Contents\n\n<img src=\"assets/md-toc_logo.png\" alt=\"md-toc logo\" width=\"160\"/>\n\n[![PyPI md-toc version](https://img.shields.io/pypi/v/md-toc.svg)](https://pypi.org/project/md-toc/)\n[![Debian 12 package](https://repology.org/badge/version-for-repo/debian_12/md-toc.svg)](https://repology.org/project/md-toc/versions)\n[![nixpkgs unstable package](https://repology.org/badge/version-for-repo/nix_unstable/python:md-toc.svg)](https://repology.org/project/python:md-toc/versions)\n[![Anaconda.org](https://anaconda.org/conda-forge/md-toc/badges/version.svg)](https://anaconda.org/conda-forge/md-toc)\n[![Downloads](https://pepy.tech/badge/md-toc)](https://pepy.tech/project/md-toc)\n[![Dependent repos (via libraries.io)](https://img.shields.io/librariesio/dependent-repos/pypi/md-toc.svg)](https://libraries.io/pypi/md-toc/dependents)\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)\n[![Buy me a coffee](assets/buy_me_a_coffee.svg)](https://buymeacoff.ee/frnmst)\n\nAutomatically generate and add an accurate table of contents to markdown files.\n\n<!--TOC-->\n\n- [Markdown Table Of Contents](#markdown-table-of-contents)\n  - [Video](#video)\n  - [Description](#description)\n  - [Features](#features)\n  - [Examples](#examples)\n  - [API examples](#api-examples)\n  - [Documentation](#documentation)\n  - [CLI Helps](#cli-helps)\n  - [Extras](#extras)\n    - [HTML output](#html-output)\n  - [License](#license)\n  - [Changelog and trusted source](#changelog-and-trusted-source)\n  - [Support this project](#support-this-project)\n\n<!--TOC-->\n\n## Video\n\n[![image](./assets/md-toc_youtube_video_thumbnail.png)](https://www.youtube.com/watch?v=guyVdPNmC0A&t=49s)\n\n## Description\n\nThe table of contents (a.k.a: TOC) generated by this program is designed\nto work with several markdown parsers such as the ones used by GitHub\nand GitLab.\n\nRules for generating the TOC are determined by the selected markdown\nparser. md-toc aimes infact to be as conformant as possible in respect\nto each one of them. This was possible by studying the available\ndocumentations and by reverse engineering the source codes.\n\nGitHub and GitLab have introduced their version of the markdown TOC\nafter md-toc and similar tools were created:\n\n- in March 2021 GitHub added an\n  [interactive TOC button](https://github.blog/changelog/2021-04-13-table-of-contents-support-in-markdown-files/)\n  at the top-left of readme files. This system works for markdown and others\n- GitLab added an [extension](https://docs.gitlab.com/ee/user/markdown.html#table-of-contents)\n  called `Table of contents` to its Gitlab Flavored Mardown\n\n## Features\n\n- works offline\n- edits file in place using a TOC marker (default `<!--TOC-->`) or\n  output to standard output\n- maximum heading level selection (1 to 6)\n- list indentation based on heading, which can optionally be disabled\n- outputs an ordered or unordered TOC list with list marker selection\n- creates anchor links to markdown headings by default or a plain list\n  as alternative\n- checks if heading level is coherent: this avoid creating an\n  erroneous TOC. This feature can be disabled if needed\n- skip any number lines before generating the TOC\n- can read content from standard input\n- handles multiple files at once\n- selection of newline string\n- check if there is difference between existing TOC in file and newly generated\n  one\n- supports GitHub, GitLab, Commonmark, Redcarpet and others\n- [pre-commit](https://pre-commit.com/) md-toc\n  [hook](https://docs.franco.net.eu.org/md-toc/pre_commit_hook.html)\n\nAnd more! See the\n[feature comparison table](https://docs.franco.net.eu.org/md-toc/features.html)\n\n## Examples\n\nYou can use md-toc in your blog, documentation based on markdown, GitHub pages,\nmarkdown files in Nextcloud, etc...\n\nI use it in [my Jekyll-based blog](https://blog.franco.net.eu.org/)\nalong with its\n[pre-commit hook](https://software.franco.net.eu.org/frnmst/blog/src/branch/master/.pre-commit-config.yaml).\nI also use it in most repositories where `README.md` files are present.\n\nMost markdown renderers do not provide a way to automatically generate a\nTOC so md-toc is useful for this purpose.\n\nA very common use case is this:\n\n```shell\n$ cat foo.md\n\n# Table of contents\n\n<!--TOC-->\n\n# this\n## is\n## a\n### foo\n#### booo\n### foo\n## file\n\n## bye\n\n# bye\n\n$ md_toc --in-place github --header-levels 6 foo.md        # or: md_toc -p github foo.md\n$ cat foo.md\n\n# Table of contents\n\n<!--TOC-->\n\n- [Table of contents](#table-of-contents)\n- [this](#this)\n  - [is](#is)\n  - [a](#a)\n    - [foo](#foo)\n      - [booo](#booo)\n    - [foo](#foo-1)\n  - [file](#file)\n  - [bye](#bye)\n- [bye](#bye-1)\n\n<!--TOC-->\n\n# this\n## is\n## a\n### foo\n#### booo\n### foo\n## file\n\n## bye\n\n# bye\n```\n\n## API examples\n\nmd-toc has a\n[public API](https://docs.franco.net.eu.org/md-toc/api.html).\nThis means for example that you can you easily build a TOC within another Python\nprogram. The easiest way to build one for a markdown file is:\n\n```python\n>>> import md_toc\n>>> f = open('foo.md')\n>>> print(f.read(), end='')\n# this\n## is\n## a\n### foo\n#### booo\n### foo\n## file\n\n## bye\n\n# bye\n>>> print(md_toc.build_toc('foo.md'), end='')\n- [this](#this)\n  - [is](#is)\n  - [a](#a)\n    - [foo](#foo)\n      - [boo](#boo)\n    - [foo](#foo-1)\n  - [file](#file)\n  - [bye](#bye)\n- [bye](#bye-1)\n```\n\nYou can also write the TOC in place:\n\n```python\n>>> import md_toc\n>>> f = open('foo.md')\n>>> print(f.read(), end='')\n# Table of contents\n\n<!--TOC-->\n\n# this\n## is\n## a\n### foo\n#### booo\n### foo\n## file\n\nTest\n\n## bye\n\n# bye\n>>> toc = md_toc.build_toc('foo.md')\n>>> md_toc.write_string_on_file_between_markers('foo.md', toc, '<!--TOC-->')\n>>> f = open('foo.md')\n>>> print(f.read(), end='')\n# Table of contents\n\n<!--TOC-->\n\n- [Table of contents](#table-of-contents)\n- [this](#this)\n  - [is](#is)\n  - [a](#a)\n    - [foo](#foo)\n      - [boo](#boo)\n    - [foo](#foo-1)\n  - [file](#file)\n  - [bye](#bye)\n- [bye](#bye-1)\n\n<!--TOC-->\n\n# this\n## is\n## a\n### foo\n#### booo\n### foo\n## file\n\nTest\n\n## bye\n\n# bye\n```\n\n## Documentation\n\n<https://docs.franco.net.eu.org/md-toc/>\n\nPlease read the\n[Markdown specification](https://docs.franco.net.eu.org/md-toc/markdown_specification.html)\nsection of the documentation to learn how this program parsers markdown\nfiles and builds a correct output.\n\n## CLI Helps\n\n```shell\n$ md_toc --help\n$ md_toc cmark --help\n$ md_toc commonmarker --help\n$ md_toc github --help\n$ md_toc gitlab --help\n$ md_toc goldmark --help\n$ md_toc redcarpet --help\n```\n\n## Extras\n\n### HTML output\n\nIf you use [Pandoc](https://pandoc.org/) you can\ngenerate an HTML output starting from a markdown file. You can do something\nlike:\n\n```shell\npandoc --from=commonmark --to=html -o a.html README.md\n```\n\n## License\n\nCopyright (C) 2017-2024 [Franco Masotti](https://blog.franco.net.eu.org/about/#contacts)\n\nmd-toc is free software: you can redistribute it and/or modify it under\nthe terms of the GNU General Public License as published by the Free\nSoftware Foundation, either version 3 of the License, or (at your\noption) any later version.\n\nmd-toc is distributed in the hope that it will be useful, but WITHOUT\nANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\nFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\nmore details.\n\nYou should have received a copy of the GNU General Public License along\nwith md-toc. If not, see <http://www.gnu.org/licenses/>.\n\n## Changelog and trusted source\n\nYou can check the authenticity of new releases using my public key.\n\nChangelogs, instructions, sources and keys can be found at\n[blog.franco.net.eu.org/software/#md-toc](https://blog.franco.net.eu.org/software/#md-toc).\n\n## Support this project\n\n- [Buy Me a Coffee](https://www.buymeacoffee.com/frnmst)\n- [Liberapay](https://liberapay.com/frnmst)\n- Bitcoin: `bc1qnkflazapw3hjupawj0lm39dh9xt88s7zal5mwu`\n- Monero: `84KHWDTd9hbPyGwikk33Qp5GW7o7zRwPb8kJ6u93zs4sNMpDSnM5ZTWVnUp2cudRYNT6rNqctnMQ9NbUewbj7MzCBUcrQEY`\n- Dogecoin: `DMB5h2GhHiTNW7EcmDnqkYpKs6Da2wK3zP`\n- Vertcoin: `vtc1qd8n3jvkd2vwrr6cpejkd9wavp4ld6xfu9hkhh0`\n",
    "bugtrack_url": null,
    "license": "GPLv3+,",
    "summary": "Automatically generate and add an accurate table of contents to markdown files",
    "version": "9.0.0",
    "project_urls": {
        "API Reference": "https://docs.franco.net.eu.org/md-toc/api.html",
        "Bug Tracker": "https://github.com/frnmst/md-toc/issues",
        "Changelog": "https://blog.franco.net.eu.org/software/CHANGELOG-md-toc.html",
        "Documentation": "https://docs.franco.net.eu.org/md-toc/",
        "Funding": "https://github.com/frnmst/md-toc#support-this-project",
        "Homepage": "https://blog.franco.net.eu.org/software/#md-toc",
        "Source Code": "https://github.com/frnmst/md-toc"
    },
    "split_keywords": [
        "markdown",
        " toc",
        " text",
        " table-of-contents",
        " documentation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c10d4b3202d4236da63d9b6b894ab7ecdbc6f6dad6e87a4970d7a22d2b315b97",
                "md5": "f954aa00468b9c2ecff43f6dec9d8543",
                "sha256": "dfd57de2faf252be1d6faf9bed7eab506e1caa7c4486ab6d6d04556426d5a7a5"
            },
            "downloads": -1,
            "filename": "md_toc-9.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f954aa00468b9c2ecff43f6dec9d8543",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 78101,
            "upload_time": "2024-04-10T22:12:36",
            "upload_time_iso_8601": "2024-04-10T22:12:36.754257Z",
            "url": "https://files.pythonhosted.org/packages/c1/0d/4b3202d4236da63d9b6b894ab7ecdbc6f6dad6e87a4970d7a22d2b315b97/md_toc-9.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b5453ad7b820f46daa3c0b95bb972d8962a136e452d19eea07125df958cf9055",
                "md5": "412fdf40b00f489792f19c2dd9a09bb7",
                "sha256": "a4e73b59f71c20b94c8c16bc6ef3bc2e80d1d40c398050101f80c3567fda7271"
            },
            "downloads": -1,
            "filename": "md_toc-9.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "412fdf40b00f489792f19c2dd9a09bb7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 65352,
            "upload_time": "2024-04-10T22:12:39",
            "upload_time_iso_8601": "2024-04-10T22:12:39.052414Z",
            "url": "https://files.pythonhosted.org/packages/b5/45/3ad7b820f46daa3c0b95bb972d8962a136e452d19eea07125df958cf9055/md_toc-9.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-10 22:12:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "frnmst",
    "github_project": "md-toc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "md-toc"
}
        
Elapsed time: 0.23188s