Search code examples
githubhyperlinkmarkdownheadingreadme

How to link to heading containing dot (.) or comma (,) in markdown file (Table of contents)?


In my Readme.MD markdown file I want to create Table of Contents (for Github). [TOC] (generated by Typora) doesn't show on Github. It shows like [TOC]. Some of my headings contain characters like a dot or a comma. Is there any way to link to these headings?

I tried almost everything I think, such as: [app.js File](#app.js-file), [app.js File](#app-js-file), [app.js](#app--js-file), [app.js](#app\.js-file), [app.js](#appjs-file). Nothing works.

Here is an example:

Table of contents:
[app.js](#app-js)
[script.js](#script.js)
[One, two, three](#one,-two,-three)

# app.js File
Some text

# script.js File
Some text

# One, two, three
Some text

P.S. Some people are talking about creating HTML-anchors (named), but how/where do you write them in your MD-file (in case it is the solution). Could you explain it please (I couldn't find any solution on the internet).


Solution

  • If you mouse over the header you want to link to in the rendered content you should see a link icon appear. Mouse over that icon to see what it's linking to, and use that as your target. I think you'll find the links named

    • appjs-file
    • scriptjs-file
    • one-two-three

    This answer points to some logic that may drive link naming but I haven't been able to confirm.

    As a side note, Markdown is designed to be easily read even in its raw form. I strongly recommend adding a space between the header hashes and header text and a blank line between the header and its content, e.g.

    # app.js File
    
    Some text
    

    Some Markdown processors will require one or both of these to render properly.