nobiot / org-transclusion

Emacs package to enable transclusion with Org Mode
https://nobiot.github.io/org-transclusion/
GNU General Public License v3.0
902 stars 43 forks source link

Transclusion of [attachment:<file>] links from org-attach? #198

Open ashgillman opened 11 months ago

ashgillman commented 11 months ago

Hi, thanks for a great contribution to the Org Mode ecosystem.

I wonder if there is a known way to transclude images included with Attachment links?

I've tried :expand-links without luck.

Many thanks, Ash

nobiot commented 11 months ago

I do not know a way; I don’t use attachment links.

But… isn’t it just a link? Then why not use it as a link?

ashgillman commented 11 months ago

The transcluded link won't work. I guess its because [attachment:] is a context-unique link. It means the file relative to the current attachment directory, and the attachment directory is derived from the current context ID.

jtoloe commented 11 months ago

Attachment links can be expanded by org-attach-expand. Perhaps this could be added to :expand-links?

On the other hand, the real question might be how to treat properties of the transcluded source. Should the properties of the source or the heading that the transcluded region is in be respected?

jtoloe commented 11 months ago

This works for me:

(defun org-transclusion-content-filter-expand-links (link)
  "Convert LINK to an absolute filename.
LINK is assumed to be an Org element. This function does nothing
to LINK if the link is already absolute.

The current buffer is assumed to be the source buffer for the
transclusion."
  (when (or
         (string-equal "file" (org-element-property :type link))
         (string-equal "attachment" (org-element-property :type link)))
    (let ((type (org-element-property :type link))
          (path (org-element-property :path link)))
      (unless (file-name-absolute-p path)
        (setq link (org-element-put-property
                    link :path
                    (if (string-equal "attachment" type)
                        (org-attach-expand path)
                      (expand-file-name
                       path
                       (file-name-directory (buffer-file-name (current-buffer)))))))
        (if (string-equal "attachment" type)
            (setq link (org-element-put-property
                        link :type "file")))
        link))))
luavreis commented 10 months ago

This may be more or less equivalent to @jtoloe function, but I have this snippet in the gist below to support ID and attachment links with anchors that I've been using for a while. It uses org-transclusion-add-functions instead of :expand-links. Posting here if it's useful to someone. https://gist.github.com/lucasvreis/24f378f6f9a95f8c17906cb351bab2e1

Edit: actually, my gist does something completely different (I misunderstood the question): it adds support for attachment links as the transclusion link, instead of expanding their occurrences within the transcluded content into absolute links.