组织模式内部 link 动态命名

Org mode internal link dynamic naming

有没有办法根据原始标题自动重命名 link?

示例:

* Headline 1
:PROPERTIES:
:ID: some_id
:END:

* Headline 2

 - Headline 1 // this is a link using an id

* Headline 3

 - Headline 1 // this is a link using an id

创建新的内部 link 时,org 允许您为该 link 指定文本。有没有办法根据标题动态更改 linking 文本?如果我将 Headline 1 更改为 Headline 4,我希望 link 的文本也自动更改为 Headline 4

这可能吗?

它有点慢,但这很有效:

(defun us/description-of-target-link (target-id)
  (car (org-map-entries '(org-entry-get nil "ITEM") (concat "ID=\"" target-id "\"")))
  )

(defun us/update-internal-link-descriptions ()
  (interactive)
  (setq offset 0)
  ;;; map over all elements in the buffer
  (org-element-map (org-element-parse-buffer) 'link
    (lambda (link)
      ;;; filter to internal links with id
      (when (string= (org-element-property :type link) "id")
        (setq content-start-point-raw (org-element-property :contents-begin link))
        (setq content-end-point-raw (org-element-property :contents-end link))
        (setq len-thing-to-remove (- content-end-point-raw content-start-point-raw))
        (setq new-description (us/description-of-target-link (org-element-property :path link)))
        (setq len-thing-to-add (length new-description))
        (setq offset-delta (- len-thing-to-add len-thing-to-remove))
        (setq content-remove-point-start-with-offset (+ content-start-point-raw offset))
        (setq content-remove-point-end-with-offset (+ content-end-point-raw offset))
        (goto-char content-remove-point-start-with-offset)
        (delete-region content-remove-point-start-with-offset content-remove-point-end-with-offset)
        (insert new-description)
        (setq offset (+ offset offset-delta ))
        )
      )
    )
  )