Blog in Emacs - Work with Images03 Feb 2024
I do my best to keep my blog simple, I would not use images/videos unless I can’t demonstrate well enough in plain text, for example, to demonstrate a mobile app using a screenshot (Learn in Emacs - Building Up Vocabulary) or how to represent stock price charts for neutral network (Speed Up Sparse Boolean Data).
Even when I do, I keep the usage to the bare minimum: all I do is insert the image, make it centralised, and put a caption on top of it.
The Org-mode supports images well, with a few additional HTML attributes for each inserted image, I can fine-control the images’ position, alignment, size etc.
However, I can’t get the benefits because I migrated my blog posts from HTML to the Markdown format for its simplicity. Plus Jekyll comes with its little quirks when it comes to Markdown images. So I have to write something for myself.
I managed to achieve a satisfactory workflow for my simple usage of images in Emacs. It works well for the Jekyll site. Here’s the code and explanation.
org-download: is the package that I use to create the images for blogging from various sources.
I can drag images from external applications to Emacs, including browsers, Preview, or iPhoto. The images will be saved in the /project/assets/org-download folder per my matrix/project setup.
For the application that I can’t drag the images, I take a screenshot inside Emacs by calling the
yt/jekyll-copy-from-org-downkload: is a little helper function that transfers the files under the org-download folder to the /assets folder in a Jekyll site.
It lists the files in the source org-download folder and provides them as a selection list. It comes with auto-completion and fuzzy matches to help me choose the file.
It also strips out the special characters in the filename otherwise the URL will be broken in Jekyll.
yt/jekyll-insert-image: lists the files in the /assets folder so I can choose easily which image to use.
It brings up the Liquid template for image so I don’t have to remember its syntax. It ensures the file path is in the correct format (starts with /assets/), I just fill in the caption and size after selecting the file.
An extract of the code is listed below for demonstration propose. Future updates will be reflected in my .emacs.d git repo.