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 · 7 comments

Comments

Projects
None yet
3 participants
@targodan
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

This comment has been minimized.

Show comment
Hide comment
@stefano-pogliani

stefano-pogliani Oct 13, 2016

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?

stefano-pogliani commented Oct 13, 2016

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

This comment has been minimized.

Show comment
Hide comment
@targodan

targodan Oct 13, 2016

Contributor

@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.

Contributor

targodan commented Oct 13, 2016

@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

This comment has been minimized.

Show comment
Hide comment
@bep

bep Oct 13, 2016

Member

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.

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 Oct 13, 2016

@targodan

This comment has been minimized.

Show comment
Hide comment
@targodan

targodan Oct 14, 2016

Contributor

@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?

Contributor

targodan commented Oct 14, 2016

@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

This comment has been minimized.

Show comment
Hide comment
@targodan

targodan Oct 18, 2016

Contributor

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

Contributor

targodan commented Oct 18, 2016

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

This comment has been minimized.

Show comment
Hide comment
@bep

bep Oct 20, 2016

Member

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!

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

This comment has been minimized.

Show comment
Hide comment
@targodan

targodan Oct 20, 2016

Contributor

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.

Contributor

targodan commented Oct 20, 2016

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 in d9f54a1 Oct 24, 2016

tychoish added a commit to tychoish/hugo that referenced this issue Aug 13, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment