How do I create some kind of table of content in GitHub wiki?

懵懂的女人 提交于 2019-12-02 15:02:10

It is nicely demonstrated in the Table of Contents of the Markdown Cheatsheet.

##### Table of Contents  
[Headers](#headers)  
[Emphasis](#emphasis)  
...snip...    
<a name="headers"/>
## Headers

If you hover over a Header in a GitHub Markdown file, you'll see a little link simple to the left of it, you can also use that link. The format for that link is <project URL#<header name>. The <header name> must be all lower case.

One possible (semi-automated) solution is Eugene Kalinin's github-markdown-toc. This tool essentially crunches through your README.md file and snarfs out #'s headings to create a TOC.

  1. Download the script https://github.com/ekalinin/github-markdown-toc
  2. Feed your README.md to the script (as noted in Eugene's README.md)

    cat README.md | bash github-markdown-toc

  3. Cut and paste generated TOC and place it at the top of your README.md file

Note that this bash implementation only works on Linux (from what I can tell).

As a side note, there is a golang implementation and is probably more of a hassle to get working.

https://github.com/jonschlinkert/markdown-toc

  • git clone your-repo.wiki.git (add the .wiki right before .git to clone the wiki
  • npm i -g markdown-toc
  • Insert <!-- toc --> (case sensitive) in your wiki's markdown
  • markdown-toc -i my-wiki-markdown.md (-i will edit it in place)
  • Profit

Update: I think maybe https://github.com/thlorenz/doctoc is more popular now.

kenorb

Currently it's not possible to do that using markdown syntax (.md). There is ongoing unofficial discussion about automatically generating table of contents TOC on rendered markdown files like README.md which lists some of the ideas.

However there are some other workarounds such as:

Since github cannot use TOC directly, but we have other alternatives.

You can automatically generate TOC via Online tool:

Generate TOC Table of Contents from GitHub Markdown or Wiki Online

or via Local tool:

github-markdown-toc

If you are not in the position to stick with Markdown, you can do as below:

  • on GitHub/wiki: switch Markdown to MediaWiki. Use __TOC__ Syntax. See sample.
  • on GitHub/repo: switch Markdown to AsciiDoc. Use :toc: Syntax. See demo.

However, using Markdown files in GitHub/repo, you can get it on GitHub Pages like in Wikipedia

  • when Jekyll is activated, it generates GitHub Pages using Kramdown by default
  • Kramdown comes with Table Of Content. Use {:toc} Syntax. See the explanation.
Stefan

You can choose the Edit mode "MediaWiki" which will generate a toc for the headers, e.g.

== First ==

== Second ==

Simplest solution to me (while I always have node.js server installed and since npm have npx) is by executing npx markdown-toc. It seems like it is one of most popular solution to this task:

ls
cat <<EOF >> test.md | tee 
## Table of Contents

<!-- toc -->
- old toc 1
- old toc 2
- old toc 3
<!-- tocstop -->

## abc
This is a b c.

## xyz
This is x y z.
EOF
ls
cat test.md
npx markdown-toc -i test.md
cat test.md

output:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!