diff --git a/internal/server/macro_expand.go b/internal/server/macro_expand.go index 09eae95..9f6934c 100644 --- a/internal/server/macro_expand.go +++ b/internal/server/macro_expand.go @@ -7,7 +7,51 @@ import ( "strings" ) -var childrenMacroRe = regexp.MustCompile(`\{\{children(?::([^}]+))?\}\}`) +var childrenMacroRe = regexp.MustCompile(`\{\{children(?::([^}\s]+))?(?:\s+exclude=([^}]+))?\}\}`) + +func splitExcludePatterns( + raw string, +) []string { + + var out []string + + for _, p := range strings.Split(raw, ",") { + + p = strings.TrimSpace(p) + + if p == "" { + continue + } + + if !strings.HasPrefix(p, "/") { + p = "/" + p + } + + out = append(out, p) + } + + return out +} + +func isExcludedPath( + path string, + patterns []string, +) bool { + + for _, p := range patterns { + + if path == p || + strings.HasPrefix( + path, + p+"/", + ) { + + return true + } + } + + return false +} func (s *Server) expandMacros( body string, @@ -24,6 +68,8 @@ func (s *Server) expandMacros( targetPath := currentPath + excludeRaw := "" + if len(matches) >= 2 && strings.TrimSpace(matches[1]) != "" { @@ -33,10 +79,22 @@ func (s *Server) expandMacros( ) } + if len(matches) >= 3 { + excludeRaw = + strings.TrimSpace( + matches[2], + ) + } + if !strings.HasPrefix(targetPath, "/") { targetPath = "/" + targetPath } + excludes := + splitExcludePatterns( + excludeRaw, + ) + children, err := s.store.ListChildPaths( targetPath, @@ -56,6 +114,13 @@ func (s *Server) expandMacros( for _, child := range children { + if isExcludedPath( + child, + excludes, + ) { + continue + } + name := strings.TrimPrefix( child,