mark2web/pkg/mark2web/navigation.go
Sebastian Frank e4c72b348f
All checks were successful
continuous-integration/drone/push Build is passing
🐛 fix(navigation.go): fix the condition for determining if a navigation element is active
The /tmp/ directory is now added to the .gitignore file to exclude it from version control. In the navigation.go file, the condition for determining if a navigation element is active has been fixed to correctly handle cases where the activeNav string matches the elPath string. The tmp/main file has been deleted.
2023-11-09 17:49:03 +00:00

107 lines
2.6 KiB
Go

package mark2web
import (
"path"
"regexp"
"strings"
"gitbase.de/apairon/mark2web/pkg/helper"
"gitbase.de/apairon/mark2web/pkg/logger"
)
// NavElement is one element with ist attributes and subs
type NavElement struct {
Navname string
GoTo string
Active bool
ColMap helper.MapString
Data interface{}
This ThisPathConfig
SubMap *map[string]*NavElement
SubSlice *[]*NavElement
}
// buildNavigation builds the navigation trees for use in templates
func (node *TreeNode) buildNavigation(curNavMap *map[string]*NavElement, curNavSlice *[]*NavElement, navActive *[]*NavElement) {
buildNavigationRecursive(node.root, curNavMap, curNavSlice, navActive, node.CurrentNavPath(), node.BackToRootPath())
}
func buildNavigationRecursive(tree *TreeNode, curNavMap *map[string]*NavElement, curNavSlice *[]*NavElement, navActive *[]*NavElement, activeNav string, backToRoot string) {
for _, el := range tree.Sub {
if el.Hidden {
continue // ignore hidden nav points from collections
}
var ignNav *string
if p := el.Config.Path; p != nil {
ignNav = p.IgnoreForNav
}
if ignNav != nil && *ignNav != "" {
regex, err := regexp.Compile(*ignNav)
logger.Eexit(err, "could not compile IngoreForNav regexp '%s' in '%s'", *ignNav, el.InputPath)
if regex.MatchString(path.Base(el.InputPath)) {
logger.D("ignoring input directory '%s' in navigation", el.InputPath)
continue
}
}
elPath := strings.TrimPrefix(el.OutputPath, Config.Directories.Output+"/")
subMap := make(map[string]*NavElement)
subSlice := make([]*NavElement, 0)
navEl := NavElement{
Active: strings.HasPrefix(activeNav, elPath+"/") || activeNav == elPath,
Data: el.Config.Data,
ColMap: el.ColMap,
SubMap: &subMap,
SubSlice: &subSlice,
}
navEl.This = el.Config.This
if navEl.Active {
// add to navActive level navigation
currentLevel := strings.Count(activeNav, "/")
if len(*navActive) <= currentLevel {
// not registered
*navActive = append(*navActive, &navEl)
}
}
n := el.Config.This.Navname
if n != nil {
navEl.Navname = *n
}
g := el.Config.This.GoTo
if g != nil {
if strings.HasPrefix(*g, "/") {
// abslute
navEl.GoTo = *g
} else {
// relative
navEl.GoTo = elPath + "/" + *g
}
} else {
navEl.GoTo = elPath + "/"
}
if activeNav != "" && activeNav != "/" {
// calculate relative path
navEl.GoTo = backToRoot + navEl.GoTo
navEl.GoTo = path.Clean(navEl.GoTo)
}
(*curNavMap)[path.Base(el.OutputPath)] = &navEl
if curNavSlice != nil {
*curNavSlice = append(*curNavSlice, &navEl)
}
buildNavigationRecursive(el, &subMap, &subSlice, navActive, activeNav, backToRoot)
}
}