# Spec: Table of Contents

**URL:** <https://meta.discourse.org/t/spec-table-of-contents/32248>\
**Category:** Feature\
**Created:** [August 18, 2015, 1:46pm UTC](https://meta.discourse.org/t/spec-table-of-contents/32248 "2015-08-18T13:46:42Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Tom\_Newsom](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tom_newsom/32/115981_2.png) [@Tom\_Newsom](https://meta.discourse.org/u/Tom_Newsom)\
**Post date:** [August 18, 2015, 1:46pm UTC](https://meta.discourse.org/t/spec-table-of-contents/32248/1 "2015-08-18T13:46:42Z")

</div>

Continuing the discussion from [Is there markdown for creating a table of contents in a topic?](https://meta.discourse.org/t/is-there-markdown-for-creating-a-table-of-contents-in-a-topic/15918/10):

> [@tobiaseigen](#):
>
> Even more lovely would be to be able to add `[toc]` in a post to automatically generate this list of links to headers within the post.

Ok, I’d really like to see this happen, so here’s my attempt at making a spec for it.

> This feature allows the automatic creation of a table of contents for any post (although it’s best suited to Wiki posts).

## Details

- To invoke the feature, add the `[contents]` markup to a post.
- After cooking the markdown, the code scans the post _below_ the `[contents]` markup and adds an anchor before each Header element (this allows for a preamble or top-level title to be excluded from the contents).
- The text and anchor of those Header elements are then composed into a `<ul>` and inserted at the location of the `[contents]` markup.
- Each sub-list should be indented by relative hierarchy, not Header size. This allows for “jumps” in Header size to produce a tidily indented list.
- List item styling, however, _is_ applied based on the Header level it refers to.

Examples with the list item styles **H1** _H2_ and H3:

**H1**  
&nbsp;&nbsp;_H2_  
&nbsp;&nbsp;_H2_  
&nbsp;&nbsp;_H2_

**H1**  
&nbsp;&nbsp;H3  
&nbsp;&nbsp;H3  
&nbsp;&nbsp;H3

_H2_  
&nbsp;&nbsp;H3  
&nbsp;&nbsp;H3  
&nbsp;&nbsp;H3

**H1**  
&nbsp;&nbsp;H3  
&nbsp;&nbsp;H3  
&nbsp;&nbsp;_H2_  
&nbsp;&nbsp;&nbsp;&nbsp;H3  
&nbsp;&nbsp;&nbsp;&nbsp;H3

**H1**  
&nbsp;&nbsp;_H2_  
&nbsp;&nbsp;&nbsp;&nbsp;H3  
&nbsp;&nbsp;&nbsp;&nbsp;H3  
&nbsp;&nbsp;_H2_  
&nbsp;&nbsp;&nbsp;&nbsp;H3  
&nbsp;&nbsp;&nbsp;&nbsp;H3

## Options

`[contents min=1 max=3]`

where min and max are the limits of H elements to index. I suppose it should support up to H6 given that those can be generated with HTML. 1 and 3 are the default if left unspecified.

I toyed with a style= parameter for bullets or numbers, but I think numbers will be misleading because the contents of the post are subject to change, making the numbering inconsistent over time.

## Example output

Pay close attention to the numbers!

```plaintext
<div class="toc-wrapper">
  <ul class ="toc-h1">
  <li><a href="#h1-1">Header text</a></li>
  <li><a href="#h1-2">Header text</a></li>
  <li><a href="#h1-3">Header text</a></li>
    <ul class="toc-h2">
    <li><a href="#h2-1">Subheader text</a></li>
    <li><a href="#h2-2">Subheader text</a></li>
      <ul class="toc-h3>
      <li>a href="#h3-1">Sub-subheader text</a>
      <li>a href="#h3-2">Sub-subheader text</a>
      <li>a href="#h3-3">Sub-subheader text</a>
     </ul>
    <li><a href="#h2-3">Subheader text</a></li>
    </ul>
  <li><a href="#h1-4">Header text</a></li>
  <li><a href="#h1-3">Header text</a></li>
    <ul class="toc-h3">
    <li><a href="#h3-4">Sub-subheader text</a></li>
    <li><a href="#h3-5">Sub-subheader text</a></li>
    </ul>
  </ul>
</div>

```

Where “h1- **3** ” is the anchor of the **third** H1 element in the topic etc.

I suppose you could generate random strings if you want to reduce the chances of clashing with a user-generated Anchor somewhere else in the post.

## ##Mockup

This post: [Laser Cutter - Trotec Speedy 300 (60W) - Tools - Discourse – South London Makerspace](https://discourse.southlondonmakerspace.org/t/laser-cutter-trotec-speedy-300/14)

 ![](https://global.discourse-cdn.com/meta/original/3X/d/a/da4c774f339a8a2d13212c58764ff8e17b653974.png) 

Makes his sort of thing, only prettier because I’m just slapping this together in Paint. If I have time later, I might do a more carefully designed mockup.

 ![](https://global.discourse-cdn.com/meta/original/3X/f/f/ff21b4c335910f66ef8935bbda0a076cdcb57cf1.png) 

##Thoughts

It takes up a lot of vertical space. Maybe it could be pushed into one of the gutters at wider window sizes? Or even float over the gutter as you scroll, so you can always access it from all points in the post?

---

_[View the full topic](https://meta.discourse.org/t/spec-table-of-contents/32248)._
