用途:把 Swift DocC 文档编译为静态网站、本地预览,并托管到 GitHub Pages 或静态服务器。主要能力:提供编译参数参考、CI/CD 流水线示例,以及 404/SPA 路由问题的排查说明。
编译方式有两种:Swift Package Manager 的 swift-docc-plugin,或直接调用 xcrun docc convert。静态托管关键参数为 --transform-for-static-hosting、--hosting-base-path <repo>、--output-path <dir>,可选 --disable-indexing 跳过搜索索引以加速 CI。
SwiftPM 编译示例: swift package --allow-writing-to-directory ./docs generate-documentation --target MyLibrary --disable-indexing --transform-for-static-hosting --hosting-base-path my-repo-name --output-path ./docs
直接使用 CLI 时先 swift package dump-symbol-graph,再 xcrun docc convert 并附加 --fallback-display-name、--fallback-bundle-identifier 等参数。
本地预览:swift package --disable-sandbox preview-documentation --target MyLibrary,默认地址 http://localhost:8080/documentation/mylibrary;也可用 python3 -m http.server 8000 --directory .build/docc-static 预览静态产物。
GitHub Pages 自动化:创建 .github/workflows/documentation.yml,授予 pages: write、id-token: write 权限,用 macos-15 生成静态文档并上传、部署。
base path 规则:项目页 https://<user>.github.io/<repo-name>/ 需传 --hosting-base-path <repo-name> 且不带斜杠;用户或组织根域名则省略该参数。
限制与注意:base path 配置错误会导致样式与脚本 404;SPA 直接访问深层链接需 fallback 路由,--transform-for-static-hosting 会自动生成;教程代码高亮可能受 Myers diff 括号偏移影响,可用 scripts/audit-docc-highlights.py 在 CI 中检查。其余平台部署细节资料暂未提供。