- commit
- 0f1da31
- parent
- c4b0a8a
- author
- ChloƩ Vulquin
- date
- 2024-03-22 08:42:58 +0000 UTC
cleanup
1 files changed,
+15,
-8
1@@ -80,8 +80,11 @@ func toBool(obj interface{}) (bool, error) {
2 }
3 }
4
5-// N = maxdepth - 1
6-// -1: disable, 0: enable (no max), 1: enable (only top-level ##)
7+// The toc frontmatter can take a boolean or an integer.
8+//
9+// A value of -1 or false means "do not generate a toc".
10+// A value of 0 or true means "generate a toc with no depth limit".
11+// A value of >0 means "generate a toc with a depth limit of $value past title".
12 func toToc(obj interface{}) (int, error) {
13 if obj == nil {
14 return -1, nil
15@@ -258,7 +261,10 @@ func ParseText(text string) (*ParsedText, error) {
16 return &parsed, fmt.Errorf("front-matter field (%s): %w", "toc", err)
17 }
18 if mtoc >= 0 {
19- AstToc(doc, btext, mtoc)
20+ err = AstToc(doc, btext, mtoc)
21+ if err != nil {
22+ return &parsed, fmt.Errorf("error generating toc: %w", err)
23+ }
24 }
25
26 description, err := toString(metaData["description"])
27@@ -414,25 +420,25 @@ func AstTitle(doc ast.Node, src []byte, clean bool) string {
28 return out
29 }
30
31-func AstToc(doc ast.Node, src []byte, mtoc int) {
32+func AstToc(doc ast.Node, src []byte, mtoc int) error {
33 var tree *toc.TOC
34 if mtoc >= 0 {
35 var err error
36 if mtoc > 0 {
37- tree, err = toc.Inspect(doc, src, toc.Compact(true), toc.MinDepth(2), toc.MaxDepth(mtoc + 1))
38+ tree, err = toc.Inspect(doc, src, toc.Compact(true), toc.MinDepth(2), toc.MaxDepth(mtoc+1))
39 } else {
40 tree, err = toc.Inspect(doc, src, toc.Compact(true), toc.MinDepth(2))
41 }
42 if err != nil {
43- return // TODO: further processing?
44+ return err
45 }
46 if tree == nil {
47- return
48+ return nil // no headings?
49 }
50 }
51 list := toc.RenderList(tree)
52 if list == nil {
53- return // no headings
54+ return nil // no headings
55 }
56
57 list.SetAttributeString("id", []byte("toc-list"))
58@@ -445,4 +451,5 @@ func AstToc(doc ast.Node, src []byte, mtoc int) {
59 // insert
60 doc.InsertBefore(doc, doc.FirstChild(), list)
61 doc.InsertBefore(doc, doc.FirstChild(), heading)
62+ return nil
63 }