I recently switched away from WordPress to the Hugo static site generator. For the most part I have been very happy. The biggest downside has been related to publishing a new post. Unlike WordPress, there is no app. Publishing requires SSHing into a server and running a series of commands by hand. What I wanted was a one click posting option. In addition I needed a way to load the article into Apple News.
Workflow to the rescue.
Now that the future of Workflow seems secure in iOS 12 Shortcuts I decided to use it to automate the process of posting a new article. The workflow takes in text, asks for a title, generates a preview, then posts to both the site and Apple News.
My setup consists of two Linux servers. One runs the Hugo software where the raw text files live. The other is the actual webserver. Everything is previewed in the first server. Then the site is built there and synced over using rsync.
The workflow accepts plain text. Currently I am not using images on the new site. So this workflow does not account for them. The nice thing is I can use basically any app that can generate text. I am not locked in to anything. Then a prompt asks for a title in proper title casing, saves it as a variable, then generates a file name by transforming to lowercase and converting spaces to hyphens. The final variable is the date in ISO 8601 format.
Now I generate the markdown document in the format my theme requires. Using cat I send it up to the test server over SSH.
Next I want to preview the post to make sure it looks how I want. Hugo has a built in preview server for this very purpose. My idea was to generate the preview over SSH and then jump to safari to see it. But I hit my first snag. When you use the hugo server command, it does not return to a prompt until you kill the running process. So using the SSH block in workflow causes the whole thing to pause at this point.
The solution was to open the Hugo server in screen. This runs it in the background and lets the workflow continue. I use the server IP instead of a domain name because I will be previewing the server over regular http. Since I am in the HSTS preload list I would be unable to get to it using the domain.
Workflow then opens this page in Safari so I can preview. It pauses until I return. When I do it kills the screen session, which terminates the preview server.
Next I ask whether to continue. If the preview had a problem I can abort at this point. Otherwise the workflow continues. I delete the public folder that was built the last time I ran Hugo. Then run it to build the new site, sync it to the webserver using rsync, and push the changes to a private GitHub repo as a form of backup.
And now the post is live. The last step is to get the it into Apple News. This was tricky because back when I was on WordPress I switched to the Apple News format away from pulling an RSS feed. This means the post needs to be uploaded using the API. But there is no Hugo plugin I can find. So I’m doing it myself.
Still in Workflow I generate a JSON file for Apple News. Thankfully News supports markdown. All I need to do to my article is to replace all newline characters with a literal BACKSLASH N to get the whole thing onto a single line. Using regex, Find BACKSLASH N, and replace them with the literal BACKSLASH BACKSLASH N. Yes, I am avoiding typing those characters out here so as not to break the workflow.
Then I use the Apple News ruby client on my server to upload the article.
And that’s it. I now have a full publishing app built right within Workflow. It really goes to show just how powerful this app is. I am beyond relieved that Apple is keeping this after purchasing the app, and that iOS 12 will probably allow for even more powerful apps to be built. And it serves as another reminder that iOS really has become a productivity platform.