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.
This commit is contained in:
Max 2024-06-14 16:20:52 +08:00
parent 971246d090
commit 93fb007280
2 changed files with 11 additions and 1 deletions

View file

@ -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,

View file

@ -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
}