Quantcast
Channel: Sacha Chua - category - emacs
Viewing all articles
Browse latest Browse all 834

Include inline SVGs in Org Mode HTML and Markdown exports

$
0
0

In my Org Mode HTML and Markdown exports, I usually want to include SVGs inline so that I can use links. Sometimes I also want to use Javascript and CSS to modify elements within the images. I used to use a my-include: link to do this, but I realized that I can also modify this behaviour by making my own functions that call org-html-link or org-md-link and then put those functions in org-export-backend-transcoders.

Here is an example of an SVG:

inline.svg

The following code overrides HTML and Markdown exports to include SVGs.

(defun my-ox-link-path (link _ info)
  (let* ((raw-path (org-element-property :path link)))
    (setq raw-path
          (org-export-file-uri
           (org-publish-file-relative-name raw-path info)))
    ;; Possibly append `:html-link-home' to relative file
    ;; name.
    (let ((home (and (plist-get info :html-link-home)
                     (org-trim (plist-get info :html-link-home)))))
      (when (and home
                 (plist-get info :html-link-use-abs-url)
                 (not (file-name-absolute-p raw-path)))
        (setq raw-path (concat (file-name-as-directory home) raw-path))))
    raw-path))

  (defun my-org-html-link (link desc info)
    (if (and
         (string= (org-element-property :type link) "file")
         (org-export-inline-image-p link (plist-get info :html-inline-image-rules)))
        (let ((path (my-ox-link-path link desc info)))
          (if (string= (file-name-extension path) "svg")
              (with-temp-buffer
                (insert-file-contents-literally path)
                (buffer-string))
            (org-html-link link desc info)))
      (org-html-link link desc info)))

(defun my-org-md-link (link desc info)
  (if (string= (org-element-property :type link) "file")
      (let ((path (my-ox-link-path link desc info)))
        (if (string= (file-name-extension path) "svg")
            (with-temp-buffer
              (insert-file-contents-literally path)
              (buffer-string))
          (org-md-link link desc info)))
    (org-md-link link desc info)))

(with-eval-after-load 'ox-html
  (setf
   (alist-get 'link (org-export-backend-transcoders (org-export-get-backend 'html)))
   'my-org-html-link))
(with-eval-after-load 'ox-md
  (setf
   (alist-get 'link (org-export-backend-transcoders (org-export-get-backend 'md)))
   'my-org-md-link))
This is part of my Emacs configuration.

Viewing all articles
Browse latest Browse all 834

Trending Articles