Automatic TOC in github

是否有可能使用Github Flavored Markdown生成自动目录表?


I created two options to generate a toc for github-flavored-markdown:

DocToc Command Line Tool (source) requires node.js

Installation:

npm install -g doctoc

Usage:

doctoc . to add table of contents to all markdown files in the current and all sub directories.

DocToc WebApp

If you want to try it online first, go to the doctoc site, paste the link of the markdown page and it will generate a table of content that you can insert at the top of your markdown file.

Github Wikis and Anchors

As Matthew Flaschen pointed out in the comments below, for its wiki pages GitHub previously didn't generate the anchors that doctoc depends on.

UPDATE: However, they fixed this issue.


GitHub Pages (which is basically a wrapper for Jekyll) appears to use kramdown, which implements all of Maruku, and therefore has support for an automatically generated table of contents via a toc attribute:

* auto-gen TOC:
{:toc}

The first line just starts an unordered list and is actually thrown away.

This results in a nested set of unordered lists, using the headers in the document.

Note: this should work for GitHub Pages, not GitHub Flavored Markdown (GFM) as used in comments or wiki pages. AFAIK a solution doesn't exist for that.


It's not automatic, but it uses Notepad++ regular expressions:

Replace all first by the second (removes all lines not having headers)

^##(#?)(#?)(.*?)$(.|r|n)*?(?=^##|z)
-12 [3](#3)n

Then (converts headers III to spaces)

-##
        -

Then (converts headers II to spaces)

-#
    -

Then (remove unused chars at the beginning and at the end of link title)

[ *((?:(?![ .:#!?;]*])[^#])*)[ #:!?;]*]
[1]

Then (convert last tokens lowercase and dash instead of spaces)

]([^ rn]*) ([^rn ]*)
]L1-2

Remove unused final pounds and initial dashes:

(?:()[-:;!?#]+$|(]#)-)
12

Remove useless chars in links:

(].*?)(?:(|))
1

And finally add parenthesis around final links:

](?!()(.*?)$
](1)

And voilà! You can even put this in a global macro if you repeat it enough time.

链接地址: http://www.djcxy.com/p/20236.html

上一篇: 在Markdown中更改图像大小

下一篇: github中的自动TOC