I spent a bit of time today updating my website after some changes in the MyST-NB and Sphinx Design ecosystems.
Along the way, I decided to redirect /posts/
to /blog/
, since it seems /blog/
is a much more common folder to use for blog posts.
This posed a problem, because the sphinx-rediraffe
extension does not allow you to redirect folders with wildcards.
AKA, you cannot do:
rediraffe_redirects = {
"posts/**/*.md": "blog/**/*.md",
}
I also didn’t want to have to manually specify every single blog post, since that’d be a very long list.
Fortunately, I figured out a solution because Sphinx’s configuration is also a Python script.
This means you can dynamically populate this configuration with pathlib
.
I’ll share the code snippet below in case it’s useful for others: