Script that keeps markdown in sync when renaming images

I wrote this script which updates the markdown when an image is renamed. Maybe someone may find it useful. Backup all computers within a 10 meter radius before you try it! :slight_smile:

Why I wrote it?

I’m porting a blog with hundreds of images which were posted over several years. The naming is terrible: anything between CRW_7127_Developed_in_UFRaw.jpg to gravity.png.

Renaming images and then finding the corresponding markdown files, correcting and saving them, would take me days. With this script it will be just hours. Could it be a feature of http://discuss.gohugo.io/#hugo server?

1 Like

I think this is interesting. I’d love to hear what others think about this.

If we are watching the FS for changes then we would know when something changed, we would still need to parse the entire text of every file to look for the prior names then rewrite the source.

Personally I think rewriting the source shouldn’t happen automatically, but I could see this as a feature behind a flag.

I think it would be amazing if Hugo did this automatically. It could tell you about missing assets, assets which are not used, and update .md files when things are moved / renamed (sure, with confirmation. I just needed to go over 450 images and confirmations would have made me slower).
Parsing hundreds or even thousands of .md files is quite fast anyway.

It could be also be a command that is run on demand. You move things around, and when you are done, you run “sync”, which interactively lets you make things consistent again, like when you resolve a conflict on git.

It would be nice if Hugo had a set of “manually run this task to fix stuff”. A task for someone could maybe be to create a API and some helpers to do this replacement stuff.

It could also be kind of a “go fix” to handle upgrades (syntax changes).

But it should be manually triggered, and the scripts should be idempotent - and marked clearly with "Backup your content before running this (read: commit to Git)).

The problem here is that it’s easy to track changes when they are being made if we are watching the filesystem (each move is a separate event). It’s very hard to reconcile changes after the fact as we see all changes simultaneously without watching the history.

I think it would be awesome if you renamed a post and it automatically updated all references and added aliases as appropriate. This could easily be accomplished with a dedicated ‘hugo move OLD NEW’ command.

I think something like ‘hugo fix’ would be very hard to implement perfectly and it’s value would depend on being reliable. I’d rather build a few individual tools that can accomplish specific tasks reliably and still give a huge benefit.

I’d also entertain a ‘hugo server --editsync’ or something mode that tracks changes and makes the changes to the source.