You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's very important in HTML (#25) to have meaningful anchors generated from heading titles (like github does).
Here is some code to automatically add and update such anchors, if the file has #+options: anchor:t
;; Meaningful # anchors for headings
(defunva/org-add-headline-anchor (pomoverwrite)
"Add a meaningful anchor to one headline (entry at point-or-marker POM).If overwrite is not null and not 'maybe then overwrite existing CUSTOM_IDs."
(org-with-point-at pom
(let ((x (cadr (org-element-headline-parser (buffer-end1)))))
(if (or (and overwrite (not (eq overwrite 'maybe)))
(not (plist-get x :CUSTOM_ID)))
(let ((title (plist-get x :raw-value)))
(setq title (replace-regexp-in-string"\\W+""-" (downcase title)))
(setq title (replace-regexp-in-string"^-""" title))
(setq title (replace-regexp-in-string"-$""" title))
(org-entry-put pom "CUSTOM_ID" title)
(org-id-add-location title (buffer-file-name (buffer-base-buffer))))))))
(defunva/org-add-headline-anchors-maybe ()
(require'ox)
"Read in-buffer #+OPTIONS: anchor:* and if set, add anchors to all headlines."
(let ((overwrite (plist-get (org-export--get-inbuffer-options) :auto-anchor)))
(when overwrite
(org-map-entries (lambda () (va/org-add-headline-anchor (point) overwrite))))))
(defunva/org-before-save-hook-add-headline-anchors ()
"Hook to add meaningful anchors upon save."
(if (and (eq major-mode 'org-mode)
(eq buffer-read-only nil))
(va/org-add-headline-anchors-maybe)))
(defunva/org-add-headline-anchors ()
"Add meaningful anchors to all headlines, overwrite existing CUSTOM_ID, add #+OPTION: anchor:t."
(interactive)
(when (eq major-mode 'org-mode)
(require'ox)
(save-excursion
(beginning-of-buffer)
(unless (re-search-forward"^#\\+options:"nil'noerror)
(beginning-of-buffer)
(org-export-insert-default-template'default))
(unless (plist-get (org-export--get-inbuffer-options) :auto-anchor)
;; force anchor:t
(beginning-of-buffer)
(re-search-forward"^\\(#\\+options:.*anchor\\):\\(\\w+\\)")
(replace-match"\\1:t"'fixedcase))
(va/org-add-headline-anchors-maybe))))
;;(remove-hook 'before-save-hook 'va/org-before-save-hook-add-ids-to-headlines)
(add-hook'before-save-hook'va/org-before-save-hook-add-headline-anchors)
I agree that this is a crucial issue, especially as HTML will be the primary target format for document exports.
As an initial question: what do you think about the naming your functions, with the va/ prefix? It is of course possible to include them in the ELOT elisp file, but maybe it would be preferable to rename them with an elot- prefix.
It's very important in HTML (#25) to have meaningful anchors generated from heading titles (like github does).
Here is some code to automatically add and update such anchors, if the file has
#+options: anchor:t
I bind it like this:
I use a module to make TOC on top, and wrote some functions to control its update automatically
The text was updated successfully, but these errors were encountered: