Skip to content

Commit 6c41054

Browse files
authored
feat: add sitemap and robots.txt (#3)
Signed-off-by: Ashing Zheng <axingfly@gmail.com>
1 parent 99eff03 commit 6c41054

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/bin
3030
/testdata/**legacy**
3131
book/functions/litgo
3232
book/functions/markerdocs
33+
book/functions/sitemap
3334

3435
## Skip testdata files that generate by tests using TestContext
3536
**/e2e-*/**

book/generate-sitemap.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Configuration
6+
BASE_URL="${SITE_URL:-https://kubebuilder-zh.vibecodinghub.org/}"
7+
BOOK_DIR="$(dirname "$0")/book"
8+
SITEMAP_TOOL="$(dirname "$0")/utils/sitemap/main.go"
9+
10+
# Build the sitemap generator
11+
echo "Building sitemap generator..."
12+
cd "$(dirname "$0")/utils/sitemap"
13+
go build -o ../../bin/sitemap-gen main.go
14+
cd - > /dev/null
15+
16+
# Generate sitemap.xml
17+
echo "Generating sitemap.xml..."
18+
./bin/sitemap-gen "$BOOK_DIR" "$BASE_URL" > "$BOOK_DIR/sitemap.xml"
19+
20+
echo "Sitemap generated at $BOOK_DIR/sitemap.xml"

book/install-and-build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,11 @@ gobin=${GOBIN:-$(go env GOPATH)/bin} # GOBIN won't always be set :-/
9090
export PATH=${gobin}:$PATH
9191
verb=${1:-build}
9292
/tmp/mdbook ${verb}
93+
94+
# Generate sitemap after building
95+
if [[ ${verb} == "build" ]]; then
96+
echo "Generating sitemap.xml..."
97+
./generate-sitemap.sh
98+
echo "create robots.txt"
99+
cp ./static/robots.txt ./book/robots.txt
100+
fi

book/static/robots.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
User-agent: *
2+
Allow: /
3+
4+
# Sitemap
5+
Sitemap: https://kubebuilder-zh.vibecodinghub.org/sitemap.xml
6+
7+
# Allow all crawlers to access the documentation
8+
# This is a public documentation site for Kubebuilder Chinese translation

book/utils/sitemap/main.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package main
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
// XML structures for sitemap
12+
type URLSet struct {
13+
XMLName xml.Name `xml:"urlset"`
14+
Xmlns string `xml:"xmlns,attr"`
15+
URLs []URL `xml:"url"`
16+
}
17+
18+
type URL struct {
19+
Loc string `xml:"loc"`
20+
LastMod string `xml:"lastmod,omitempty"`
21+
ChangeFreq string `xml:"changefreq,omitempty"`
22+
Priority string `xml:"priority,omitempty"`
23+
}
24+
25+
func main() {
26+
if len(os.Args) < 3 {
27+
fmt.Fprintf(os.Stderr, "Usage: %s <book-dir> <base-url>\n", os.Args[0])
28+
os.Exit(1)
29+
}
30+
31+
bookDir := os.Args[1]
32+
baseURL := strings.TrimSuffix(os.Args[2], "/")
33+
34+
urlset := URLSet{
35+
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
36+
URLs: []URL{},
37+
}
38+
39+
// Walk through the book directory to find HTML files
40+
err := filepath.Walk(bookDir, func(path string, info os.FileInfo, err error) error {
41+
if err != nil {
42+
return err
43+
}
44+
45+
// Skip non-HTML files
46+
if !strings.HasSuffix(path, ".html") {
47+
return nil
48+
}
49+
50+
// Skip print.html if it exists
51+
if strings.HasSuffix(path, "print.html") {
52+
return nil
53+
}
54+
55+
// Get relative path from book directory
56+
relPath, err := filepath.Rel(bookDir, path)
57+
if err != nil {
58+
return err
59+
}
60+
61+
// Convert Windows paths to URL paths
62+
urlPath := filepath.ToSlash(relPath)
63+
64+
// Create full URL
65+
fullURL := baseURL + "/" + urlPath
66+
67+
// Get file modification time
68+
lastMod := info.ModTime().Format("2006-01-02T15:04:05-07:00")
69+
70+
// Determine priority and change frequency
71+
priority := "0.5"
72+
changeFreq := "weekly"
73+
74+
// Higher priority for important pages
75+
if urlPath == "index.html" {
76+
priority = "1.0"
77+
changeFreq = "daily"
78+
} else if strings.Contains(urlPath, "quick-start") ||
79+
strings.Contains(urlPath, "getting-started") ||
80+
strings.Contains(urlPath, "introduction") {
81+
priority = "0.8"
82+
changeFreq = "weekly"
83+
} else if strings.Contains(urlPath, "tutorial") {
84+
priority = "0.7"
85+
}
86+
87+
// Add URL to sitemap
88+
urlset.URLs = append(urlset.URLs, URL{
89+
Loc: fullURL,
90+
LastMod: lastMod,
91+
ChangeFreq: changeFreq,
92+
Priority: priority,
93+
})
94+
95+
return nil
96+
})
97+
98+
if err != nil {
99+
fmt.Fprintf(os.Stderr, "Error walking directory: %v\n", err)
100+
os.Exit(1)
101+
}
102+
103+
// Generate XML
104+
output, err := xml.MarshalIndent(urlset, "", " ")
105+
if err != nil {
106+
fmt.Fprintf(os.Stderr, "Error generating XML: %v\n", err)
107+
os.Exit(1)
108+
}
109+
110+
// Write XML header and content
111+
fmt.Printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n%s\n", output)
112+
}

0 commit comments

Comments
 (0)