Merge pull request #622 from trheyi/main

refactor: Update Namespace function to handle special characters in name
This commit is contained in:
Max 2024-06-14 17:42:36 +08:00 committed by GitHub
commit cc3a9d9140
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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
}