Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table of Contents (ToC) not always rendered #2433

Closed
targodan opened this issue Sep 11, 2016 · 8 comments
Closed

Table of Contents (ToC) not always rendered #2433

targodan opened this issue Sep 11, 2016 · 8 comments
Labels

Comments

@targodan
Copy link
Contributor

targodan commented Sep 11, 2016

I wanted to add a ToC to some of my posts and found that it is not being rendered under certain circumstances.

My setup

Using the blackburn theme, added a shortcode called /layouts/shortcodes/toc.html:

<nav id="toc">
    {{ .Page.TableOfContents }}
</nav>

Added a post called /content/post/test.md:

+++
title = "Test"
+++

# ToC

{{% toc %}}

# a

a

# b

b

# c

c

Hugo version: Hugo Static Site Generator v0.16 BuildDate: 2016-09-01T17:43:30+02:00

The rendered result

When I just execute hugo like this

$ hugo -d public/

The ToC is not rendered / is empty.
The same thing happens when I execute hugo as a server.

$ hugo server

When executing as server and I modify and save the /layouts/shortcodes/toc.html it renders the ToC.
When I now change and save the /content/post/test.md again, the ToC disappears again.

Under any circumstances I did not get any errors.

By "does not render / is empty" I mean the result looks like this in the markup.

<nav id="toc">
</nav>
@stefano-pogliani
Copy link

I have a similar problem when trying to render the summary of other pages.

I use a taxonomy called pagesets to group pages and then I have a shortcode to render the list of pages in the taxonomy:

{{ $pageset := .Get 0 -}}
{{ $pages := .Site.Taxonomies.pagesets.Get $pageset }}

{{ range $pages }}
<div class="pageset-block">
  <h2>
    <a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
  </h2>
  <p>{{ .Page.Summary }}</p>
</div>
{{ end }}

I am not familiar with the Go programming language but I was able to follow the build process to the following lines:

My use case is a sort of "index page" for the pages in the pageset.
As such I suspect that, for me, the shortcode in the "index page" is processed before the pages referenced in it are.

@targodan where you able to find a solution/work around for your problem?

@targodan
Copy link
Contributor Author

@stefano-pogliani Sadly I wasn't, I just created the ToC by hand. 😒 . I have gathered a bit of experience with go. If I find the time I'll take a look at the code you found over the weekend.

@bep
Copy link
Member

bep commented Oct 13, 2016

In Hugo 0.17 shortcodes are processed as late as possible -- but do not expect us to lift the laws of physics: We cannot create a summary for the page etc. before we have processed the shortcodes, a shortcode cannot list the content that surrounds it ... Use regular templates for these operations. I'm closing this now. This belongs on the discussion forum.

@bep bep closed this as completed Oct 13, 2016
@targodan
Copy link
Contributor Author

@bep I don't know about the summary problem but ToCs are a feature of hugo that does not work. Ergo this issue should not be closed.

In the name of being constructive: I looked at the code @stefano-pogliani found and were a bit confused by the order in which things are done. First shortcodes are handled and then the ToC is created. Unless I have not considered something important this contradicts your statement of "shortcodes are processed as late as possible".

Please take a look at my commit targodan/hugo@7473e32 I tested it and now the ToC in shortcode works. Yes, this propably means that shortcodes in headings might act weiredly (the next thing I will test) but now the ToC works. On the other hand, if not in shortcodes then where can we use the .Page.TableOfContents feature?

@targodan
Copy link
Contributor Author

I opened a post in the discussion forum: https://discuss.gohugo.io/t/toc-not-working-in-shortcodes/4347

@bep bep reopened this Oct 20, 2016
@bep
Copy link
Member

bep commented Oct 20, 2016

I'll reopen this -- thinking about it this may be possible to do without too much work. But a PR that just moves some lines and "it works for me" will not be merged. Tests!

@targodan
Copy link
Contributor Author

Of course. I only went for a quick shot without tests because you closed the issue and I didn't want to waste my time, not knowing if this will be reopened.

I will add some tests and then open a pull request. I hope to find the time over the weekend but can't promise anything, as I haven't looked at how you guys do your testing yet.

@bep bep closed this as completed in d9f54a1 Oct 24, 2016
tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
@github-actions
Copy link

github-actions bot commented Apr 1, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants