A few weeks ago I wrote about failed image uploads. In that post I used an embedded Asciinema player, recommended by long-time SCCS friend Branen, to show the console output of atop.
I had initially tried ttyrec and ttygif, both of which are available in Homebrew. However ttyrec gave me some weird formatting errors and requires screen recording permissions to capture the Terminal window. asciinema is also in Homebrew and, even better, the recorded .cast files can be embedded using JavaScript in an “animated” text <div> that supports text selection, and in a much smaller file, since it’s not just a bunch of GIF frames.
There are a few old Asciinema WordPress plugins out there, including asciiplayer and wp-asciinema, but both of them are more than a few years old and don’t appear to support Blocks. I decided to follow the Player Quick Start and just create a Custom HTML block. I’m sure there’s a more robust/reusable way of doing this, but I won’t need this feature that often, so for now it’s Good Enough™.
First I had to upload the latest Asciinema player release. I didn’t want to mess with an NPM build or anything so I just manually grabbed the CSS and minified JS assets from there and uploaded them to my server under a new folder wp-content/asciinema/. I’ll probably update this manually periodically but this is otherwise a one-time operation.
First I had to record the session. I wanted to make sure the window was sized nicely and started with my web server session, so I ran asciinema this way:
asciinema rec -c "ssh mumakil" --cols 80 --rows 32 --overwrite mumakil-atop.cast
After running some atop commands and closing the session, I uploaded the recording text file to my server into the appropriate monthly wp-content directory. I could make this an even more targeted recording by also using the -c argument to ssh inside the command so it starts running atop right away.
Finally in the post editor I added a Custom HTML block containing the code necessary to load the player and my recording:
<link rel="stylesheet" type="text/css" href="/wp-content/asciinema/asciinema-player.css" />
<div id="mumakil-atop"></div>
<script src="/wp-content/asciinema/asciinema-player.min.js"></script>
<script>
AsciinemaPlayer.create('/wp-content/uploads/2024/07/mumakil-atop.cast', document.getElementById('mumakil-atop'));
</script>
This works in the WordPress block editor with its Preview mode. This also allows you to test that your asciinema setup is working; you’ll see nothing if the player files can’t load, as well as console errors that should give you an idea of what path/permissions/code issues need fixing. There’s no requirement for the id of the <div> to match the uploaded filename but I like to do that for consistency. The only thing that really matters is that all the paths have to be correct and that the files are readable by your web server user, and the target empty <div> matches the id specified as an argument to the player create script.
So, after initial player upload, the steps for a given post embed are:
- Record using asciinema
- Upload the output file somewhere publicly accessible
- Create the code snippet that loads the player and embeds the uploaded output file
You can of course also load a recording from another location by URL, passed as the first argument to AsciinemaPlayer.create instead of a local path, such as this gravity simulator example by temhelk:
If you were embedding multiple recordings in one post, you wouldn’t need the <link> and <script> elements to load the player again and could just do:
<div id="second-recording"></div>
<script>
AsciinemaPlayer.create('/wp-content/uploads/2024/07/second-recording.cast', document.getElementById('second-recording'));
</script>
That said I haven’t tested multiple embeds, and it might break down on search/index pages if there are multiple posts containing a block with the initial load, though they should be idempotent and cached. I guess the risk would be if the Asciinema player script has any JavaScript state side effects?
I think this custom HTML snippet would also work in a lot of other blog engines, or just in a static page. I also want to try it on the internal wiki at work. If you have a better way of setting this up, let me know!
What do you think?