#!/usr/bin/env bash
# Adds to every built book page and writes robots.txt and sitemap.xml.
# mdBook's templates cannot turn a chapter's .md path into its .html URL, so this runs on the built
# tree (usage: seo.sh ). index.html and introduction.html are the same page: both point at /.
set -euo pipefail
dir="${1:?usage: seo.sh }"
base="https://docs.solidcraft.io"
cd "$dir"
urls=()
while IFS= read -r f; do
f="${f#./}"
case "$f" in
404.html|toc.html) # not content pages: keep them out of the index
sed -i 's|||' "$f"; continue ;;
esac
grep -q 'rel="canonical"' "$f" && continue # mdBook's redirect stubs already name their target
case "$f" in
index.html|introduction.html) path="" ;;
*/index.html) path="${f%index.html}" ;;
*) path="$f" ;;
esac
url="$base/$path"
sed -i "s|||" "$f"
[ "$f" = print.html ] || [ "$f" = introduction.html ] || urls+=("$url")
done < <(find . -name '*.html' | sort)
{
echo ''
echo ''
for u in "${urls[@]}"; do echo " $u"; done
echo ''
} > sitemap.xml
# The release candidate and edge web apps can break: keep them out of search results.
printf 'User-agent: *\nAllow: /\nDisallow: /app-rc/\nDisallow: /app-edge/\n\nSitemap: %s/sitemap.xml\n' "$base" > robots.txt