use hugo's toc

This commit is contained in:
zwbetz-gh 2021-09-12 14:24:53 -05:00
parent b87a99af1f
commit 87e1a8608f
12 changed files with 20 additions and 31 deletions

View File

@ -16,7 +16,7 @@ An accessibility-friendly Hugo theme, ported from the [original Cupper](https://
- [Favicons](#favicons) - [Favicons](#favicons)
- [Shortcodes](#shortcodes) - [Shortcodes](#shortcodes)
- [Syntax highlighting](#syntax-highlighting) - [Syntax highlighting](#syntax-highlighting)
- [Disable toc for a blog post](#disable-toc-for-a-blog-post) - [Enable Table of Contents for a Blog Post](#enable-table-of-contents-for-a-blog-post)
- [Localization](#localization) - [Localization](#localization)
- [Custom CSS and JS](#custom-css-and-js) - [Custom CSS and JS](#custom-css-and-js)
- [Default to Dark Theme](#default-to-dark-theme) - [Default to Dark Theme](#default-to-dark-theme)
@ -82,14 +82,14 @@ By default, only a few languages are supported. If you want to add more, follow
1. Download the JS file, then copy it to `static/js/prism.js` 1. Download the JS file, then copy it to `static/js/prism.js`
1. Download the CSS file, then copy it to `static/css/prism.css` 1. Download the CSS file, then copy it to `static/css/prism.css`
## Disable toc for a blog post ## Enable Table of Contents for a Blog Post
Blog posts that have two or more subheadings (`<h2>`s) automatically get a table of contents. To disable this set `toc` to `false`. For example: Set `toc` to `true`. For example:
``` ```
--- ---
title: "My page with a few headings" title: "My page with a few headings"
toc: false toc: true
--- ---
``` ```

View File

@ -431,8 +431,8 @@ caption {
border: 1px solid; border: 1px solid;
padding: 1.5rem; padding: 1.5rem;
} }
.toc h2 { .toc nav {
font-size: 1rem; margin-top: 1rem;
} }
.toc ol { .toc ol {
margin-left: 0.75rem; margin-left: 0.75rem;

View File

@ -53,7 +53,7 @@
{{ if not .Site.Params.hideHeaderLinks }} {{ if not .Site.Params.hideHeaderLinks }}
/* Add "link here" links to <h2> headings */ /* Add "link here" links to <h2> headings */
(function () { (function () {
var headings = document.querySelectorAll('main > h2'); var headings = document.querySelectorAll('h2, h3, h4, h5, h6');
Array.prototype.forEach.call(headings, function (heading) { Array.prototype.forEach.call(headings, function (heading) {
var id = heading.getAttribute('id'); var id = heading.getAttribute('id');

View File

@ -2,6 +2,7 @@
date: "2014-09-28" date: "2014-09-28"
tags: ["hugo", "theme", "command-line"] tags: ["hugo", "theme", "command-line"]
title: "Creating a New Theme" title: "Creating a New Theme"
toc: true
--- ---

View File

@ -2,6 +2,7 @@
title: "Cupper Shortcodes" title: "Cupper Shortcodes"
date: 2019-02-12T23:39:06-06:00 date: 2019-02-12T23:39:06-06:00
tags: [hugo, shortcodes] tags: [hugo, shortcodes]
toc: true
--- ---
## blockquote ## blockquote

View File

@ -2,12 +2,12 @@
title: "Cupper Typography" title: "Cupper Typography"
date: 2019-02-14T15:36:09-06:00 date: 2019-02-14T15:36:09-06:00
tags: [hugo, markdown] tags: [hugo, markdown]
toc: true
--- ---
## Headings ## Headings
``` ```
# Heading 1
## Heading 2 ## Heading 2
### Heading 3 ### Heading 3
#### Heading 4 #### Heading 4
@ -15,7 +15,6 @@ tags: [hugo, markdown]
###### Heading 6 ###### Heading 6
``` ```
# Heading 1
## Heading 2 ## Heading 2
### Heading 3 ### Heading 3
#### Heading 4 #### Heading 4

View File

@ -2,6 +2,7 @@
date: "2014-04-02" date: "2014-04-02"
tags: ["go", "templates", "hugo"] tags: ["go", "templates", "hugo"]
title: "(Hu)go Template Primer" title: "(Hu)go Template Primer"
toc: true
--- ---
Hugo uses the excellent [Go][] [html/template][gohtmltemplate] library for Hugo uses the excellent [Go][] [html/template][gohtmltemplate] library for
@ -235,7 +236,7 @@ Notice how once we have entered the loop the value of {{ . }} has changed. We
have defined a variable outside of the loop so we have access to it from within have defined a variable outside of the loop so we have access to it from within
the loop. the loop.
# Hugo Parameters ## Hugo Parameters
Hugo provides the option of passing values to the template language Hugo provides the option of passing values to the template language
through the site configuration (for sitewide values), or through the meta through the site configuration (for sitewide values), or through the meta

View File

@ -2,6 +2,7 @@
date: "2014-04-02" date: "2014-04-02"
tags: ["hugo", "git", "fun"] tags: ["hugo", "git", "fun"]
title: "Getting Started with Hugo" title: "Getting Started with Hugo"
toc: true
--- ---
## Step 1. Install Hugo ## Step 1. Install Hugo

View File

@ -10,6 +10,7 @@ tags:
- git - git
- templates - templates
title: Migrate to Hugo from Jekyll title: Migrate to Hugo from Jekyll
toc: true
--- ---
## Move static content to `static` ## Move static content to `static`

View File

@ -13,7 +13,7 @@
- id: dark_theme - id: dark_theme
translation: "dark theme:" translation: "dark theme:"
- id: table_of_contents - id: table_of_contents
translation: Table of contents translation: Table of Contents
- id: publish_date - id: publish_date
translation: "Publish date:" translation: "Publish date:"
- id: last_updated - id: last_updated

View File

@ -1,19 +1,6 @@
{{ $headings := findRE "<h2.*?>(.|\n])+?</h2>" .Content }} {{ if eq .Params.toc true }}
{{ if ge (len $headings) 2 }}
<nav class="toc" aria-labelledby="toc-heading"> <nav class="toc" aria-labelledby="toc-heading">
<h2 id="toc-heading">{{ T "table_of_contents" }}</h2> <strong id="toc-heading">{{ T "table_of_contents" }}</strong>
<ol> {{ .TableOfContents }}
{{ range $headings }}
<li>
{{ $id := findRE "id=\".*\"" . }}
{{ $id = index $id 0 }}
{{ $id = strings.TrimPrefix "id=\"" $id }}
{{ $id = strings.TrimSuffix "\"" $id }}
<a href="#{{ $id }}">
{{ . | htmlUnescape | plainify }}
</a>
</li>
{{ end }}
</ol>
</nav> </nav>
{{ end }} {{ end }}

View File

@ -36,9 +36,7 @@
</div> </div>
{{ end }} {{ end }}
{{ if ne .Params.toc false }} {{ partial "toc.html" . }}
{{ partial "toc" . }}
{{ end }}
{{ .Content }} {{ .Content }}
</main> </main>