-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchor.go
70 lines (57 loc) · 1.14 KB
/
anchor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package md2html
import "fmt"
type AnchorNode struct {
Id string
}
func NewAnchor(id string) *AnchorNode {
a := &AnchorNode{Id: id}
return a
}
func (a *AnchorNode) toString() string {
return fmt.Sprintf("<a id=\"%v\"></a>", a.Id)
}
func (a *AnchorNode) parse(s string) bool {
return false
}
/*
找其anchorn上面小于它的节点做为其父节点
1
2
3
2
1
1
3
2
1
*/
type HeadLevel struct {
Id string `json:"id"`
Name string `json:"name"`
Children []*HeadLevel `json:"children"`
level int
}
func NewHeadLevel(id, name string, l int) *HeadLevel {
t := &HeadLevel{Id: id, Name: name, level: l}
t.Children = make([]*HeadLevel, 0)
return t
}
func (l *HeadLevel) addChild(c *HeadLevel) {
l.Children = append(l.Children, c)
}
func (c *Content) parseHeadLevel() *HeadLevel {
for i := 1; i < len(c.alllevel); i += 1 {
item := c.alllevel[i]
p := c.findParent(i-1, item.level)
p.addChild(item)
}
return c.alllevel[0]
}
func (c *Content) findParent(lastindex int, myLevel int) *HeadLevel {
for i := lastindex; i >= 0; i -= 1 {
if c.alllevel[i].level < myLevel {
return c.alllevel[i]
}
}
return c.alllevel[0]
}