Skip to content

Commit 7f63996

Browse files
committed
feat(docs): implement dynamic branch based docs versioning
Signed-off-by: jaydeep869 <jaydeeppokhariya2106@gmail.com>
1 parent 698876d commit 7f63996

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

docs-website/build-versions.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Navigate to the docs-website directory
5+
cd "$(dirname "$0")"
6+
7+
echo "Building Docusaurus versions from docs/* branches..."
8+
9+
# Clean up any existing versioned generated folders
10+
rm -rf versioned_docs versioned_sidebars versions.json
11+
mkdir -p versioned_docs versioned_sidebars
12+
13+
# Initialize versions array
14+
VERSIONS=()
15+
16+
# Fetch docs branches (adjust 'origin' if your remote is named differently)
17+
git fetch origin '+refs/heads/docs/*:refs/remotes/origin/docs/*' || true
18+
19+
# Find all branches matching 'docs/*'
20+
BRANCHES=$(git branch -r | grep 'origin/docs/' | sed 's/^[[:space:]]*origin\///' || true)
21+
22+
for branch in $BRANCHES; do
23+
# Only treat docs/X.Y.Z branches as version snapshots.
24+
if ! [[ "$branch" =~ ^docs/[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
25+
echo "Skipping non-version docs branch: $branch"
26+
continue
27+
fi
28+
29+
# Extract version number (e.g., docs/0.7.0 -> 0.7.0)
30+
VERSION=${branch#docs/}
31+
echo "Processing version: $VERSION from branch: $branch"
32+
33+
# Create the target directory for this version's docs
34+
TARGET_DIR="versioned_docs/version-$VERSION"
35+
mkdir -p "$TARGET_DIR"
36+
37+
# Extract the 'docs/' folder from that specific branch
38+
# using git archive so we don't have to switch branches
39+
if ! (cd .. && git archive "$branch" docs/) | tar -x -C "$TARGET_DIR"; then
40+
echo "Warning: Could not extract docs/ from $branch. Skipping version $VERSION."
41+
rm -rf "$TARGET_DIR"
42+
continue
43+
fi
44+
45+
# Extract the sidebars file for this version.
46+
# Docusaurus expects this in versioned_sidebars/version-{version}-sidebars.js
47+
if ! git show "$branch:docs-website/sidebars.js" > "versioned_sidebars/version-${VERSION}-sidebars.js" 2>/dev/null; then
48+
echo "Warning: Could not extract sidebars.js from $branch. Skipping version $VERSION."
49+
rm -rf "$TARGET_DIR"
50+
rm -f "versioned_sidebars/version-${VERSION}-sidebars.js"
51+
continue
52+
fi
53+
54+
VERSIONS+=("$VERSION")
55+
56+
# Extract README.md and CONTRIBUTING.md if they are included in the docs config
57+
git show "$branch:README.md" > "$TARGET_DIR/README.md" || true
58+
git show "$branch:CONTRIBUTING.md" > "$TARGET_DIR/CONTRIBUTING.md" || true
59+
done
60+
61+
# Generate versions.json for Docusaurus to read
62+
if [ ${#VERSIONS[@]} -gt 0 ]; then
63+
# Write the versions array as a JSON list using jq or basic Node/Python string manipulation
64+
# Quick inline node script to write a valid JSON array
65+
node -e "const fs=require('fs'); fs.writeFileSync('versions.json', JSON.stringify(process.argv.slice(1)));" "${VERSIONS[@]}"
66+
echo "versions.json generated with: ${VERSIONS[*]}"
67+
else
68+
echo "[]" > versions.json
69+
echo "No docs/* branches found. Generating empty versions.json."
70+
fi

docs-website/docusaurus.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ const config = {
7070
src: 'img/logo.png',
7171
},
7272
items: [
73+
{
74+
type: 'docsVersionDropdown',
75+
position: 'right',
76+
},
7377
{
7478
type: "search",
7579
position: "right",

docs-website/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"docusaurus": "docusaurus",
77
"start": "docusaurus start",
8-
"build": "docusaurus build",
8+
"build": "./build-versions.sh && CI=false docusaurus build",
99
"swizzle": "docusaurus swizzle",
1010
"deploy": "docusaurus deploy",
1111
"clear": "docusaurus clear",

0 commit comments

Comments
 (0)