Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

markdown link to header

I'm using GitLab to write a read.me file.

I tried to create a link to a header. According to the wiki an id should be automatically created:

see here

I created a header using:

### 1. This is my Header 

and tried to create a link to it:

[link](#1--this-is-my-header) 

but it is not working. What am I doing wrong?

like image 946
user7431005 Avatar asked Jul 07 '18 09:07

user7431005


People also ask

How do I create an anchor in Markdown?

Add a custom anchor To add an anchor to a heading in HTML, add a <section> element with an id attribute. Don't use <a name> . Use lowercase for id values, and put hyphens between words. To add an anchor to a heading in Markdown, add the following code to the end of the line that the heading is on.

How do you link a heading in Obsidian?

Link to headingsStart typing a link like you would normally. When the note you want is highlighted, press # instead of Enter and you'll see a list of headings in that file. Continue typing or navigate with arrow keys as before, press # again at each subheading you want to add, and Enter to complete the link.

How do I add a link in Github Markdown?

There are two ways to create links. Or leave it empty and use the link text itself. URLs and URLs in angle brackets will automatically get turned into links. http://www.example.com or http://www.example.com and sometimes example.com (but not on Github, for example).


2 Answers

Markdown IDs are generated using some rules i've been able to google: (text to lowercase, non-word punctuation removed, spaces converted to hyphens, two or more hyphens in a row converted to one, naming collisions have incremented number appended, ...)

I found an easy way to figure out what the anchor link should be. Use your browser's HTML inspector to inspect the header you want to link to. The header tag's ID should be what you use. So for example my heading looks like this in the HTML inspector:

<h2 id="markdown-header-changing-plsql-parameters-and-shared-developers-lifecycle">   Changing PL/SQL parameters and shared developer's lifecycle </h2> 

And I can link to it in markup like so:

[See instructions below](#markdown-header-changing-plsql-parameters-and-shared-developers-lifecycle) 

And now "See instructions below" is linked to my header anchor.

like image 29
Rick Tilley Avatar answered Sep 19 '22 03:09

Rick Tilley


In the Documentation you link to we learn that...

The IDs are generated from the content of the header according to the following rules:

  1. All text is converted to lowercase.
  2. All non-word text (e.g., punctuation, HTML) is removed.
  3. All spaces are converted to hyphens.
  4. Two or more hyphens in a row are converted to one.
  5. If a header with the same ID has already been generated, a unique incrementing number is appended, starting at 1.

Note rule 4: "Two or more hyphens in a row are converted to one." However, the example you tried has two hyphens in a row (after the 1). Remove one of them and you should have it.

[link](#1-this-is-my-header) 

From time to time I have encountered a unique header which is converted into an ID in some non-obvious way. A quick way to work out the ID is to use your browser's view source and/or inspect tools to view the HTML source code. For example, you might find the following HTML for your example:

<h3 id="1-this-is-my-header">1. This is my Header</h3> 

Then just use the contents of the id attribute with a hash to link to that header: #1-this-is-my-header.

like image 161
Waylan Avatar answered Sep 21 '22 03:09

Waylan