Embedding the Table of Content of external pages

Hi there,

(edit: you can ignore this post until I work seriously on it)

You do not seem to be using the parameter you pass into your shortcode:

1 Like

(edited, see below for a recent version)

Your code makes very little sense to me.

  • You pass a parameter to your shortcode that is never used
  • You set $page to either the empty string or to a .Page object.
  • Resources.GetMatch requires a string parameter, not a .Page object.
  • If you pass it the empty string, what do you expect to get?

Perhaps posting a link to your repository generates better responses here

You are absolutely right, I’ll take time to drink a coffee and will update this post if I still can’t find my way,
Thanks @chrillek !

Ok I’ve got something better:

{{- $page := .Get "page" -}}

{{ with $page }}
  {{ (site.GetPage $page).TableOfContents }}
{{ end }}

the only problem now is that anchors URLs do not contain the full path to the external page:
The generated URL for each of the external titles looks like #anchor instead of foo/#anchor

The solution I’m finally using:

{{- $page := .Get "page" -}}

{{ with $page }}
  {{- $toc := (site.GetPage $page).TableOfContents -}}
  {{ replace $toc `href="#` (printf `href="%s#` (site.GetPage $page).RelPermalink) | safeHTML }}
{{ end }}

You might want to store the result of (siteGetPage $page).RelPermalink in a variable and use that inside the loop. Old thing: Move invariants out of loops.

1 Like

Related resources, for the record:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.