#!/usr/bin/env bash
# Assemble the MkDocs docs directory (`_web/`) from the book Markdown sources.
# Only Markdown + images are copied; code, PDFs and LaTeX sources are left out
# so the generated site stays small. The original sources are never modified.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DEST="$ROOT/_web"
rm -rf "$DEST"
mkdir -p "$DEST"
# Site homepage (root index.md).
cp "$ROOT/index.md" "$DEST/index.md"
# The language editions, each with its images/ subfolder.
for lang in book book-en book-ta book-vi book-zhtw; do
mkdir -p "$DEST/$lang"
cp -R "$ROOT/$lang" "$DEST/"
done
# The companion experiment directories (chapterN/). Each chapter has a
# README.md (experiment index) plus one subfolder per experiment, also
# documented by its own README.md. These are exposed under /chapterN/ so
# readers can step from the chapter prose straight into runnable code.
for ch in chapter1 chapter2 chapter3 chapter4 chapter5 \
chapter6 chapter7 chapter8 chapter9 chapter10; do
if [ -d "$ROOT/$ch" ]; then
cp -R "$ROOT/$ch" "$DEST/"
fi
done
# Copy site-level assets (JS/CSS for the language switcher) that MkDocs
# resolves relative to docs_dir.
cp -R "$ROOT/extras" "$DEST/extras"
# Site-wide static assets — logo, favicon, social OG images. Referenced by
# mkdocs.yml as `assets/<file>` (relative to docs_dir).
if [ -d "$ROOT/assets" ]; then
mkdir -p "$DEST/assets"
cp -R "$ROOT/assets/." "$DEST/assets/"
fi
# Keep only Markdown and images; drop .tex/.py/.lua/.pdf/.sh etc.
find "$DEST" -type f \
! -name '*.md' \
! -name '*.svg' \
! -name '*.png' \
! -name '*.jpg' \
! -name '*.jpeg' \
! -name '*.js' \
! -name '*.css' \
-delete
# Drop bulk data files that some experiments bundle as their dataset but
# that don't belong in the reading site (hundreds of legal-doc markdown
# files would also slow the git-revision-date plugin to a crawl).
rm -rf \
"$DEST/chapter3/contextual-retrieval/laws" \
"$DEST/chapter3/agentic-rag/laws" \
2>/dev/null || true
# Rewrite the relative links used inside the experiment READMEs so they
# resolve correctly in the MkDocs site. Source files are NOT modified —
# only the copies under _web/.
#
# The README source uses GitHub-style relative paths that don't survive
# MkDocs rendering. Two patterns appear in chapter index pages
# (`chapterN/README.md`):
# ../book/chapter1.md (point at the chapter prose)
# ../README.md (point at the repo root / homepage)
#
# MkDocs renders pages as directory URLs (`chapter1/`), so the `.md`
# suffix must be stripped. Keep the paths RELATIVE (no leading slash) so
# they keep working under the site's sub-path
# (`https://bojieli.github.io/ai-agent-book/`).
find "$DEST/chapter"* -type f -name '*.md' -print0 \
| xargs -0 sed -i.bak \
-e 's|\.\./book/\([a-zA-Z0-9_-]*\)\.md|../book/\1/|g' \
-e 's|\.\./README\.md|../|g'
# macOS sed needs the backup suffix above; clean up the .bak files.
find "$DEST" -name '*.md.bak' -delete
echo "Assembled docs into $DEST"