Assign IDs to every entry in Org-mode

前端 未结 2 708
情深已故
情深已故 2020-12-25 09:04

Org-mode has a bundled extension called org-id, that implements global unique IDs for org-mode files. Every entry (a headline with its body) can have an ID property in its <

2条回答
  •  梦毁少年i
    2020-12-25 09:37

    I agree with Stuart—I don't think there's anything in org-id to do this automatically so it really comes down to when you want to add the ids.

    If you use org-capture to add all of your items then org-capture-prepare-finalize-hook is a logical place to put the code:

    (add-hook 'org-capture-prepare-finalize-hook 'org-id-get-create)
    

    Alternately you could do as Stuart suggested and add a save hook for org-mode files. I think the most idiomatic way to process all of the headlines in the file would be to use the mapping API:

    (defun my/org-add-ids-to-headlines-in-file ()
      "Add ID properties to all headlines in the current file which
    do not already have one."
      (interactive)
      (org-map-entries 'org-id-get-create))
    

    Finally we just need to add a before-save-hook which only runs in org-mode:

    (add-hook 'org-mode-hook
              (lambda ()
                (add-hook 'before-save-hook 'my/org-add-ids-to-headlines-in-file nil 'local)))
    

    That should do it!

提交回复
热议问题