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

Add Level to the Heading struct #10776

Closed
jmooring opened this issue Mar 2, 2023 · 5 comments · Fixed by #11774
Closed

Add Level to the Heading struct #10776

jmooring opened this issue Mar 2, 2023 · 5 comments · Fixed by #11774

Comments

@jmooring
Copy link
Member

jmooring commented Mar 2, 2023

Use Case 1

Building a TOC by walking .Page.Fragments.Headings works great, and it's easy to control the start and end levels from site and/or page params.

[params.toc]
startLevel = 2
endLevel = 4

However, to do this, the partial needs to keep track of the current Level while walking the map, adding 1 each time it goes a level deeper. This is not difficult to do, but would be a bit easier if we could access .Level for each heading.

Use Case 2

Control whether or not a TOC is displayed based on the number of headings of a certain level or levels.

[params.toc]
startLevel = 2
endLevel = 4
minNumHeadings = 3

If there are 3 or more headings of Level 2, 3, or 4 (combined), then display the TOC, otherwise don't.

I could walk the map once to get the counts, then walk it again to render, but it would be easier if I could get the counts up front. While some sort of count method would be ideal, adding Level would be sufficient.

{{ $count := where (sort .Fragments.HeadingsMap) "Level" "in" (slice 2 3 4) | len }}

The example above works because sort removes the first level keys.

@bep
Copy link
Member

bep commented Mar 3, 2023

Yea, we can add that.

But I think what you ask for gets less obvious if you start from an arbitrary level, e.g.

{{ $startHeading := index .Fragments.HeadingMap "heading-on-level3" }}
// How do I  walk 2 level down from where I am now?

Building a ToC with this new structure would always need a recursive template, so passing add $currentLevel 1 to the template shouldn't be too hard.

@jmooring
Copy link
Member Author

jmooring commented Mar 3, 2023

Yea, we can add that.

I suggest waiting until a use case with more value comes along.

add $currentLevel 1 to the template shouldn't be too hard

Agreed.

@bep bep added Proposal and removed Enhancement labels Mar 3, 2023
@l1x
Copy link

l1x commented Mar 9, 2023

{{ range .Fragments.HeadingsMap }}
{{ $headings = $headings | append . }}
{{ end }}

Could you show a complete example please?

{{ range .Fragments.HeadingsMap }}
  {{ $headings = $headings | append . }}
{{ end }}
plate failed: template: posts/single.html:19:17: executing "main" at <$headings>: undefined variable: $headings
Error: Error building site: failed to render pages: render of "page" failed: "layouts/posts/single.html:19:17": execute of template failed: template: posts/single.html:19:17: executing "main" at <$headings>: undefined variable: $headings

Thanks in advance!

@khayyamsaleem
Copy link
Contributor

@l1x This kind of question is better suited for https://discourse.gohugo.io/ so we can focus the discussion on @jmooring 's feature request.

However, the error you're encountering is because there's no $headings variable initialized, so before the range loop, you can just initialize it as an empty slice:

{{ $headings := slice }}
{{ range .Fragments.HeadingsMap }}
  {{ $headings = $headings | append . }}
{{ end }}

But best to ask these question on discourse; something like "Collect headings from .Page"

@bep bep modified the milestones: v0.112.0, v0.113.0 Apr 15, 2023
@bep bep modified the milestones: v0.113.0, v0.115.0 Jun 13, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 5, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
jmooring added a commit to jmooring/hugo that referenced this issue Dec 2, 2023
@bep bep added Enhancement and removed Proposal labels Dec 4, 2023
@bep bep closed this as completed in #11774 Dec 4, 2023
bep pushed a commit that referenced this issue Dec 4, 2023
Copy link

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 Jan 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants