Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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-*/**
20 changes: 20 additions & 0 deletions book/generate-sitemap.sh
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions book/install-and-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions book/static/robots.txt
Original file line number Diff line number Diff line change
@@ -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
112 changes: 112 additions & 0 deletions book/utils/sitemap/main.go
Original file line number Diff line number Diff line change
@@ -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 <book-dir> <base-url>\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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n%s\n", output)
}
Loading