1

Is there a way to create anchors within a blog post and link to them like one would with html hrefs to an element?

I basically want a table of contents with links on the top of a long blogpost.

There is a question on this already and it seems like there in no actual answer without plugs, wanted a confirmation: How to create a table of contents to Jekyll blog post?

Community
  • 1
  • 1
VSO
  • 11,546
  • 25
  • 99
  • 187

1 Answers1

1

Is there a way to create anchors within a blog post and link to them like one would with html hrefs to an element?

Yes, using html hrefs to an element. The following html code:

<hr>

<h1>Table of contents:</h1>

<ul>
  <li><a href="#goto-1">Chapter 1</a><a id="toc-1"></a></li>
  <li><a href="#goto-2">Chapter 2</a><a id="toc-2"></a></li>
</ul>

<hr>

<a id="goto-1"></a>
<h1><a href="#toc-1">↑</a> Chapter 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<a id="goto-2"></a>
<h1><a href="#toc-2">↑</a> Chapter 2</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

Can be generated with the following markdown in a post:

---

**Table of contents:**

* [Chapter 1](#goto-1)<a id="toc-1"></a>
* [Chapter 2](#goto-2)<a id="toc-2"></a>

---

<a id="goto-1"></a>**[↑](#toc-1) Chapter 1**
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

<a id="goto-2"></a>**[↑](#toc-2) Chapter 2**
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
rocambille
  • 15,398
  • 12
  • 50
  • 68