From ea7124db5230f79e80af1a1856dd6efdb6f2597f Mon Sep 17 00:00:00 2001 From: Ralph Ocdol Date: Mon, 31 Mar 2025 22:24:51 +0800 Subject: [PATCH] feat: custom-api's replaceAll with regex support --- internal/glance/widget-custom-api.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/glance/widget-custom-api.go b/internal/glance/widget-custom-api.go index c9db2df..e092fc3 100644 --- a/internal/glance/widget-custom-api.go +++ b/internal/glance/widget-custom-api.go @@ -468,8 +468,16 @@ var customAPITemplateFuncs = func() template.FuncMap { return strings.TrimSuffix(s, suffix) }, "trimSpace": strings.TrimSpace, - "replaceAll": func(old, new, s string) string { - return strings.ReplaceAll(s, old, new) + "replaceAll": func(oldOrPattern, new, s string) string { + if s == "" { + return "" + } + + regex := getCachedRegexp(oldOrPattern) + if regex == nil { + return strings.ReplaceAll(s, oldOrPattern, new) + } + return regex.ReplaceAllString(s, new) }, "findMatch": func(pattern, s string) string { if s == "" {