@@ -27,13 +27,6 @@ import (
27
27
28
28
var byteMailto = []byte ("mailto:" )
29
29
30
- // Header holds the data about a header.
31
- type Header struct {
32
- Level int
33
- Text string
34
- ID string
35
- }
36
-
37
30
// ASTTransformer is a default transformer of the goldmark tree.
38
31
type ASTTransformer struct {}
39
32
@@ -42,12 +35,13 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
42
35
metaData := meta .GetItems (pc )
43
36
firstChild := node .FirstChild ()
44
37
createTOC := false
45
- toc := [] Header {}
38
+ ctx := pc . Get ( renderContextKey ).( * markup. RenderContext )
46
39
rc := & RenderConfig {
47
40
Meta : "table" ,
48
41
Icon : "table" ,
49
42
Lang : "" ,
50
43
}
44
+
51
45
if metaData != nil {
52
46
rc .ToRenderConfig (metaData )
53
47
@@ -56,7 +50,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
56
50
node .InsertBefore (node , firstChild , metaNode )
57
51
}
58
52
createTOC = rc .TOC
59
- toc = make ([]Header , 0 , 100 )
53
+ ctx . TableOfContents = make ([]markup. Header , 0 , 100 )
60
54
}
61
55
62
56
_ = ast .Walk (node , func (n ast.Node , entering bool ) (ast.WalkStatus , error ) {
@@ -66,23 +60,20 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
66
60
67
61
switch v := n .(type ) {
68
62
case * ast.Heading :
69
- if createTOC {
70
- text := n .Text (reader .Source ())
71
- header := Header {
72
- Text : util .BytesToReadOnlyString (text ),
73
- Level : v .Level ,
74
- }
75
- if id , found := v .AttributeString ("id" ); found {
76
- header .ID = util .BytesToReadOnlyString (id .([]byte ))
77
- }
78
- toc = append (toc , header )
79
- } else {
80
- for _ , attr := range v .Attributes () {
81
- if _ , ok := attr .Value .([]byte ); ! ok {
82
- v .SetAttribute (attr .Name , []byte (fmt .Sprintf ("%v" , attr .Value )))
83
- }
63
+ for _ , attr := range v .Attributes () {
64
+ if _ , ok := attr .Value .([]byte ); ! ok {
65
+ v .SetAttribute (attr .Name , []byte (fmt .Sprintf ("%v" , attr .Value )))
84
66
}
85
67
}
68
+ text := n .Text (reader .Source ())
69
+ header := markup.Header {
70
+ Text : util .BytesToReadOnlyString (text ),
71
+ Level : v .Level ,
72
+ }
73
+ if id , found := v .AttributeString ("id" ); found {
74
+ header .ID = util .BytesToReadOnlyString (id .([]byte ))
75
+ }
76
+ ctx .TableOfContents = append (ctx .TableOfContents , header )
86
77
case * ast.Image :
87
78
// Images need two things:
88
79
//
@@ -199,12 +190,12 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
199
190
return ast .WalkContinue , nil
200
191
})
201
192
202
- if createTOC && len (toc ) > 0 {
193
+ if createTOC && len (ctx . TableOfContents ) > 0 {
203
194
lang := rc .Lang
204
195
if len (lang ) == 0 {
205
196
lang = setting .Langs [0 ]
206
197
}
207
- tocNode := createTOCNode (toc , lang )
198
+ tocNode := createTOCNode (ctx . TableOfContents , lang )
208
199
if tocNode != nil {
209
200
node .InsertBefore (node , firstChild , tocNode )
210
201
}
0 commit comments