ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Comprehensive Rust 删除课程页面后如何在 book.toml 添加 redirect 保证旧链接可访问

Comprehensive Rust 删除课程页面后如何在 book.toml 添加 redirect 保证旧链接可访问 Comprehensive Rust 删除课程页面后如何在 book.toml 添加 redirect 保证旧链接可访问【免费下载链接】comprehensive-rustThis is the Rust course used by the Android team at Google. It provides you the material to quickly teach Rust.项目地址: https://gitcode.com/GitHub_Trending/co/comprehensive-rustComprehensive Rust 是一本用 mdBook 构建的 Rust 课程课程页面会随课程结构调整而移动或删除。删除src/下的.md页面后指向旧页面的链接会失效。项目通过book.toml中[output.html.redirect]段的跳转表redirect map解决这个问题为每个被删除的页面添加一条旧路径 → 新路径映射让访问旧地址的读者被重定向到新位置。本文说明如何添加这样的映射以及项目自带的 CI 检查是如何验证这些映射的。准备条件已克隆仓库本地有 git 历史CI 检查通过git diff对比origin/main本地验证同样依赖与某个 commit 的差异。已安装 Python 3验证脚本.github/workflows/check-redirects.py用它运行。课程本身的构建依赖mdbook、mdbook-svgbob 等按 README.md 的 Setup 一节安装先安装 Rust 和 Bazel然后运行cargo xtask install-tools。添加 redirect 条目本身只需要编辑book.toml构建验证mdbook build、mdbook serve要求见 CONTRIBUTING.md。在 book.toml 中添加 redirect 条目打开根目录的 book.toml找到[output.html.redirect]段。该段中的注释写明了格式约定# Redirects in the form of old-path new-path, where the new path # is relative to the old path. # # Please keep the table sorted and avoid multi-step redirects. [output.html.redirect] async/async-await.html ../concurrency/async/async-await.html concurrency.html concurrency/welcome.html三个要点全部来自该段注释与真实条目键是旧路径值是新路径且新路径相对于旧路径解析。例如async/async-await.html ../concurrency/async/async-await.html旧地址async/async-await.html指向新位置concurrency/async/async-await.html两者相差两级目录所以值带../。保持表按字典序排序keep the table sorted。避免多跳重定向avoid multi-step redirects不要把旧路径 A 指向 B、B 又指向 C而应让 A 直接指向最终页面。旧路径怎么推导CI 脚本 check-redirects.py 定义了推导规则被删除的.md文件去掉src/前缀、把.md换成.html就是 redirect 表里应有的键。例如删除src/async/futures.md键就是async/futures.html对应条目async/futures.html ../concurrency/async/futures.html值新路径同样相对于键解析。CI 脚本会把它还原回.md路径检查文件是否真实存在所以值的写法必须能让旧目录 新路径落到一个真实存在的.md文件上例如src/async/../concurrency/async/futures.html归一化后是src/concurrency/async/futures.md。目录页和重定向到站点根被删除的是目录入口页如structure.html时值可以带.htmlstructure.html running-the-course/course-structure.html。脚本允许键和值的扩展名是.html或空ALLOWED_REDIRECT_EXTENSIONS [.html, ]所以值也可以是目录路径。实际条目welcome.html ./就是把旧的欢迎页跳转到站点根。添加条目时把它按字母序插入[output.html.redirect]段中而不是追加到段尾。验证check-redirects 脚本与 CI项目内置了验证脚本 check-redirects.py用法来自脚本文档字符串python3 .github/workflows/check-redirects.py 仓库根目录 要对比的 commit如 origin/mainCI 中 check-redirects.yml 在每个 pull request 上运行checkout 时设置fetch-depth: 0拿完整历史然后执行python3 .github/workflows/check-redirects.py . origin/main脚本做三件事对应三种输出找出删除的文件git diff commit --diff-filterD --name-only筛选出src/下、以.md结尾的文件。检查缺失的 redirect每个删除文件的推导 HTML 路径必须出现在[output.html.redirect]的键里否则打印The following deleted files are missing a redirect entry in book.toml:并列出文件名。检查非法条目一行里必须恰好一个否则报Only one expected键和值的扩展名必须是.html或空否则报Invalid file extension. Path must be a directory or a file ending in .html.值按相对旧路径解析还原成.md后文件必须存在否则报could not find ...。退出码即判定标准存在非法条目或缺失的 redirect 时退出码为 1否则为 0。所以本地在提交前可以直接跑一遍确认python3 .github/workflows/check-redirects.py . origin/main输出为空、退出码 0 即通过有输出时按上面三类报错定位是漏加了条目还是新路径写错了。注意事项与后续脚本只检查删除的.md文件是否有 redirect它不会检查你新加的条目是否指向了想要的页面除了目标文件存在性。值写错但文件存在例如指向了错误的新页面脚本发现不了添加后要自己核对新旧路径的对应关系。写网页测试tests/目录WebdriverIO时GEMINI.md 的 Handling Redirects 一节提醒mdbook按book.toml的[output.html.redirect]做跳转测试导航应使用最终的非跳转 URL导航到会被重定向的地址浏览器跟随后可能丢掉 URL 查询参数导致依赖这些参数的功能测试失败。也就是说redirect 是给读者用的自动化测试里要绕开它。修改完book.toml后按 CONTRIBUTING.md 的要求确认mdbook build能通过、mdbook serve可用再用dprint fmt统一格式化然后提交 pull request 让 check-redirects 工作流在origin/main基线上做最终校验。【免费下载链接】comprehensive-rustThis is the Rust course used by the Android team at Google. It provides you the material to quickly teach Rust.项目地址: https://gitcode.com/GitHub_Trending/co/comprehensive-rust创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表