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

后端 未结 9 613
孤城傲影
孤城傲影 2021-01-30 02:05

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

相关标签:
9条回答
  • 2021-01-30 02:39

    Due to the fact that github has it's own way of generating id=".." attributes in h1, h2, h3, etc... headers in html version after processing Markdown (for example Bitbucket use little different pattern of sluggifying headers title to id="slug") it is handy to don't reinvent the wheel and use library that reverse engineered this process.

    I found one quite good library for this task called markdown-toc.

    For me it seems the best solution because I always have installed node on my machine.

    Just execute npx markdown-toc -i file.md.

    And it looks like it is one of more popular tools for this task - at least in node.js ecosystem.

    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:

    0 讨论(0)
  • 2021-01-30 02:47

    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.

    0 讨论(0)
  • 2021-01-30 02:49

    Visual Studio Code

    If you happen to use Visual Studio Code, there is easy-to-use extension called Markdown All in One that can make the TOC for any .md file in an instant.

    Just open Command Palette (Ctrl-Shift-P) -> Markdown: Create Table of Contents

    Auto-update messes your edited TOC?

    As an additional tip, you might want to turn the "automatic TOC updates on save" OFF by using

      "markdown.extension.toc.updateOnSave": false,
    

    in your Visual Studio Settings (Command Palette -> Preferences: Open Settings (JSON)).

    0 讨论(0)
提交回复
热议问题