{"id":21499191,"url":"https://github.com/frnmst/md-toc","last_synced_at":"2025-07-15T21:31:13.917Z","repository":{"id":45891988,"uuid":"116044907","full_name":"frnmst/md-toc","owner":"frnmst","description":"Automatically generate and add an accurate table of contents to markdown files.","archived":false,"fork":false,"pushed_at":"2024-04-16T18:00:01.000Z","size":1502,"stargazers_count":30,"open_issues_count":6,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-29T21:24:25.149Z","etag":null,"topics":["cmark","cmark-gfm","commonmark","github","gitlab","inplace","markdown","python","python3","redcarpet","table-of-contents","toc"],"latest_commit_sha":null,"homepage":"https://docs.franco.net.eu.org/md-toc/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frnmst.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"tidelift":"pypi/md-toc","liberapay":"frnmst","custom":["https://www.buymeacoffee.com/frnmst"]}},"created_at":"2018-01-02T18:30:38.000Z","updated_at":"2024-09-03T01:13:21.000Z","dependencies_parsed_at":"2024-03-10T20:30:11.706Z","dependency_job_id":"f9985b67-aa8d-4e30-80c3-649c7a02fa16","html_url":"https://github.com/frnmst/md-toc","commit_stats":{"total_commits":497,"total_committers":9,"mean_commits":55.22222222222222,"dds":0.295774647887324,"last_synced_commit":"a7561f324fdfc11be83c5c92902edf93c8bc3a94"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frnmst%2Fmd-toc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frnmst%2Fmd-toc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frnmst%2Fmd-toc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frnmst%2Fmd-toc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frnmst","download_url":"https://codeload.github.com/frnmst/md-toc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226071047,"owners_count":17569103,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmark","cmark-gfm","commonmark","github","gitlab","inplace","markdown","python","python3","redcarpet","table-of-contents","toc"],"created_at":"2024-11-23T17:10:05.736Z","updated_at":"2024-11-23T17:10:06.422Z","avatar_url":"https://github.com/frnmst.png","language":"Python","readme":"# Markdown Table Of Contents\n\n\u003cimg src=\"assets/md-toc_logo.png\" alt=\"md-toc logo\" width=\"160\"/\u003e\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\u0026logoColor=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\u003c!--TOC--\u003e\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\u003c!--TOC--\u003e\n\n## Video\n\n[![image](./assets/md-toc_youtube_video_thumbnail.png)](https://www.youtube.com/watch?v=guyVdPNmC0A\u0026t=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 `\u003c!--TOC--\u003e`) 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\u003c!--TOC--\u003e\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\u003c!--TOC--\u003e\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\u003c!--TOC--\u003e\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\u003e\u003e\u003e import md_toc\n\u003e\u003e\u003e f = open('foo.md')\n\u003e\u003e\u003e 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\u003e\u003e\u003e 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\u003e\u003e\u003e import md_toc\n\u003e\u003e\u003e f = open('foo.md')\n\u003e\u003e\u003e print(f.read(), end='')\n# Table of contents\n\n\u003c!--TOC--\u003e\n\n# this\n## is\n## a\n### foo\n#### booo\n### foo\n## file\n\nTest\n\n## bye\n\n# bye\n\u003e\u003e\u003e toc = md_toc.build_toc('foo.md')\n\u003e\u003e\u003e md_toc.write_string_on_file_between_markers('foo.md', toc, '\u003c!--TOC--\u003e')\n\u003e\u003e\u003e f = open('foo.md')\n\u003e\u003e\u003e print(f.read(), end='')\n# Table of contents\n\n\u003c!--TOC--\u003e\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\u003c!--TOC--\u003e\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\u003chttps://docs.franco.net.eu.org/md-toc/\u003e\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 \u003chttp://www.gnu.org/licenses/\u003e.\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","funding_links":["https://tidelift.com/funding/github/pypi/md-toc","https://liberapay.com/frnmst","https://www.buymeacoffee.com/frnmst"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrnmst%2Fmd-toc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrnmst%2Fmd-toc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrnmst%2Fmd-toc/lists"}