From ef1b005f5ce53cf099e5eaf1bb4630f4ea34f04a Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sat, 2 Jan 2021 22:52:00 -0600 Subject: Make anchors in Org more useful --- config.org | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/config.org b/config.org index 65dc47a..93e136d 100644 --- a/config.org +++ b/config.org @@ -1488,6 +1488,47 @@ I’ve put org mode under Applications, as opposed to Writing, because it’s m (add-hook 'emacs-lisp-mode-hook #'org-link-minor-mode) #+end_src +*** Useful HTML Anchors + + #+begin_src emacs-lisp + (defun my/ensure-headline-ids (&rest _) + "Ensure better headlines for Org trees. + + Use CUSTOM_ID property, or barring that, a \"fixed\" title. + + All non-alphanumeric characters are cleverly replaced with ‘-’. + + If multiple trees end-up with the same id property, issue a + message and undo any property insertion thus far. + + E.g., ↯ We'll go on a ∀∃⇅ adventure + ↦ We'll-go-on-a-adventure + " + (interactive) + (let ((ids)) + (org-map-entries + (lambda () + (org-with-point-at (point) + (let ((id (org-entry-get nil "CUSTOM_ID"))) + (unless id + (thread-last (nth 4 (org-heading-components)) + (s-replace-regexp "[^[:alnum:]']" "-") + (s-replace-regexp "-+" "-") + (s-chop-prefix "-") + (s-chop-suffix "-") + (setq id)) + (if (not (member id ids)) + (push id ids) + (message-box "Oh no, a repeated id!\n\n\t%s" id) + (undo) + (setq quit-flag t)) + (org-entry-put nil "CUSTOM_ID" id)))))))) + + ;; Whenever html & md export happens, ensure we have headline ids. + (advice-add 'org-html-export-to-html :before 'my/ensure-headline-ids) + (advice-add 'org-md-export-to-markdown :before 'my/ensure-headline-ids) + #+end_src + ** Git #+begin_src emacs-lisp -- cgit 1.4.1-21-gabe81