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

橙三吉。 提交于 2019-12-03 01:25:42

问题


If you look here: http://en.wikipedia.org/wiki/Stack_Overflow

You'll notice there's a little "Content" section, if you click on one of the links, it will send you to a specific section on the page.

How do I do this in GitHub wiki? With Markdown or whatever they use?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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:

  • Use AsciiDoc instead as per suggestion from this comment. For example:

    :toc: macro
    :toc-title:
    :toclevels: 99
    # Title
    
    toc::[]
    
    ## A
    
    ### A2
    
    ## B
    
    ### B2
    

    Check the example at littlebits/react-popover (README.adoc).

  • Online Table Of Content Generator (raychenon/play-table-of-contents)

  • arthurhammer/github-toc - browser extension that adds a table of contents to GitHub repos



回答5:


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




回答6:


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.



回答7:


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

== First ==

== Second ==



回答8:


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:



来源:https://stackoverflow.com/questions/18244417/how-do-i-create-some-kind-of-table-of-content-in-github-wiki

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