Working with Table of Contents - Array Structure

What is the breakdown of the .TableofContents arrary?

For various resons I need to be able to get at individual entries so I can ignore some and use other and in general custom format the table as I please.
As it turns out in the example I have there is no 1,2,3rd level headings just 4th in the table and the full array starts with the markers for the uppers levels, that won’t work looks horrible. Plus I need to not display the first entry.

So I guess I just need to know .TableofContents.What?? to access various levels and entries.

Too…Any way to get hugo/go to display an array’s structure?

While I am on this topic I also do not understand the various page arrays’ contexts very well.

For example
I want to allow end-user editors to thow in a TOC anywhere in the doucment. I have a shortcode {{% toc %}} with corresponding html file

if I put simply
{{ .TableOfContents. }}
into the shortocde file I get this error
ERR: template: shortcodes/toc.html:2:3: executing “shortcodes/toc.html” at <.TableOfContents>: TableOfContents is not a field of struct type *hugolib.ShortcodeWithPage

if I put
{{ Page.TableOfContents. }}
into the shortocde file I get this error
ERR: html/template:shortcodes/toc.html: “shortcodes/toc.html” is an incomplete

if I put {{ .TableOfContents. }} it in my single.html template no problem it renders.

I guess I don’t saavy the scope/context that shortcodes live in. How to I make this work?

In the second example, {{ .Page.TableOfContents }} should work.

.TableOfContents is an HTML string, not an array or tree structure. On my “I should really do this” list of things to do with Hugo, I want to parse it as a tree structure so that we can write better versions of it.

2 Likes

should I make a formal feature request then? Maybe it should be a structure in future versions for the reasons mentioned with some default method for rendering it to html for those who don’t want to mess with it as it is. As it is though is pretty useless to me.

On Fri, Dec 12, 2014 at 2:03 PM, halostatue <discuss@gohugo.io> wrote:


halostatue
December 12

.TableOfContents is an HTML string, not an array or tree structure. On my “I should really do this” list of things to do with Hugo, I want to parse it as a tree structure so that we can write better versions of it.


To respond, reply to this email or visit http://discuss.gohugo.io/t/working-with-table-of-contents-array-structure/470/4 in your browser.

To unsubscribe from these emails, visit your user preferences.

It would be worth making that a feature request because I’m not sure when I’m going to get to it.

This doesn’t seem to work for me. I’ve created the following shortcode (toc.ace):

div
  table
    tr
      td Path
      td '{{ .Page.Path }}'

    tr
      td Summary
      td '{{ .Page.Summary }}'

    tr
      td URL
      td '{{ .Page.URL }}'

    tr
      td TOC
      td '{{ .Page.TableOfContents }}'

and call it from a content page (basics.md). The rendered result is:

Path 'basics.md
Summary ''
URL ''
TOC ‘’

I’ve tried putting {{ .TableOfContents }} directly in my layouts/_default/single.ace template and that works fine (but of course I don’t want a TOC on every single content page, nor does the top-level template know the best place to put it — that’s why I want to use a shortcode). So… could it be that shortcodes are rendered during page rendering, meaning that some fields of hugolib.Page are still uninitialized?

This is the story about the chicken and the egg.

One could maybe solve your case by delaying the shortcode processing, but you would then get support questions from people using the content and complaining about not seeing the shortcode content … If you can come up with a solution to that, you struck gold!

And the above also does look hacky. Why not put it in a partial?

Do you mean to put it in a partial that I call from the shortcode? That seems to have the same effect. If you mean why don’t I put it in a partial that I call from my template, well, that way I have to put it either before or after the .Content, whereas I’d like to put it in the middle of the content, between the introductory paragraph and the rest of the content.

The template approach could work if there were another method on hugolib.Page that returned “all of the content that isn’t in the summary”… then I could use the following in my single template:

{{ .Summary }}
{{ .TableOfContents }}
{{ .EverythingElse }}

The second. The correct way.

I notice what you want is something I want that we really couldn’t build yet without some really ugly hacks.

With the above implemented we could give you the different sections (at least for Blackfriday) without the penalty cost of doing byte searching.

Thanks: it looks like that (which would enable fixing Hugo issue 716) is really what I want. I’ll subscribe to those issues now. :slight_smile:

I am trying to solve the same problem (https://discuss.gohugo.io/t/accessing-table-of-content-tree/5863)

@dkebler did you figure out how to access table of content structure, or end up making a feature request?