# Table Of Content HTML partial

**URL:** <https://discourse.gohugo.io/t/table-of-content-html-partial/35406>\
**Category:** support\
**Created:** [November 2, 2021, 8:32am UTC](https://discourse.gohugo.io/t/table-of-content-html-partial/35406 "2021-11-02T08:32:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benmarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/benmarte/32/5948_2.png) [@benmarte](https://discourse.gohugo.io/u/benmarte)\
**Post date:** [November 2, 2021, 8:32am UTC](https://discourse.gohugo.io/t/table-of-content-html-partial/35406/1 "2021-11-02T08:32:27Z")

</div>

Is it possible to customize the HTML markup of how the TOC renders? Currently, it only renders the anchor without the page’s permalink rendering this feature unusable.

I’ve tried making a render-link and a toc partial but that does not seem to produce what I want.

It would be great if we could range `{{.TableOfContents}}` and render it however we choose instead of hugo deciding how to render it.

I’m going to make this a feature request in the meantime if anyone knows how to accomplish this I would greatly appreciate any insight.

Thanks.

---

<div class="post-metadata">

**Author:** ![benmarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/benmarte/32/5948_2.png) [@benmarte](https://discourse.gohugo.io/u/benmarte)\
**Post date:** [November 2, 2021, 9:45am UTC](https://discourse.gohugo.io/t/table-of-content-html-partial/35406/2 "2021-11-02T09:45:29Z")

</div>

Just leaving this here in case anyone else is looking to solve this issue, here is how I fixed it using javascript.

```auto
// get all page headers

var headings = document.querySelectorAll("h1, h2, h3, h4, h5, h6");

// table of contents container typically a: <ul id=""toc"></ul>

var tableOfContents = document.querySelector("#toc");

// function to render TOC

function renderTOC () {
  var toc = [...headings].map(link => `<li>${link.innerHTML}</li>`).join('');
  return tableOfContents.innerHTML = toc;
}

renderTOC();

```

And this is what my render-heading.html template looks like in order to fix proper anchor links:

```auto
<h{{ .Level }} id="{{ .Anchor | safeURL }}"><a href="{{.Page.URL}}#{{ .Anchor | safeURL }}" class="has-text-black">{{ .Text | safeHTML }}</a></h{{ .Level }}>

```

Hopefully, this will help others.

---

<div class="post-metadata">

**Author:** ![lbliii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lbliii/32/9902_2.png) [@lbliii](https://discourse.gohugo.io/u/lbliii)\
**Post date:** [February 7, 2023, 7:18pm UTC](https://discourse.gohugo.io/t/table-of-content-html-partial/35406/3 "2023-02-07T19:18:36Z")

</div>

bumping this. i really love the `_markup/render-codeblock.html` functionality that we have for headers, images, etc… could we get one for `_markup/render-toc.html`?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 7, 2023, 10:19pm UTC](https://discourse.gohugo.io/t/table-of-content-html-partial/35406/4 "2023-02-07T22:19:37Z")

</div>

I’m not convinced there would be much value to a render hook for the Table of Contents (TOC), primarily due to the limitations of the existing `.TableOfContents` method.

The right way to generated a TOC is _after_ the HTML has been written to the public directory.

1. Generate the TOC with JavaScript ([tocbot.js](https://tscanlin.github.io/tocbot/), etc.)
2. Generate the TOC with a partial as suggested [here](https://discourse.gohugo.io/t/shortcodes-within-shortcodes-not-rendering/42343/2)

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [February 9, 2023, 10:20pm UTC](https://discourse.gohugo.io/t/table-of-content-html-partial/35406/5 "2023-02-09T22:20:04Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
