diff options
| author | a73x <[email protected]> | 2024-09-29 10:09:46 +0100 |
|---|---|---|
| committer | a73x <[email protected]> | 2024-09-29 10:09:46 +0100 |
| commit | bddebada1aeee5eb87ff4e52a0b7bdf9c966f912 (patch) | |
| tree | f74b403957febe52faa261aad2ca80c0ff46be20 | |
| parent | 917043de43777fc1429e23ced7cf9d179cbcf61f (diff) | |
feat(web): made toc optional
| -rw-r--r-- | content/posts/001.md | 1 | ||||
| -rw-r--r-- | content/posts/002.md | 1 | ||||
| -rw-r--r-- | content/posts/003.md | 1 | ||||
| -rw-r--r-- | content/posts/004.md | 1 | ||||
| -rw-r--r-- | content/posts/005.md | 1 | ||||
| -rw-r--r-- | content/posts/006.md | 1 | ||||
| -rw-r--r-- | content/posts/007.md | 1 | ||||
| -rw-r--r-- | html/highlight.go | 14 | ||||
| -rw-r--r-- | html/html.go | 4 | ||||
| -rw-r--r-- | pages/pages.go | 8 |
10 files changed, 28 insertions, 5 deletions
diff --git a/content/posts/001.md b/content/posts/001.md index f7d520e..8aee591 100644 --- a/content/posts/001.md +++ b/content/posts/001.md @@ -1,6 +1,7 @@ --- title: "Go Benchmarking" tags: posts +toc: true --- The benchmark cycle: diff --git a/content/posts/002.md b/content/posts/002.md index 93ee423..8cb8b19 100644 --- a/content/posts/002.md +++ b/content/posts/002.md @@ -1,6 +1,7 @@ --- title: Go Project Layouts tags: posts +toc: true --- Do you lay awake at night and consider how to optimally layout your Go project? diff --git a/content/posts/003.md b/content/posts/003.md index 0f47968..1536c93 100644 --- a/content/posts/003.md +++ b/content/posts/003.md @@ -1,6 +1,7 @@ --- title: "Levels of Optimisation" tags: posts +toc: true --- This probably isn't strictly true, but it makes sense to me. diff --git a/content/posts/004.md b/content/posts/004.md index e5d28b4..dc25d77 100644 --- a/content/posts/004.md +++ b/content/posts/004.md @@ -1,6 +1,7 @@ --- title: "Writing HTTP Handlers" tags: posts +toc: true --- I'm sharing how I write handlers in Go. diff --git a/content/posts/005.md b/content/posts/005.md index 9f37c70..e2b3dfc 100644 --- a/content/posts/005.md +++ b/content/posts/005.md @@ -1,6 +1,7 @@ --- title: "Go's unique pkg" tags: posts +toc: true --- https://pkg.go.dev/unique diff --git a/content/posts/006.md b/content/posts/006.md index 30f25f2..d6fad1f 100644 --- a/content/posts/006.md +++ b/content/posts/006.md @@ -1,6 +1,7 @@ --- title: Kubernetes Intro tags: posts +toc: true --- My crash course to Kubernetes. diff --git a/content/posts/007.md b/content/posts/007.md index 34230b7..39d1f70 100644 --- a/content/posts/007.md +++ b/content/posts/007.md @@ -1,6 +1,7 @@ --- title: Building a Static Site with Hugo and Docker tags: posts +toc: true --- This will be a quick walkthrough of how to create a static site using Hugo, and use Nginx to serve it. diff --git a/html/highlight.go b/html/highlight.go index 087e043..d198ad0 100644 --- a/html/highlight.go +++ b/html/highlight.go @@ -52,7 +52,11 @@ func (h Highlighter) myRenderHook(w io.Writer, node ast.Node, entering bool) (as return ast.GoToNext, false } -func newRenderer() *mdhtml.Renderer { +type opts struct { +} + +func newRenderer(hasToc bool) *mdhtml.Renderer { + htmlFormatter := html.New(html.WithClasses(true), html.TabWidth(2)) styleName := "monokailight" @@ -62,8 +66,14 @@ func newRenderer() *mdhtml.Renderer { htmlFormatter: htmlFormatter, highlightStyle: highlightStyle, } + + flags := mdhtml.CommonFlags | mdhtml.FootnoteReturnLinks + if hasToc { + flags = flags | mdhtml.TOC + } + opts := mdhtml.RendererOptions{ - Flags: mdhtml.CommonFlags | mdhtml.TOC | mdhtml.FootnoteReturnLinks, + Flags: flags, RenderNodeHook: h.myRenderHook, } return mdhtml.NewRenderer(opts) diff --git a/html/html.go b/html/html.go index 6b8d071..40a6ad5 100644 --- a/html/html.go +++ b/html/html.go @@ -5,10 +5,10 @@ import ( "github.com/gomarkdown/markdown/parser" ) -func MDToHTML(md []byte) []byte { +func MDToHTML(md []byte, hasToc bool) []byte { extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock | parser.Footnotes p := parser.NewWithExtensions(extensions) - renderer := newRenderer() + renderer := newRenderer(hasToc) return markdown.ToHTML(md, p, renderer) } diff --git a/pages/pages.go b/pages/pages.go index 8adb9e1..5ca5cff 100644 --- a/pages/pages.go +++ b/pages/pages.go @@ -48,7 +48,13 @@ func renderTemplate(config GlobalState, content markdown.Content) (string, error return "", fmt.Errorf("failed to execute content template: %v", err) } - data.Body = string(html.MDToHTML(newContent.Bytes())) + hasToc := false + _, ok := content.Meta["toc"] + if ok { + hasToc = content.Meta["toc"].(bool) + } + + data.Body = string(html.MDToHTML(newContent.Bytes(), hasToc)) b := &bytes.Buffer{} if err := t.Execute(b, data); err != nil { |
