diff --git a/.gitignore b/.gitignore index 8a36ddd..2ea25aa 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/bin /testdata/**legacy** book/functions/litgo book/functions/markerdocs +book/functions/sitemap ## Skip testdata files that generate by tests using TestContext **/e2e-*/** diff --git a/book/generate-sitemap.sh b/book/generate-sitemap.sh new file mode 100755 index 0000000..76eb433 --- /dev/null +++ b/book/generate-sitemap.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -e + +# Configuration +BASE_URL="${SITE_URL:-https://kubebuilder-zh.vibecodinghub.org/}" +BOOK_DIR="$(dirname "$0")/book" +SITEMAP_TOOL="$(dirname "$0")/utils/sitemap/main.go" + +# Build the sitemap generator +echo "Building sitemap generator..." +cd "$(dirname "$0")/utils/sitemap" +go build -o ../../bin/sitemap-gen main.go +cd - > /dev/null + +# Generate sitemap.xml +echo "Generating sitemap.xml..." +./bin/sitemap-gen "$BOOK_DIR" "$BASE_URL" > "$BOOK_DIR/sitemap.xml" + +echo "Sitemap generated at $BOOK_DIR/sitemap.xml" diff --git a/book/install-and-build.sh b/book/install-and-build.sh index f958904..c577a4a 100755 --- a/book/install-and-build.sh +++ b/book/install-and-build.sh @@ -90,3 +90,11 @@ gobin=${GOBIN:-$(go env GOPATH)/bin} # GOBIN won't always be set :-/ export PATH=${gobin}:$PATH verb=${1:-build} /tmp/mdbook ${verb} + +# Generate sitemap after building +if [[ ${verb} == "build" ]]; then + echo "Generating sitemap.xml..." + ./generate-sitemap.sh + echo "create robots.txt" + cp ./static/robots.txt ./book/robots.txt +fi diff --git a/book/static/robots.txt b/book/static/robots.txt new file mode 100644 index 0000000..0efb43b --- /dev/null +++ b/book/static/robots.txt @@ -0,0 +1,8 @@ +User-agent: * +Allow: / + +# Sitemap +Sitemap: https://kubebuilder-zh.vibecodinghub.org/sitemap.xml + +# Allow all crawlers to access the documentation +# This is a public documentation site for Kubebuilder Chinese translation diff --git a/book/utils/sitemap/main.go b/book/utils/sitemap/main.go new file mode 100644 index 0000000..f7afdc4 --- /dev/null +++ b/book/utils/sitemap/main.go @@ -0,0 +1,112 @@ +package main + +import ( + "encoding/xml" + "fmt" + "os" + "path/filepath" + "strings" +) + +// XML structures for sitemap +type URLSet struct { + XMLName xml.Name `xml:"urlset"` + Xmlns string `xml:"xmlns,attr"` + URLs []URL `xml:"url"` +} + +type URL struct { + Loc string `xml:"loc"` + LastMod string `xml:"lastmod,omitempty"` + ChangeFreq string `xml:"changefreq,omitempty"` + Priority string `xml:"priority,omitempty"` +} + +func main() { + if len(os.Args) < 3 { + fmt.Fprintf(os.Stderr, "Usage: %s \n", os.Args[0]) + os.Exit(1) + } + + bookDir := os.Args[1] + baseURL := strings.TrimSuffix(os.Args[2], "/") + + urlset := URLSet{ + Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9", + URLs: []URL{}, + } + + // Walk through the book directory to find HTML files + err := filepath.Walk(bookDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + // Skip non-HTML files + if !strings.HasSuffix(path, ".html") { + return nil + } + + // Skip print.html if it exists + if strings.HasSuffix(path, "print.html") { + return nil + } + + // Get relative path from book directory + relPath, err := filepath.Rel(bookDir, path) + if err != nil { + return err + } + + // Convert Windows paths to URL paths + urlPath := filepath.ToSlash(relPath) + + // Create full URL + fullURL := baseURL + "/" + urlPath + + // Get file modification time + lastMod := info.ModTime().Format("2006-01-02T15:04:05-07:00") + + // Determine priority and change frequency + priority := "0.5" + changeFreq := "weekly" + + // Higher priority for important pages + if urlPath == "index.html" { + priority = "1.0" + changeFreq = "daily" + } else if strings.Contains(urlPath, "quick-start") || + strings.Contains(urlPath, "getting-started") || + strings.Contains(urlPath, "introduction") { + priority = "0.8" + changeFreq = "weekly" + } else if strings.Contains(urlPath, "tutorial") { + priority = "0.7" + } + + // Add URL to sitemap + urlset.URLs = append(urlset.URLs, URL{ + Loc: fullURL, + LastMod: lastMod, + ChangeFreq: changeFreq, + Priority: priority, + }) + + return nil + }) + + if err != nil { + fmt.Fprintf(os.Stderr, "Error walking directory: %v\n", err) + os.Exit(1) + } + + // Generate XML + output, err := xml.MarshalIndent(urlset, "", " ") + if err != nil { + fmt.Fprintf(os.Stderr, "Error generating XML: %v\n", err) + os.Exit(1) + } + + // Write XML header and content + fmt.Printf("\n%s\n", output) +}