The script will generate a TOC for a Markdown file, based on the headers in the file.
The script will generate a Table of Contents for a Markdown file, based on the headers in the file.
It generates and prints the TOC to the console, and optionally also inserts it into the original file.
It generates and prints the TOC to the console, and optionally also inserts it into the original file.
For the latter, it will first create a backup copy of the file and then replace any section named "# Contents" with the new table of contents **in the original input document**.
For the latter, it will first create a backup copy of the file and then replace any section named "# Contents" with the new table of contents **in the original input document**. To keep the original document intact, it may be a good idea to first create a backup copy of the document and work on that copy.
Alternatively, one can specify an output file name with the `--outfile` option. The script will then create a new file.
### Support for Table-of-Content Tags
### Support for Table-of-Content Tags
Some renderers support special tags in the Markdown document to automatically insert a TOC. This script supports the following tags:
Various markdown renderers support special tags in the Markdown document to automatically insert a TOC. This script supports the following tags:
-`<!--TOC-->`
-`<!--TOC-->`
-`[TOC]`
-`[TOC]`
-`[CONTENT]`
-`[CONTENT]`
If the `--toctags` and `--add-content` options are specified, the script will replace these tags with the generated TOC in the original input document. To keep the original document intact, it may be a good idea to first create a backup copy of the document and work on that copy.
If the `--toctags` and `--add-content` options are specified, the script will replace these tags with the generated TOC in the original input document.
## Prerequisites
## Prerequisites
- Python 3.8 or higher
- Python 3.10 or higher
## Usage
## Usage
...
@@ -46,5 +48,8 @@ options:
...
@@ -46,5 +48,8 @@ options:
indent spaces for each level (default: 4)
indent spaces for each level (default: 4)
--level LEVELS, -l LEVELS
--level LEVELS, -l LEVELS
limit the TOC levels; 0 means no limit (default: 0)
limit the TOC levels; 0 means no limit (default: 0)
--outfile OUTFILE, -o OUTFILE
set the output file name; if not set, the input file will be
overwritten (default: None)
--toc-tags, -t replace special TOC tokens in the document (default: False)
--toc-tags, -t replace special TOC tokens in the document (default: False)