diff --git a/README.md b/README.md index 956ca99..7c3b3f2 100644 --- a/README.md +++ b/README.md @@ -52,10 +52,10 @@ 分为对列表页文章数据的修改的插件和对影响整个程序表现的插件 -| 列表页文章数据插件 | 整个程序表现的插件 | -|----------------------|----------------| -| passwordProject 密码保护 | enlighter 代码高亮 | -| digest 自动生成指定长度的摘要 | | +| 列表页文章数据插件 | 整个程序表现的插件 | +|----------------------|--------------------------------------| +| passwordProject 密码保护 | enlighter 代码高亮(需要在后台安装enlighterjs插件) | +| digest 自动生成指定长度的摘要 | | #### 其它 diff --git a/helper/tree/tree.go b/helper/tree/tree.go new file mode 100644 index 0000000..66c2ff7 --- /dev/null +++ b/helper/tree/tree.go @@ -0,0 +1,68 @@ +package tree + +import "github.com/fthvgb1/wp-go/helper/slice" + +type Node[T any, K comparable] struct { + Data T + Children *[]Node[T, K] + Parent K +} + +func (n *Node[T, K]) GetChildren() []T { + return slice.Map(*n.Children, func(t Node[T, K]) T { + return t.Data + }) +} + +func (n *Node[T, K]) ChildrenByOrder(fn func(T, T) bool) []T { + a := slice.Map(*n.Children, func(t Node[T, K]) T { + return t.Data + }) + slice.Sort(a, fn) + return a +} + +func (n *Node[T, K]) loop(fn func(T, int), deep int) { + for _, nn := range *n.Children { + fn(nn.Data, deep) + if len(*nn.Children) > 0 { + nn.loop(fn, deep+1) + } + } +} + +func (n *Node[T, K]) Loop(fn func(T, int)) { + n.loop(fn, 0) +} +func (n *Node[T, K]) orderByLoop(fn func(T, int), orderBy func(T, T) bool, deep int) { + slice.Sort(*n.Children, func(i, j Node[T, K]) bool { + return orderBy(i.Data, j.Data) + }) + for _, nn := range *n.Children { + fn(nn.Data, deep) + if len(*nn.Children) > 0 { + nn.orderByLoop(fn, orderBy, deep+1) + } + } +} + +func (n *Node[T, K]) OrderByLoop(fn func(T, int), orderBy func(T, T) bool) { + n.orderByLoop(fn, orderBy, 0) +} + +func Root[T any, K comparable](a []T, top K, fn func(T) (child, parent K)) *Node[T, K] { + m := make(map[K]*Node[T, K]) + m[top] = &Node[T, K]{Children: new([]Node[T, K])} + for _, t := range a { + c, p := fn(t) + node := Node[T, K]{Parent: p, Data: t, Children: new([]Node[T, K])} + m[c] = &node + parent, ok := m[p] + if !ok { + m[p] = &Node[T, K]{Children: new([]Node[T, K])} + parent = m[p] + } + *parent.Children = append(*parent.Children, node) + } + return m[top] +} diff --git a/internal/theme/wp/components/widget/category.go b/internal/theme/wp/components/widget/category.go index e57a06c..f71b75c 100644 --- a/internal/theme/wp/components/widget/category.go +++ b/internal/theme/wp/components/widget/category.go @@ -5,6 +5,7 @@ import ( "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/helper/tree" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" @@ -124,7 +125,9 @@ func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, ca currentCategory = h.Index.Param.Category } showCount := conf["count"].(int64) - for _, category := range categories { + fn := func(category models.TermsMy, deep int) { + lv := fmt.Sprintf("level-%d", deep+1) + sep := strings.Repeat(" ", deep*2) selected := "" if category.Name == currentCategory { selected = "selected" @@ -133,8 +136,19 @@ func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, ca if showCount != 0 { count = fmt.Sprintf("(%d)", category.Count) } - s.Sprintf(` -`, selected, category.Terms.TermId, category.Name, count) + s.Sprintf(` +`, lv, selected, category.Terms.TermId, sep, category.Name, count) + } + if conf["hierarchical"].(int64) == 0 { + for _, category := range categories { + fn(category, 0) + } + } else { + tree.Root(categories, 0, func(t models.TermsMy) (child, parent uint64) { + return t.TermTaxonomyId, t.Parent + }).Loop(func(category models.TermsMy, deep int) { + fn(category, deep) + }) } s.WriteString(" \n") } @@ -183,6 +197,7 @@ func parseDropdownCate(h *wp.Handle) (cateName string, r bool) { if i < 0 { return } + r = true cateName = cc.Name return }