Selective org inline image toggle

Today while working on my flameshot article, I wanted to toggle inline images only where my cursor was. I was piling a ton of images and didn't want to wade through each of them.

output-2025-01-07-10:44:50.gif
Figure 1: animation of toggling inline images at point

Here's the same image in html that I toggle on org mode:

clipboard-20250107T090717.png
Figure 2: An image for toggling 1
clipboard-20250107T090717.png
Figure 3: An image for toggling 2

Luckily, org-toggle-inline-images allows you to specify the beginning and end of a region:

(org-toggle-inline-images t 379 448)

And here's the code:

(defun jwow/toggle-image ()
  "Toggle viewing the image from an org LINK at POINT if the LINK paths to
an image. Otherwise, toggle all images.

If the link is to an image but it does not exist, nothing will happen."
  (interactive)
  (if-let ((link-at-point (org-in-regexp org-link-bracket-re 1)))
      (if-let* ((link-context (org-element-context))
                (local-file? (member (org-element-property :type link-context)
                                     '("attachment" "file")))
                (image? (org-file-image-p (org-element-property :path link-context))))
          (org-toggle-inline-images t (car link-at-point) (cdr link-at-point))
        (org-toggle-inline-images t))
    (org-toggle-inline-images t)))

(define-key org-mode-map (kbd "C-t") #'jwow/toggle-image)

Most of the time taken to code was actually digging into ol.el and org.el to determine if something was a valid image link or not.

Hope this helped!