#+TITLE: Sacha Chua's Emacs configuration #+OPTIONS: toc:nil h:4 #+SETUPFILE: /home/sacha/vendor/org-html-themes/org/theme-readtheorg.setup #+STARTUP: showeverything #+PROPERTY: header-args:emacs-lisp :tangle yes :results silent :exports code #+ELEVENTY_COLLECTIONS: _posts #+ELEVENTY_BASE_DIR: ~/proj/static-blog/ #+ELEVENTY_CATEGORIES+: emacs #+ELEVENTY_LAYOUT: layouts/post #+LINK: dotemacs https://sachachua.com/dotemacs#%s #+TOC: headlines 4 #+RESULTS: * Moving my Org post subtree to the 11ty directory :11ty:org:emacs:blogging: :PROPERTIES: :EXPORT_DATE: 2023-01-08T11:07:23-0500 :EXPORT_ELEVENTY_PERMALINK: /blog/2023/01/moving-my-org-post-subtree-to-the-11ty-directory/ :EXPORT_ELEVENTY_FILE_NAME: blog/2023/01/moving-my-org-post-subtree-to-the-11ty-directory/ :END: I sometimes want to move the Org source for my blog posts to the same directory as the 11ty-exported HTML. This should make it easier to update and reexport blog posts in the future. The following code copies or moves the subtree to the 11ty export directory. #+begin_src emacs-lisp :results silent (defun my-org-11ty-copy-subtree (&optional do-cut) "Copy the subtree for the current post to the 11ty export directory. With prefix arg, move the subtree." (interactive (list current-prefix-arg)) (let* ((file-properties (org-element-map (org-element-parse-buffer) 'keyword (lambda (el) (list (org-element-property :key el) (org-element-property :value el) (buffer-substring-no-properties (org-element-property :begin el) (org-element-property :end el)))))) (entry-properties (org-entry-properties)) (filename (expand-file-name "index.org" (expand-file-name (assoc-default "EXPORT_ELEVENTY_FILE_NAME" entry-properties) (car (assoc-default "ELEVENTY_BASE_DIR" file-properties)))))) (unless (file-directory-p (file-name-directory filename)) (make-directory (file-name-directory filename) t)) ;; find the heading that sets the current EXPORT_ELEVENTY_FILE_NAME (goto-char (org-find-property "EXPORT_ELEVENTY_FILE_NAME" (org-entry-get-with-inheritance "EXPORT_ELEVENTY_FILE_NAME"))) (org-copy-subtree 1 (if do-cut 'cut)) (with-temp-file filename (org-mode) (insert (or (mapconcat (lambda (file-prop) (elt file-prop 2)) file-properties "") "") "\n") (org-yank)) (find-file filename) (goto-char (point-min)))) #+end_src Then this adds a link to it: #+begin_src emacs-lisp :results silent (defun my-org-export-filter-body-add-index-link (string backend info) (if (and (member backend '(11ty html)) (plist-get info :file-name) (plist-get info :base-dir) (file-exists-p (expand-file-name "index.org" (expand-file-name (plist-get info :file-name) (plist-get info :base-dir))))) (concat string (format "
" (plist-get info :permalink))) string)) (with-eval-after-load 'ox (add-to-list 'org-export-filter-body-functions #'my-org-export-filter-body-add-index-link)) #+end_src Then I want to wrap the whole thing up in an export function: #+begin_src emacs-lisp (defun my-org-11ty-export (&optional async subtreep visible-only body-only ext-plist) (let* ((info (org-11ty--get-info subtreep visible-only)) (file (org-11ty--base-file-name subtreep visible-only))) (unless (string= (plist-get info :input-file) (expand-file-name "index.org" (expand-file-name (plist-get info :file-name) (plist-get info :base-dir)))) (save-window-excursion (my-org-11ty-copy-subtree))) (org-11ty-export-to-11tydata-and-html async subtreep visible-only body-only ext-plist) (my-org-11ty-find-file))) #+end_src Now to figure out how to override the export menu. Totally messy hack! #+begin_src emacs-lisp (with-eval-after-load 'ox-11ty (map-put (caddr (org-export-backend-menu (org-export-get-backend '11ty))) ?o (list "To Org, JSON, HTML" 'my-org-11ty-export))) #+end_src