improved children macro: add exclude

This commit is contained in:
Anya Tokugawa committed 2026-05-31 20:52:47 +09:00
1 parent 627466c935
commit d300354725
1 file changed
+66 -1
+66 -1
View File
@@ -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,