diff --git a/org-roam-utils.el b/org-roam-utils.el index f62a331..0f64a82 100644 --- a/org-roam-utils.el +++ b/org-roam-utils.el @@ -179,6 +179,10 @@ specified via the #+ROAM_ALIAS property." (cons title alias-list) alias-list))) +(defun org-roam--extract-ref () + "Extract the ref from current buffer." + (cdr (assoc "ROAM_KEY" (org-roam--extract-global-props '("ROAM_KEY"))))) + (defun org-roam--build-cache (dir) "Build the org-roam caches in DIR." (let ((backward-links (make-hash-table :test #'equal)) @@ -201,7 +205,7 @@ specified via the #+ROAM_ALIAS property." (insert-file-contents file) (when-let ((titles (org-roam--extract-titles))) (puthash file titles file-titles)) - (when-let ((ref (cdr (assoc "ROAM_KEY" (org-roam--extract-global-props '("ROAM_KEY")))))) + (when-let ((ref (org-roam--extract-ref))) ;; FIXME: this overrides previous refs, should probably have a ;; warning when ref is not unique (puthash ref file refs))) diff --git a/org-roam.el b/org-roam.el index 5de476c..8cefe00 100644 --- a/org-roam.el +++ b/org-roam.el @@ -506,7 +506,12 @@ This is equivalent to removing the node from the graph." ;; Clean out forward links (remhash file (org-roam--forward-links-cache))) ;; Step 2: Remove from the title cache - (remhash file (org-roam--titles-cache)))) + (remhash file (org-roam--titles-cache)) + ;; Step 3: Remove from the refs cache + (maphash (lambda (k v) + (when (string= v file) + (remhash k (org-roam--refs-cache)))) + (org-roam--refs-cache)))) (defun org-roam--update-cache-titles () "Insert the title of the current buffer into the cache." @@ -515,12 +520,21 @@ This is equivalent to removing the node from the graph." titles (org-roam--titles-cache)))) +(defun org-roam--update-cache-refs () + "Insert the ref of the current buffer into the cache." + (when-let ((ref (org-roam--extract-ref))) + (puthash ref + (file-truename (buffer-file-name (current-buffer))) + (org-roam--refs-cache)))) + (defun org-roam--update-cache () "Update org-roam caches for the current buffer file." (save-excursion (org-roam--clear-file-from-cache) ;; Insert into title cache (org-roam--update-cache-titles) + ;; Insert into ref cache + (org-roam--update-cache-refs) ;; Insert new items (let ((items (org-roam--parse-content))) (dolist (item items) diff --git a/tests/test-org-roam.el b/tests/test-org-roam.el index 81f4eb0..afdc45a 100644 --- a/tests/test-org-roam.el +++ b/tests/test-org-roam.el @@ -324,7 +324,17 @@ ;; Links are updated with the appropriate name (expect (with-temp-buffer (insert-file-contents (abs-path "f3.org")) - (buffer-string)) :to-match (regexp-quote "[[file:meaningful-title.org][meaningful-title]]")))) + (buffer-string)) :to-match (regexp-quote "[[file:meaningful-title.org][meaningful-title]]"))) + + (it "web_ref -> hello" + (expect (->> (org-roam--refs-cache) + (gethash "https://google.com/")) + :to-equal (abs-path "web_ref.org")) + (rename-file (abs-path "web_ref.org") + (abs-path "hello.org")) + (expect (->> (org-roam--refs-cache) + (gethash "https://google.com/")) + :to-equal (abs-path "hello.org")))) (describe "delete file updates cache" (before-each @@ -340,4 +350,12 @@ (gethash (abs-path "f1.org"))) :to-be nil) (expect (->> (org-roam--backward-links-cache) (gethash (abs-path "nested/f1.org")) - (gethash (abs-path "nested/f2.org"))) :not :to-be nil))) + (gethash (abs-path "nested/f2.org"))) :not :to-be nil)) + (it "delete web_ref" + (expect (->> (org-roam--refs-cache) + (gethash "https://google.com/")) + :to-equal (abs-path "web_ref.org")) + (delete-file (abs-path "web_ref.org")) + (expect (->> (org-roam--refs-cache) + (gethash "https://google.com/")) + :to-be nil)))