将目录添加到标记文件
mdtoc的Python项目详细描述
标记目录生成器
mdtoc
是一个命令行实用程序,用于在Markdown(.md)文件中生成目录
Supports | Python 2.7 | 3.4 | 3.5 | 3.6 | 3.7 |
Latest Release | |
Package Status | |
License |
安装
通过pip install mdtoc
安装。
基本用法
将这些分隔符添加到标记文件中:
<!---toc start-->
<!---toc end-->
然后,从命令行运行:
$ mdtoc /path/to/myfile.md
这将覆盖目标文件/path/to/myfile.md
的位置,用目录替换上面标记的分隔符之间的文本。(呈现时分隔符本身是不可见的注释。)如果要写入新文件,请使用--outfile
。如果只想将toc写入stdout,请使用--stdout
。
技术细节
mdtoc
只解析标记为“atx样式”的头:在行的开头,后跟头的1-6个哈希字符(#
)。它不会not当前检测到“setext style”(带下划线)头。
mdtoc
还合并了来自GitHub-flavored Markdown的规则,从而提供了更完整的规范:An ATX heading consists of a string of characters, parsed as inline content, between an opening sequence of 1–6 unescaped # characters and an optional closing sequence of any number of unescaped
#
characters. The opening sequence of#
characters must be followed by a space or by the end of line. The optional closing sequence of#
s must be preceded by a space and may be followed by spaces only. The opening # character may be indented 0-3 spaces. The raw contents of the heading are stripped of leading and trailing spaces before being parsed as inline content. The heading level is equal to the number of # characters in the opening sequence.
请将GitHub-flavored Markdown规则视为此工具使用的最终经验规则集。如果发现有违反此规则的情况,欢迎使用拉取请求。
您还可以查看tests/examples.md
中的实例这个文档是GitHub采用降价格式的直观证明,还包括一些甚至GitHub页面本身都没有涉及的新颖示例
GitHub的一个突破是,这个工具将制表符作为空格进行计数,无论是出于何种目的这与github规范不同,github规范将空间严格定义为U+0020
。
另外两个小音符:
mdtoc
将忽略标记代码块(```
)中以#
开头的注释。mdtoc
不检查头级别的一致性/连续性。如果3级标题直接位于1级标题之后,则会按原样呈现。
完整的命令行帮助文档
$ mdtoc --help
usage: mdtoc [-h] [--version] [--check-links] [--outfile OUTFILE | --stdout]
markdown_file
Generates table of contents for Markdown files.
The algorithm searches for the text blocks
between the delimiters:
<!---toc start--->
... anything ...
<!---toc end--->
The contents of the block are then replaced
by a table of contents.
positional arguments:
markdown_file relative or abs. path of the Markdown
(.md) file to overwrite
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
--check-links find all hyperlinks and ensure that
they point to something valid
--outfile OUTFILE instead of overwriting the input file,
write to this file instead
--stdout don't write or overwrite any file;
just print the TOC to stdout
Created by Scott Frazer (https://github.com/scottfrazer).