serhii.net

In the middle of the desert you can say anything you want

20 Jan 2022

Hugo use page permalinks to map Days from different folders to the same section in URL

Redirecting stuff

Had /dtb/days/day122.md-type posts, the older ones, and /dtb/days/1234-1234-my-title.md-type newer posts. They lived both in the same directory on disk, /content/dtb/days/.... The latter were converted from Obsidian, which meant (among other things) that deleting a page in Obsidian wouldn’t automatically delete the corresponding converted one in Hugo, and I couldn’t just rm -rf ..../days before each conversion because that would delete the older day234.md posts.

I wanted to put them in different folders on disk in ./content/, but keep the url structure serhii.net/dtb/post-name/ for both of them.

Solution was making all /dtb posts (incl. pages) use the section (dtb) in the permalink in config.yaml:

permalinks:
    dtb: '/:section/:filename'

Now they do, regardless of their location on disk.

Then I moved the old posts into ./content/dtb/old_days, kept the new ones in ./content/dtb/days

Lastly, this removes all converted posts (= all .mds except _index.md) before conversion so that no stray markdown posts are left:

find $OLD_DAYS | grep -v _index.md | xargs  rm 

Unsolved problems

Google still has serhii.net/dtb/days/... pages cached, and currently they’re available both from there and from /dtb/.... I can’t find a way to redirect all of the /dtb/days/... to /dtb/... except manually adding stuff to the frontmatter of each. I have scripts for that, but still ugly.

.htaccess is our friend.

" RewriteRule ^d/dtb(.*)$ /dtb$1 [R=301,NC,L]
RewriteRule ^dtb/days(.*)$ /dtb$1 [R=301,NC,L]

This is getting more and more bloated.

Generally, I see absolutely no reason not to rewrite this mess of build scripts in Python. obyde is a Python package, handling settings, file operations etc. is more intuitive to me in Python.

Instead I keep re-learning bash/zsh escape syntax every time, and I’m procrastinating doing error handling for the same reasons.

The only non-native thing would be rsync and git, which can be handled through a subprocess.

Nel mezzo del deserto posso dire tutto quello che voglio.