attachment link export to relative path in html export

because org-attach attachment link in html export are expanded to file link before parse, to change attachment link behaviour in html export, only need to modify org-attach-expand-links function.

(defun org-attach-expand-links (_)
  "Expand links in current buffer.
It is meant to be added to `org-export-before-parsing-hook'."
  (save-excursion
    (while (re-search-forward "attachment:" nil t)
      (let ((link (org-element-context)))
        (when (and (org-element-type-p link 'link)
                   (string-equal "attachment"
                                 (org-element-property :type link)))
          (let* ((description (and (org-element-contents-begin link)
                                   (buffer-substring-no-properties
                                    (org-element-contents-begin link)
                                    (org-element-contents-end link))))
                 (file (org-element-property :path link))
                 (new-link (org-link-make-string
                            (concat "file:" (file-relative-name (org-attach-expand file)))
                            description)))
            (goto-char (org-element-end link))
            (skip-chars-backward " \t")
            (delete-region (org-element-begin link) (point))
            (insert new-link)))))))

Author: Linfeng He

Created: 2024-04-03 Wed 20:58