Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Table of contents #97

Open
ilyasustun opened this issue Jul 21, 2017 · 3 comments
Open

Table of contents #97

ilyasustun opened this issue Jul 21, 2017 · 3 comments

Comments

@ilyasustun
Copy link

How can I add a table of contents that reads the titles and subtitles from the markdown, and rmarkdown document? If it is not possible, can such functionality be added?

@schlawiner
Copy link

schlawiner commented Sep 2, 2017

This should help you: https://gohugo.io/content-management/toc/
In short: add the {{.TableOfContents}} page variable into your template where you want it to appear. I added it right above {{ .Content }} in my single_article.html partial (I made a copy of it in hugo/layouts/partials/single_article.html to override the default and leaving the original untouched for easier updating):

    <div class="article-entry" itemprop="articleBody">  
        {{ .TableOfContents }}  
        {{ .Content }}  
    </div>

@bluprince13
Copy link

Adding {{.TableOfContents}} does not work for me. It renders as the text ' {{.TableOfContents}}' when I view it on the browser.

@bluprince13
Copy link

bluprince13 commented Nov 4, 2017

Ah! .TableOfContents is a variable, so you can't use it directly in a markdown file.

You can create your own shortcode for use in a markdown file.

  1. I created a new file layouts/shortcodes/toc.html with the one liner:
    {{ .Page.TableOfContents }}

  2. Then in my markdown file, I just call
    {{< toc >}}
    wherever I want the table of contents to appear. Easy peasy!

Btw, @schlawiner's answer shows how to override a theme, which will include a toc in every article.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants