How to link to part of the same document in Markdown?

前端 未结 14 1676
眼角桃花
眼角桃花 2020-11-29 14:50

I am writing a large Markdown document and would like to place a table of contents of sorts at the beginning that will provide links to various locations in the document. Ho

14条回答
  •  無奈伤痛
    2020-11-29 15:11

    Some additional things to keep in mind if ya ever get fancy with symbols within headings that ya want to navigate to...

    # What this is about
    
    
    ------
    
    
    #### Table of Contents
    
    
    - [About](#what-this-is-about)
    
    - [⚡ Sunopsis](#9889-tldr)
    
    - [:gear: Grinders](#it-grinds-my-gears)
    
    - [Attribution]
    
    
    ------
    
    
    ## ⚡ TLDR
    
    
    Words for those short on time or attention.
    
    
    ___
    
    
    ## It Grinds my :gear:s
    
    
    Here _`:gear:`_ is not something like ⚙ or ⛭
    
    
    ___
    
    
    ## ⛤ Attribution
    
    
    Probably to much time at a keyboard
    
    
    
    [Attribution]: #9956-attribution
    

    ... things like #, ;, &, and : within heading strings are generally are ignored/striped instead of escaped, and one can also use citation style links to make quick use easier.

    Notes

    GitHub supports the :word: syntax in commits, readme files, etc. see gist(from rxaviers) if using'em is of interest there.

    And for just about everywhere else decimal or hexadecimal can be used for modern browsers; the cheat-sheet from w3schools is purdy handy, especially if using CSS ::before or ::after pseudo-elements with symbols is more your style.

    Bonus Points?

    Just in case someone was wondering how images and other links within a heading is parsed into an id...

    - [Imaged](#alt-textbadge__examplehttpsexamplecom-to-somewhere)
    
    
    ## [![Alt Text][badge__example]](https://example.com) To Somewhere
    
    
    [badge__example]:
      https://img.shields.io/badge/Left-Right-success.svg?labelColor=brown&logo=stackexchange
      "Eeak a mouse!"
    

    Caveats

    MarkDown rendering differs from place to place, so things like...

    ## methodName([options]) => Promise
    

    ... on GitHub will have an element with id such as...

    id="methodnameoptions--promise"
    

    ... where as vanilla sanitation would result in an id of...

    id="methodnameoptions-codepromisecode"
    

    ... meaning that writing or compiling MarkDown files from templates either requires targeting one way of slugifeing, or adding configurations and scripted logic for the various clever ways that places like to clean the heading's text.

提交回复
热议问题