- commit
- 5d6d483
- parent
- ee83c2b
- author
- Eric Bower
- date
- 2024-03-21 16:11:57 +0000 UTC
feat(prose): toc
4 files changed,
+50,
-10
M
go.mod
+1,
-0
1@@ -38,6 +38,7 @@ require (
2 github.com/yuin/goldmark-meta v1.1.0
3 go.abhg.dev/goldmark/anchor v0.1.1
4 go.abhg.dev/goldmark/hashtag v0.3.1
5+ go.abhg.dev/goldmark/toc v0.10.0
6 golang.org/x/crypto v0.18.0
7 gopkg.in/yaml.v2 v2.4.0
8 )
M
go.sum
+4,
-0
1@@ -267,6 +267,8 @@ go.abhg.dev/goldmark/anchor v0.1.1 h1:NUH3hAzhfeymRqZKOkSoFReZlEAmfXBZlbXEzpD2Qg
2 go.abhg.dev/goldmark/anchor v0.1.1/go.mod h1:zYKiaHXTdugwVJRZqInVdmNGQRM3ZRJ6AGBC7xP7its=
3 go.abhg.dev/goldmark/hashtag v0.3.1 h1:k0FQwEtVQ1SstIRR2fqDJ4VNYUS0AXLp869V0qHOZMg=
4 go.abhg.dev/goldmark/hashtag v0.3.1/go.mod h1:rXtvxXPL7auhPMGRdG02UrXn/9LMm6PNdP5HO64zbVU=
5+go.abhg.dev/goldmark/toc v0.10.0 h1:de3LrIimwtGhBMKh7aEl1c6n4XWwOdukIO5wOAMYZzg=
6+go.abhg.dev/goldmark/toc v0.10.0/go.mod h1:OpH0qqRP9v/eosCV28ZeqGI78jZ8rri3C7Jh8fzEo2M=
7 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8 golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
9 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
10@@ -361,3 +363,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
11 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
13 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14+pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
15+pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
+4,
-0
1@@ -59,6 +59,10 @@ table {
2 height: auto;
3 }
4
5+#toc {
6+ margin: 0;
7+}
8+
9 .thumbnail-link {
10 z-index: 1;
11 }
1@@ -19,6 +19,7 @@ import (
2 gtext "github.com/yuin/goldmark/text"
3 "go.abhg.dev/goldmark/anchor"
4 "go.abhg.dev/goldmark/hashtag"
5+ "go.abhg.dev/goldmark/toc"
6 yaml "gopkg.in/yaml.v2"
7 )
8
9@@ -183,17 +184,20 @@ func ParseText(text string) (*ParsedText, error) {
10 html.WithClasses(true),
11 ),
12 )
13+ extenders := []goldmark.Extender{
14+ extension.GFM,
15+ extension.Footnote,
16+ meta.Meta,
17+ &hashtag.Extender{},
18+ hili,
19+ &anchor.Extender{
20+ Position: anchor.Before,
21+ Texter: anchor.Text("#"),
22+ },
23+ }
24 md := goldmark.New(
25 goldmark.WithExtensions(
26- extension.GFM,
27- extension.Footnote,
28- meta.Meta,
29- &hashtag.Extender{},
30- hili,
31- &anchor.Extender{
32- Position: anchor.Before,
33- Texter: anchor.Text("#"),
34- },
35+ extenders...,
36 ),
37 goldmark.WithParserOptions(
38 parser.WithAutoHeadingID(),
39@@ -203,12 +207,39 @@ func ParseText(text string) (*ParsedText, error) {
40 ),
41 )
42 context := parser.NewContext()
43-
44 // we do the Parse/Render steps manually to get a chance to examine the AST
45 btext := []byte(text)
46 doc := md.Parser().Parse(gtext.NewReader(btext), parser.WithContext(context))
47 metaData := meta.Get(context)
48
49+ showToc, err := toBool(metaData["toc"])
50+ if err != nil {
51+ return &parsed, fmt.Errorf("front-matter field (%s): %w", "toc", err)
52+ }
53+ // we need to add an extension after parsing frontmatter
54+ if showToc {
55+ extenders = append(extenders, &toc.Extender{
56+ Title: "toc",
57+ TitleDepth: 2,
58+ Compact: true,
59+ TitleID: "toc",
60+ ListID: "toc-list",
61+ })
62+ md = goldmark.New(
63+ goldmark.WithExtensions(
64+ extenders...,
65+ ),
66+ goldmark.WithParserOptions(
67+ parser.WithAutoHeadingID(),
68+ ),
69+ goldmark.WithRendererOptions(
70+ ghtml.WithUnsafe(),
71+ ),
72+ )
73+ context := parser.NewContext()
74+ doc = md.Parser().Parse(gtext.NewReader(btext), parser.WithContext(context))
75+ }
76+
77 // title:
78 // 1. if specified in frontmatter, use that
79 title, err := toString(metaData["title"])