Last time we looked at using the Wayback Machine Query GitHub Action to automate querying the Wayback Machine for all our broken links. Now we need to apply the changes to our files.

Ideally there’d be a GitHub Action that could take a list of our changes and apply them to a list of files. I did search for something like that but all I could find were actions that only made one single change. Time to create another action.

Enter the Replace multiple strings in files GitHub Action.

For example, to find all instances of ‘Multiple’ and replace them with ‘Many’ for all the .md files in the current directory you can do:

- uses: flcdrg/replace-multiple-action@v1
  with:
    files: './*.md'
    find: '[{ "find": "Multiple", "replace": "Many" }]'

For my case I want something like this:

- uses: flcdrg/replace-multiple-action@v1
  with:
    files: './*.md'
    find: '[{ "find": "http://localhost", "replace": "https://localhost"}, { "find": "http://davidgardiner.net.au", "replace": "https://david.gardiner.net.au" }]'
    prefix: '(^|\\s+|\\()'
    suffix: '($|\\s+|\\))'

The prefix and suffix input properties need some explanation. Originally I was just using a plain string find and replace, but I discovered that there was a problem.

Consider that I could have multiple broken links to a site. eg. blog.spencen.com/2010/09/04/word-puzzle-to-sliverlight-phonendashpart-3.aspx and blog.spencen.com.

I replace all the instances of the first URL with http://web.archive.org/web/20100926212957/http://blog.spencen.com/2010/09/04/word-puzzle-to-sliverlight-phonendashpart-3.aspx.

The problem comes with the next find/replace in that it is now looking for blog.spencen.com and that value also exists in the new snapshot URL! We potentially end up in a recursive ‘inception’ mess. To avoid this, we can supply some partial regular expressions that get concatenation before and after the broken URL when we’re searching. In my case those expressions mean that blog.spencen.com won’t be matched in the middle of the snapshot URL.

I’ve made these properties so the action is more general than just my use case of updating my blog posts.

To actually use the action in concert with the previous actions, I’m using it thus:


- name: Replacements
  uses: flcdrg/replace-multiple-action@v1
  with:
    find: ${{ steps.wayback.outputs.replacements }}
    prefix: '(^|\\s+|\\()'
    suffix: '($|\\s+|\\))'

The key difference here is that I’m making use of the output property from the Wayback Machine Query action.

Our files have been updated. Next we finish off the workflow by creating a pull request with the changes.