Search code examples
emacselisporg-mode

how to org mode image absolute path of export html?


I need to export absoluted Image url in html in emacs org-mode file:

when i write the following code:

[[file:/images/a.jgp]]

export of html code is :

<img src="file:///images/a.jpg" >

but which i need is :

<img src="/images/a.jgp">

so how can i export what i wanted , instead of use #+BEGIN_HTML tag ?

ps: my emacs config:

 16 ;; org-mode project define
 17 (setq org-publish-project-alist
 18       '(
 19         ("org-blog-content"
 20          ;; Path to your org files.
 21          :base-directory "~/ChinaXing.org/org/"
 22          :base-extension "org"
 23 
 24          ;; Path to your jekyll project.
 25          :publishing-directory "~/ChinaXing.org/jekyll/"
 26          :recursive t
 27          :publishing-function org-publish-org-to-html
 28          :headline-levels 4
 29          :html-extension "html"
 30          :table-of-contents t
 31          :body-only t ;; Only export section between <body></body>
 32          )
 33 
 34         ("org-blog-static"
 35          :base-directory "~/ChinaXing.org/org/"
 36          :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|php\\|svg"
 37          :publishing-directory "~/ChinaXing.org/jekyll/"
 38          :recursive t
 39          :publishing-function org-publish-attachment)
 40         ("blog" :components ("org-blog-content" "org-blog-static"))
 41         ))

Solution

  • The way to do this is to register a new kind of link in org-mode, using org-add-link-type. That lets you supply a custom export format.

    org-add-link-type requires a prefix, a "what happens when you click the link?" function, and an export function.

    I use a prefix of img, so my links look like [[img:logo.png][Logo]]. My image files are in ../images/ (relative to the .org files), and from the webserver they show up in /images/. So for those settings, putting this in .emacs provides the solution:

    (defun org-custom-link-img-follow (path)
      (org-open-file-with-emacs
       (format "../images/%s" path)))
    
    (defun org-custom-link-img-export (path desc format)
      (cond
       ((eq format 'html)
        (format "<img src=\"/images/%s\" alt=\"%s\"/>" path desc))))
    
    (org-add-link-type "img" 'org-custom-link-img-follow 'org-custom-link-img-export)
    

    You'll probably need to amend the paths for your setup, but that's the recipe. As you'd expect, C-hforg-add-link-type will give you the full gory details.

    Oh, and for what it's worth, here's the code I'm using for inter-post links (like [[post:otherfile.org][Other File]]). There's a little Jekyll magic in the output format, so watch the double-%s.

    (defun org-custom-link-post-follow (path)
      (org-open-file-with-emacs path))
    
    (defun org-custom-link-post-export (path desc format)
      (cond
       ((eq format 'html)
        (format "<a href=\"{%% post_url %s %%}\">%s</a>" path desc))))
    
    (org-add-link-type "post" 'org-custom-link-post-follow 'org-custom-link-post-export)