mirror of
https://github.com/org-roam/org-roam
synced 2025-09-18 16:06:49 -05:00
Build additional titles cache
This commit is contained in:
66
org-roam.el
66
org-roam.el
@@ -169,8 +169,11 @@ If `ABSOLUTE', return the absolute file-path. Else, return the relative file-pat
|
|||||||
,(async-inject-variables "org-roam-files")
|
,(async-inject-variables "org-roam-files")
|
||||||
,(async-inject-variables "org-roam-directory")
|
,(async-inject-variables "org-roam-directory")
|
||||||
(let ((backward-links (make-hash-table :test #'equal))
|
(let ((backward-links (make-hash-table :test #'equal))
|
||||||
(forward-links (make-hash-table :test #'equal)))
|
(forward-links (make-hash-table :test #'equal))
|
||||||
(cl-flet* ((org-roam--parse-content (file) (with-temp-buffer
|
(file-titles (make-hash-table :test #'equal)))
|
||||||
|
(cl-flet* ((org-roam--parse-content
|
||||||
|
(file)
|
||||||
|
(with-temp-buffer
|
||||||
(insert-file-contents file)
|
(insert-file-contents file)
|
||||||
(with-current-buffer (current-buffer)
|
(with-current-buffer (current-buffer)
|
||||||
(org-element-map (org-element-parse-buffer) 'link
|
(org-element-map (org-element-parse-buffer) 'link
|
||||||
@@ -191,7 +194,9 @@ If `ABSOLUTE', return the absolute file-path. Else, return the relative file-pat
|
|||||||
(list :from file
|
(list :from file
|
||||||
:to (file-truename (expand-file-name path org-roam-directory))
|
:to (file-truename (expand-file-name path org-roam-directory))
|
||||||
:content (string-trim content))))))))))
|
:content (string-trim content))))))))))
|
||||||
(org-roam--process-items (items) (mapcar
|
(org-roam--process-items
|
||||||
|
(items)
|
||||||
|
(mapcar
|
||||||
(lambda (item)
|
(lambda (item)
|
||||||
(pcase-let ((`(:from ,p-from :to ,p-to :content ,content) item))
|
(pcase-let ((`(:from ,p-from :to ,p-to :content ,content) item))
|
||||||
;; Build forward-links
|
;; Build forward-links
|
||||||
@@ -215,14 +220,32 @@ If `ABSOLUTE', return the absolute file-path. Else, return the relative file-pat
|
|||||||
(let ((contents-hash (make-hash-table :test #'equal)))
|
(let ((contents-hash (make-hash-table :test #'equal)))
|
||||||
(puthash p-from (list content) contents-hash)
|
(puthash p-from (list content) contents-hash)
|
||||||
(puthash p-to contents-hash backward-links))))))
|
(puthash p-to contents-hash backward-links))))))
|
||||||
items)))
|
items))
|
||||||
|
(org-roam--extract-title
|
||||||
|
(buffer)
|
||||||
|
(with-current-buffer buffer
|
||||||
|
(org-element-map
|
||||||
|
(org-element-parse-buffer)
|
||||||
|
'keyword
|
||||||
|
(lambda (kw)
|
||||||
|
(when (string= (org-element-property :key kw) "TITLE")
|
||||||
|
(org-element-property :value kw)))
|
||||||
|
:first-match t))))
|
||||||
(mapcar #'org-roam--process-items
|
(mapcar #'org-roam--process-items
|
||||||
(mapcar #'org-roam--parse-content org-roam-files)))
|
(mapcar #'org-roam--parse-content org-roam-files))
|
||||||
|
(mapcar (lambda (file)
|
||||||
|
(with-temp-buffer
|
||||||
|
(insert-file-contents file)
|
||||||
|
(when-let ((title (org-roam--extract-title (current-buffer))))
|
||||||
|
(puthash file title file-titles))))
|
||||||
|
org-roam-files))
|
||||||
(list
|
(list
|
||||||
:forward forward-links
|
:forward forward-links
|
||||||
:backward backward-links)))
|
:backward backward-links
|
||||||
|
:titles file-titles)))
|
||||||
(lambda (cache)
|
(lambda (cache)
|
||||||
(setq org-roam-cache cache))))
|
(setq org-roam-cache cache)
|
||||||
|
(message "Org-roam cache built!"))))
|
||||||
|
|
||||||
(defun org-roam--insert-item (item)
|
(defun org-roam--insert-item (item)
|
||||||
"Insert `ITEM' into `org-roam-cache'.
|
"Insert `ITEM' into `org-roam-cache'.
|
||||||
@@ -231,7 +254,8 @@ If `ABSOLUTE', return the absolute file-path. Else, return the relative file-pat
|
|||||||
|
|
||||||
Before calling this function, `org-roam-cache' should be already populated."
|
Before calling this function, `org-roam-cache' should be already populated."
|
||||||
(let ((forward-cache (plist-get org-roam-cache :forward))
|
(let ((forward-cache (plist-get org-roam-cache :forward))
|
||||||
(backward-cache (plist-get org-roam-cache :backward)))
|
(backward-cache (plist-get org-roam-cache :backward))
|
||||||
|
(title-cache (plist-get org-roam-cache :titles)))
|
||||||
(pcase-let ((`(:from ,p-from :to ,p-to :content ,content) item))
|
(pcase-let ((`(:from ,p-from :to ,p-to :content ,content) item))
|
||||||
;; Build forward-links
|
;; Build forward-links
|
||||||
(let ((links (gethash p-from forward-cache)))
|
(let ((links (gethash p-from forward-cache)))
|
||||||
@@ -255,7 +279,8 @@ Before calling this function, `org-roam-cache' should be already populated."
|
|||||||
(puthash p-from (list content) contents-hash)
|
(puthash p-from (list content) contents-hash)
|
||||||
(puthash p-to contents-hash backward-cache))))
|
(puthash p-to contents-hash backward-cache))))
|
||||||
(setq org-roam-cache (list :forward forward-cache
|
(setq org-roam-cache (list :forward forward-cache
|
||||||
:backward backward-cache)))))
|
:backward backward-cache
|
||||||
|
:titles title-cache)))))
|
||||||
|
|
||||||
(defun org-roam--parse-content ()
|
(defun org-roam--parse-content ()
|
||||||
"Parse the current buffer, and return a list of items for processing."
|
"Parse the current buffer, and return a list of items for processing."
|
||||||
@@ -286,8 +311,9 @@ This is equivalent to removing the node from the graph."
|
|||||||
(with-current-buffer (current-buffer)
|
(with-current-buffer (current-buffer)
|
||||||
(let ((file (file-truename (buffer-file-name buffer)))
|
(let ((file (file-truename (buffer-file-name buffer)))
|
||||||
(forward-cache (plist-get org-roam-cache :forward))
|
(forward-cache (plist-get org-roam-cache :forward))
|
||||||
(backward-cache (plist-get org-roam-cache :backward)))
|
(backward-cache (plist-get org-roam-cache :backward))
|
||||||
;; Setup 1: Remove all existing links for file
|
(titles-cache (plist-get org-roam-cache :titles)))
|
||||||
|
;; Step 1: Remove all existing links for file
|
||||||
(when-let ((forward-links (gethash file forward-cache)))
|
(when-let ((forward-links (gethash file forward-cache)))
|
||||||
;; Delete backlinks to file
|
;; Delete backlinks to file
|
||||||
(dolist (link forward-links)
|
(dolist (link forward-links)
|
||||||
@@ -296,12 +322,28 @@ This is equivalent to removing the node from the graph."
|
|||||||
(puthash link backward-links backward-cache)))
|
(puthash link backward-links backward-cache)))
|
||||||
;; Clean out forward links
|
;; Clean out forward links
|
||||||
(remhash file forward-cache))
|
(remhash file forward-cache))
|
||||||
(setq org-roam-cache (list :forward forward-cache :backward backward-cache)))))
|
;; Step 2: Remove from the title cache
|
||||||
|
(remhash file titles-cache)
|
||||||
|
(setq org-roam-cache (list :forward forward-cache
|
||||||
|
:backward backward-cache
|
||||||
|
:titles titles-cache)))))
|
||||||
|
|
||||||
|
(defun org-roam--update-cache-title (buffer)
|
||||||
|
"Inserts the `TITLE' of file in buffer into the cache."
|
||||||
|
(when-let ((titles-cache (plist-get org-roam-cache :titles))
|
||||||
|
(title (org-roam--extract-title buffer)))
|
||||||
|
(puthash (file-truename (buffer-file-name buffer))
|
||||||
|
title
|
||||||
|
titles-cache)
|
||||||
|
(setq org-roam-cache (plist-put org-roam-cache :titles titles-cache))))
|
||||||
|
|
||||||
(defun org-roam--update-cache ()
|
(defun org-roam--update-cache ()
|
||||||
"Update `org-roam-cache' for the current buffer file."
|
"Update `org-roam-cache' for the current buffer file."
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(org-roam--clear-cache-for-buffer (current-buffer))
|
(org-roam--clear-cache-for-buffer (current-buffer))
|
||||||
|
;; Insert into title cache
|
||||||
|
(org-roam--update-cache-title (current-buffer))
|
||||||
|
;; Insert new items
|
||||||
(let ((items (org-roam--parse-content)))
|
(let ((items (org-roam--parse-content)))
|
||||||
(dolist (item items)
|
(dolist (item items)
|
||||||
(org-roam--insert-item item)))))
|
(org-roam--insert-item item)))))
|
||||||
|
Reference in New Issue
Block a user