From 93fb007280acf48eda107e60ca41801fda5dc7b4 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 14 Jun 2024 16:20:52 +0800 Subject: [PATCH] refactor: Update Namespace function to handle special characters in name The Namespace function in utils.go has been updated to handle special characters in the name parameter. This ensures that the generated namespace is valid and avoids any potential issues with naming conflicts. The function now replaces forward slashes, square brackets, and hyphens with underscores before generating the namespace. This change improves the reliability and robustness of the code. --- sui/core/build.go | 2 +- sui/core/utils.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sui/core/build.go b/sui/core/build.go index 0f865738..3e6cf189 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -208,7 +208,7 @@ func (page *Page) parse(doc *goquery.Document, option *BuildOption, warnings []s } p := ipage.Get() - namespace := fmt.Sprintf("__page_%s_%d", strings.ReplaceAll(name, "/", "_"), idx) + namespace := Namespace(name, idx) html, style, script, warns, err := p.BuildForImport(&BuildOption{ SSR: option.SSR, AssetRoot: option.AssetRoot, diff --git a/sui/core/utils.go b/sui/core/utils.go index 13d2be39..32fe9a69 100644 --- a/sui/core/utils.go +++ b/sui/core/utils.go @@ -2,6 +2,7 @@ package core import ( "bytes" + "fmt" "strings" "github.com/PuerkitoBio/goquery" @@ -25,3 +26,12 @@ func NewDocumentString(htmlContent string) (*goquery.Document, error) { } return goquery.NewDocumentFromNode(docNode), nil } + +// Namespace convert the name to namespace +func Namespace(name string, idx int) string { + name = strings.ReplaceAll(name, "/", "_") + name = strings.ReplaceAll(name, "[", "_") + name = strings.ReplaceAll(name, "]", "_") + namespace := fmt.Sprintf("__page_%s_%d", name, idx) + return namespace +}