#!/usr/bin/env bash # export-static.sh —— 导出可离线浏览的 VitePress 静态包 # # 用法: # bash scripts/export-static.sh # 仅构建并修复路径 # bash scripts/export-static.sh --pack # 额外打包成 dist.zip # # 产物路径:docs/.vitepress/dist/ (附 start-server.command / start-server.bat) # # 原理: # 1) 以 base=./ 构建,让 JS/CSS 等资源引用指向相对路径。 # 2) VitePress 对 Markdown 里的 /media/xxx 不会按 HTML 所在层级自动调相对 # 路径,所以这里手工把 guide/*.html 里的 /media/ 改成 ../media/,根目录 # 的 *.html 里的 /media/ 改成 ./media/。 # 3) 放入 start-server.command / start-server.bat,用 Python/Node 起本地 # HTTP 服务(解决浏览器对 file:// 下 ES Module 的 CORS 限制)。 # set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" DIST="$ROOT/docs/.vitepress/dist" cd "$ROOT" echo "==> 1) 构建 VitePress(base=./)" VITEPRESS_BASE=./ pnpm docs:build echo "==> 2) 修复 Markdown 绝对图片路径" # guide/*.html 需要 ../media/,根目录 *.html 需要 ./media/ fix_media_paths() { local file="$1" prefix="$2" tmp tmp="${file}.tmp" sed \ -e "s|src=\"/media/|src=\"${prefix}media/|g" \ -e "s|href=\"/media/|href=\"${prefix}media/|g" \ "$file" > "$tmp" mv "$tmp" "$file" } if [ -d "$DIST/guide" ]; then for f in "$DIST"/guide/*.html; do [ -e "$f" ] || continue fix_media_paths "$f" "../" done fi for f in "$DIST"/*.html; do [ -e "$f" ] || continue fix_media_paths "$f" "./" done # 校验是否还有残留 if grep -RIn 'src="/media\|href="/media' "$DIST" >/dev/null 2>&1; then echo "⚠️ 仍有绝对 /media/ 路径残留,建议检查" grep -RIn 'src="/media\|href="/media' "$DIST" | head else echo "✅ /media 路径全部转为相对" fi echo "==> 3) 写入本地 HTTP 启动脚本" cat > "$DIST/start-server.command" <<'EOF' #!/bin/bash # Digi Agent Builder 3.0 指导书 - 本地静态服务启动器(macOS / Linux) cd "$(dirname "$0")" PORT=8000 if command -v python3 &>/dev/null; then open "http://localhost:${PORT}/" 2>/dev/null || true python3 -m http.server "${PORT}" elif command -v python &>/dev/null; then open "http://localhost:${PORT}/" 2>/dev/null || true python -m SimpleHTTPServer "${PORT}" elif command -v node &>/dev/null; then open "http://localhost:${PORT}/" 2>/dev/null || true npx --yes serve -l "${PORT}" . else echo "未检测到 Python 或 Node,请先安装 Python 3。" read -n 1 -s -r -p "按任意键退出..." fi EOF chmod +x "$DIST/start-server.command" cat > "$DIST/start-server.bat" <<'EOF' @echo off REM Digi Agent Builder 3.0 指导书 - 本地静态服务启动器(Windows) cd /d "%~dp0" set PORT=8000 where python3 >nul 2>&1 && (start "" "http://localhost:%PORT%/" & python3 -m http.server %PORT% & goto :eof) where python >nul 2>&1 && (start "" "http://localhost:%PORT%/" & python -m SimpleHTTPServer %PORT% & goto :eof) where node >nul 2>&1 && (start "" "http://localhost:%PORT%/" & npx --yes serve -l %PORT% . & goto :eof) echo 未检测到 Python 或 Node,请先安装 Python 3。 pause EOF echo "" echo "✅ 静态导出完成:$DIST" echo " macOS / Linux:双击 start-server.command" echo " Windows:双击 start-server.bat" if [[ "${1:-}" == "--pack" ]]; then ZIP="$ROOT/dist-export.zip" echo "" echo "==> 4) 打包 dist-export.zip" rm -f "$ZIP" if command -v zip >/dev/null 2>&1; then (cd "$DIST" && zip -rq "$ZIP" . -x '*.DS_Store') else # 兼容未安装 zip 命令的系统(macOS/Linux 均自带 Python 3) (cd "$DIST" && python3 -m zipfile -c "$ZIP" .) fi echo "✅ 打包完成:$ZIP ($(du -sh "$ZIP" | awk '{print $1}'))" fi