Do not put TOC in Summary

2019-04-03 443 words 3 mins read

By default, The TOC generated by ox-hugo is inserted into “Summary” in Hugo.

This is how to prebent it (Taken from ox-hugo test site).

Copy summary_minus_toc.html

Copy summary_minus_toc.html from this site.

summary_minus_toc.html looks like this:

Code Snippet 1: layouts/partials/summary_minus_toc.html (html)
{{- with .Description -}}
    {{- . | markdownify | printf "<p>%s</p>" | safeHTML -}}
{{- else -}}
    {{- $summary_has_org_toc := substr .Content 0 30 | findRE "[\".]ox-hugo-toc" -}}
    {{- if $summary_has_org_toc -}}
        {{- $content_splits := split .RawContent "<!--endtoc-->" -}} <!-- Need to use .RawContent as we will be parsing for 'more' comment later. -->
        <!-- If Org TOC is present, the special comment endtoc would also be present.
             In that case, output only the part *after* that comment as Summary. -->
        {{- $summary_raw := index $content_splits 1 -}}
        {{- $summary_splits := split $summary_raw "" -}}
        {{- if eq (len $summary_splits) 2 -}}
            {{- index $summary_splits 0 | markdownify -}}
        {{- else -}}
            {{- $summary_raw | markdownify | truncate 300 -}}
        {{- end -}}
    {{- else -}}
        <!-- Print the whole Summary if endtoc special comment is not found. -->
        {{- .Summary |  printf "<p>%s</p>" | safeHTML -}}
    {{- end -}}
{{- end -}}

I modified some code as below so that raw HTML codes are not shown.

Code Snippet 2: Modified summary_minus_toc.html (html)
{{- with .Description -}}
    {{- . | markdownify | printf "<p>%s</p>" | safeHTML -}}
{{- else -}}
    {{- $summary_has_org_toc := substr .Content 0 30 | findRE "[\".]ox-hugo-toc" -}}
    {{- if $summary_has_org_toc -}}
        {{- $content_splits := split .RawContent "<!--endtoc-->" -}} <!-- Need to use .RawContent as we will be parsing for 'more' comment later. -->
        <!-- If Org TOC is present, the special comment endtoc would also be present.
             In that case, output only the part *after* that comment as Summary. -->
        {{- $summary_raw := index $content_splits 1 -}}
        {{- $summary_splits := split $summary_raw "<!--more-->" -}}
        {{- if eq (len $summary_splits) 2 -}}
            {{- index $summary_splits 0 | markdownify -}}
        {{- else -}}
            {{- $summary_raw | markdownify | plainify | truncate 300 -}}
        {{- end -}}
    {{- else -}}
        <!-- Print the whole Summary if endtoc special comment is not found. -->
        {{- .Summary |  printf "<p>%s</p>" | safeHTML -}}
    {{- end -}}
{{- end -}}

I changed this:

(html)
{{- $summary_raw | markdownify | truncate 300 -}}

to:

(html)
{{- $summary_raw | markdownify | plainify | truncate 300 -}}

This works as follows:

  • If #+DESCRIPTION: exitst, it will be used.
  • If ox-hugo has made a TOC (if class ox-hugo-toc exists). delte it, make “Summary”.

How to use summary_minus_toc.html

In a template containig “Summary”, use this code:

Code Snippet 3: How to use summary_minus_toc.html (html)
{{ partial "summary_minus_toc.html" . }}

Tags: Hugo ox-hugo

Related Articles:


We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it