yao/sui/core/utils.go
Max 93fb007280 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.
2024-06-14 16:20:52 +08:00

37 lines
922 B
Go

package core
import (
"bytes"
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
// NewDocument create a new document
func NewDocument(htmlContent []byte) (*goquery.Document, error) {
docNode, err := html.Parse(bytes.NewReader(htmlContent))
if err != nil {
return nil, err
}
return goquery.NewDocumentFromNode(docNode), nil
}
// NewDocumentString create a new document
func NewDocumentString(htmlContent string) (*goquery.Document, error) {
docNode, err := html.Parse(strings.NewReader(htmlContent))
if err != nil {
return nil, err
}
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
}