Links in TOC, .TableOfContent bypasses render-link.html

In some cases I use chapters headings to redirect elsewhere, to not make the page too long.
So it gets like this:

## Exosomes and viruses: two sides of the same coin: [⇐](virus-exosome-en)

Here is the part of render-link.html that creates the destination ($destination) part

{{- $destination := .Destination }}
{{if eq $destination "add_ref"}}
	{{.Text}}
{{else}}
	{{- $isRemote := or (or (in $destination ":") (strings.HasPrefix $destination "//")) (strings.HasPrefix $destination "//") }}
	{{- if $isRemote }}
		{{ .Page.Scratch.Add "Sources" (slice (dict "url" .Destination ".Text" .Title)) }}
	{{- else }}
	  {{- $url := urls.Parse .Destination }}
	  {{- $path := strings.TrimSuffix "/_index.md" $url.Path }}
	  {{- $path = strings.TrimSuffix "/_index" $path }}
	  {{- $path = strings.TrimSuffix ".md" $path }}
	  {{- $page := site.GetPage $path }}
	  {{- if $page }}
	    {{- $destination = $page.RelPermalink }}
	    {{- if $url.Fragment }}
	      {{- $destination = print $destination "#" $url.Fragment }}
	    {{- end }}
	  {{- else if fileExists (print .Page.File.Dir .Destination) }}
	    <!-- Nothing -->
	  {{- else -}}
		{{ with resources.Get .Destination }}
		{{ $destination = .RelPermalink}}
	  {{ end}}
	{{- end }}
{{- end }}

But .TableOfContent bypasses render-link.html completely ! To me this is a dysfunction. It reads href=virus-exosome-en, which would obviously be the address of a page ressource.
How do I avoid that ?

If I don’t misunderstand, the TOC will extract and make a link to the heading in the {{ .Content }} (H1, H2, H3 … H6) which the lower and upper can be configured by TOC site config.

1 Like

Yes. as I said it bypasses render-link.html. it creates its own link with whatever it finds within the heading, and that is a faulty behavior. Be it a bug or a faulty design, it’s still bad, whatever’s inside the heading should have it’s markdown processed before going through .TableOfContent

But a link in a table of content should only ever point to a heading on the same page, a fragment identifier on a heading element. If it doesn’t it’s not a table of contents, it’s an index page.

That it’s faulty behaviour isn’t obvious to me – I have never thought that the ToC links should look/behave like the inline content links, and I suspect I would be very surprised if it behaved the way you suggests.

That said, with the latest Hugo version, there’s a data structure that allows you to build your own table of contents HTML.

4 Likes

Which one ?

Fragments

I see… But I don’t get how to use it, there are examples to search into it but not generate stuff from it

@Arif posted a good example. Also, you should follow the link to the Go documentation in Hugo’s. It contains some more details (but is also quite terse).

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