diff --git a/Makefile b/Makefile index fe50561246..aa357c1b40 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,10 @@ community-local: tmpdir environment npx antora --stacktrace --log-format=pretty --log-level=info \ kw-local-community-playbook.yml \ 2>&1 | tee -a tmp/build.log - cd build/site && ln -s kubewarden/latest latest + cd build/site && ln -sf kubewarden/latest latest + ./scripts/generate-redirects.sh \ + build/site \ + admission-controller/1.35/en @echo "" @echo "If your build was successful, you can preview the site with" @echo "'make preview'." @@ -28,7 +31,10 @@ community-remote: tmpdir environment npx antora --stacktrace --log-format=pretty --log-level=info \ kw-remote-community-playbook.yml \ 2>&1 | tee -a tmp/build.log - cd build/site && ln -s kubewarden/latest latest + cd build/site && ln -sf kubewarden/latest latest + ./scripts/generate-redirects.sh \ + build/site \ + admission-controller/1.35/en .PHONY: community-netlify-preview community-netlify-preview: tmpdir environment @@ -36,7 +42,10 @@ community-netlify-preview: tmpdir environment npx antora --attribute build-environment=netlify --stacktrace --log-format=pretty --log-level=info \ kw-local-community-playbook.yml \ 2>&1 | tee -a tmp/build.log - cd build/site && ln -s kubewarden/latest latest + cd build/site && ln -sf kubewarden/latest latest + ./scripts/generate-redirects.sh \ + build/site \ + admission-controller/1.35/en .PHONY: clean diff --git a/netlify.toml b/netlify.toml index 7d0614b35e..184b8976c0 100644 --- a/netlify.toml +++ b/netlify.toml @@ -5,14 +5,3 @@ command = "make community-netlify-preview" #[build.environment] # NODE_OPTIONS = "--max_old_space_size=4096" - -# Redirect old Docusaurus-style URLs (/) to the new Antora URLs -# (/admission-controller/1.35/en/). -# Netlify serves existing files directly, so this only fires for paths that -# don't have a real file in the build output (the old doc URLs). -# Netlify pretty URL feature automatically adds the .html on the new Antora -# URLs. -[[redirects]] -from = "/*" -to = "/admission-controller/1.35/en/:splat" -status = 301 diff --git a/scripts/generate-redirects.sh b/scripts/generate-redirects.sh new file mode 100755 index 0000000000..30a0bb0673 --- /dev/null +++ b/scripts/generate-redirects.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# generate-redirects.sh +# +# Generates static HTML meta-refresh redirect files for old flat Docusaurus- +# style URLs, pointing them at the equivalent pages in the new Antora structure. +# +# Instead of reading source .adoc files, this script derives redirect targets +# from the HTML files that Antora actually produced under ANTORA_PREFIX in the +# built site. This guarantees the redirect list matches the real build output, +# regardless of which branch or remote Antora pulled content from. +# +# Usage: ./scripts/generate-redirects.sh +# site-output-dir path to the built site root (e.g. build/site) +# antora-prefix path under site-output-dir where Antora pages live +# (e.g. admc/1.35/en) +# +# Example: +# ./scripts/generate-redirects.sh \ +# build/site \ +# admission-controller/1.35/en + +set -euo pipefail + +SITE_DIR="${1:?site-output-dir argument required}" +ANTORA_PREFIX="${2:?antora-prefix argument required}" + +if [ ! -d "$SITE_DIR" ]; then + echo "ERROR: site output directory not found: $SITE_DIR" >&2 + exit 1 +fi + +ANTORA_DIR="${SITE_DIR}/${ANTORA_PREFIX}" + +if [ ! -d "$ANTORA_DIR" ]; then + echo "ERROR: antora prefix directory not found: $ANTORA_DIR" >&2 + exit 1 +fi + +count=0 + +while IFS= read -r -d '' html; do + # Derive the page path relative to the antora prefix dir, without .html + rel="${html#"$ANTORA_DIR"/}" + rel="${rel%.html}" + + # Derive the resulting html page + antora_html_file="${SITE_DIR}/${rel}.html" + antora_index_file="${SITE_DIR}/${rel}/index.html" + + # The old flat URL path (what users had bookmarked under Docusaurus) + flat_dir="${SITE_DIR}/${rel}" + out_file="${flat_dir}/index.html" + new_url="/${ANTORA_PREFIX}/${rel}.html" + + # Skip only if Antora already produced a real page at this path. + # Otherwise, overwrite the redirect deterministically on each run. + if [ -e "$antora_html_file" ] || { [ -e "$antora_index_file" ] && [ "$antora_index_file" != "$out_file" ]; }; then + continue + fi + + # Overwrite unconditionally so version bumps to ANTORA_PREFIX always take effect + mkdir -p "$flat_dir" + cat >"$out_file" < + + + + + + Redirecting... + + +

This page has moved. Click here if you are not redirected.

+ + +HTML + + count=$((count + 1)) +done < <(find "$ANTORA_DIR" -name "*.html" -not -name "index.html" -print0) + +echo "Generated ${count} redirect file(s) under ${SITE_DIR}."