-
Notifications
You must be signed in to change notification settings - Fork 602
Mkdocs Migration Python Hook for #3860 #3999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: RoseWrightdev The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Welcome @RoseWrightdev! |
Hi @RoseWrightdev. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
On a very quick read, this is looking good. But it's been long enough since I programmed properly in Python that it's going to take me a bit to review the actual code. I may not be able to get to it until after I come back from PTO on the 14th of October, sorry. I'd like to see this confirmed to work properly with the changes introduced in #4075 though, since it will also change the TOC a bit. |
Summary
/kind feature
/kind docs
This pull request introduces tooling to facilitate large-scale documentation refactoring within MkDocs. It addresses the challenge of maintaining link integrity during file reorganization.
The implementation provides a command-line utility and an automated build hook that work together to assign permanent IDs to documents, generate 301 redirects for moved files, and convert fragile relative links into a resilient, future-proof macro format.
This tool serves as the technical foundation for the documentation migration outlined in the accompanying proposal, making a safe and efficient refactor achievable.
The Problem
When restructuring a documentation site, manually updating hundreds of internal links and creating redirects is not only tedious but also highly prone to errors. This process often leads to:
The Solution
This implementation provides a comprehensive, automated workflow that addresses these issues through a powerful command-line script (linking.py) and seamless build-time integration (main.py).
Key Features & Workflow
The workflow is broken down into three simple, powerful commands:
1. Prepare (--prepare)
Assigns a unique, permanent ID to every Markdown file and creates a redirect_map.json to snapshot the site's initial structure.
python linking.py --prepare
2. Convert Links (--convert-links)
Scans all documents and replaces fragile relative links with a resilient, ID-based macro that is immune to future file moves.
python linking.py --convert-links
Before: [My Link](../../section/another-page.md)
After: [My Link]({{ internal_link('another-page-id') }})
3. Build (mkdocs build)
During the build, the on_files hook automatically compares the new file structure to the original map and injects all necessary 301 redirect rules directly into your mkdocs.yml configuration. No manual updates are needed.
What's Included
Code
Testing
How to Use
Install Dependencies:
pip install mkdocs-redirects mkdocs-macros-plugin
Run the Migration Workflow:
# Step 1: Prepare files with unique IDs
python linking.py --prepare --docs-dir ./site-src
# Step 2: Convert all relative links to the macro format
python linking.py --convert-links --docs-dir ./site-src
# Step 3: Manually move and reorganize files as needed
git mv docs/old-path/doc.md docs/new-path/doc.md
# Step 4: Build the site. Redirects are generated automatically.
mkdocs build