diff --git a/CHANGELOG.md b/CHANGELOG.md index b1ef857..36e9a72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Changed - [#1352](https://github.com/org-roam/org-roam/pull/1352) prefer lower-case for roam_tag and roam_alias in interactive commands - [#1513](https://github.com/org-roam/org-roam/pull/1513) replaced hardcoded "svg" with defcustom org-roam-graph-filetype +- [#1540](https://github.com/org-roam/org-roam/pull/1540) allow `roam_tag` and `roam_alias` to be specified on multiple lines ### Fixed - [#1281](https://github.com/org-roam/org-roam/pull/1281) fixed idle-timer not instantiated on `org-roam-mode` diff --git a/org-roam.el b/org-roam.el index ed07060..a815c7b 100644 --- a/org-roam.el +++ b/org-roam.el @@ -559,6 +559,28 @@ any), and Org keywords. Org keywords take precedence." (org-roam--extract-global-props-keyword props) (org-roam--extract-global-props-drawer props))) +(defun org-roam--extract-prop-as-list (prop) + "Extract PROP from the current Org buffer as a list. + +This is the common logic behind the extraction of roam_tags and +roam_alias." + ;; Values are split in two ways: + ;; 1. with spaces and double quotes: + ;; #+prop: a b c \"quoted string\" + ;; -> '(\"a\" \"b\" \"c\" \"quoted string\") + ;; 2. and/or with multiple lines: + ;; #+prop: a b + ;; #+prop: c d + ;; -> '(\"a\" \"b\" \"c\" \"d\") + (--> (org-roam--extract-global-props (list prop)) + ;; so that the returned order is the same as in the buffer + nreverse + ;; '(("ROAM_TAGS" . "a b") ("ROAM_TAGS" . "c d")) + ;; -> '("a b" "c d") + (mapcar #'cdr it) + (mapcar #'split-string-and-unquote it) + ;; We have a list of lists at this point. Join them. + (apply #'append it))) (defun org-roam--get-outline-path () "Return the outline path to the current entry. @@ -667,18 +689,15 @@ If FILE-PATH is nil, use the current file." (defun org-roam--extract-titles-alias () "Return the aliases from the current buffer. Reads from the \"roam_alias\" property." - (let* ((prop (org-roam--extract-global-props '("ROAM_ALIAS"))) - (aliases (or (cdr (assoc "ROAM_ALIAS" prop)) - ""))) - (condition-case nil - (split-string-and-unquote aliases) - (error - (progn - (lwarn '(org-roam) :error - "Failed to parse aliases for buffer: %s. Skipping" - (or org-roam-file-name - (buffer-file-name))) - nil))))) + (condition-case nil + (org-roam--extract-prop-as-list "ROAM_ALIAS") + (error + (progn + (lwarn '(org-roam) :error + "Failed to parse aliases for buffer: %s. Skipping" + (or org-roam-file-name + (buffer-file-name))) + nil)))) (defun org-roam--extract-titles-headline () "Return the first headline of the current buffer." @@ -735,17 +754,15 @@ tag." (defun org-roam--extract-tags-prop (_file) "Extract tags from the current buffer's \"#roam_tags\" global property." - (let* ((prop (or (cdr (assoc "ROAM_TAGS" (org-roam--extract-global-props '("ROAM_TAGS")))) - ""))) - (condition-case nil - (split-string-and-unquote prop) - (error - (progn - (lwarn '(org-roam) :error - "Failed to parse tags for buffer: %s. Skipping" - (or org-roam-file-name - (buffer-file-name))) - nil))))) + (condition-case nil + (org-roam--extract-prop-as-list "ROAM_TAGS") + (error + (progn + (lwarn '(org-roam) :error + "Failed to parse tags for buffer: %s. Skipping" + (or org-roam-file-name + (buffer-file-name))) + nil)))) (defun org-roam--extract-tags-vanilla (_file) "Extract vanilla `org-mode' tags. diff --git a/tests/roam-files/tags/tag.org b/tests/roam-files/tags/tag.org index 206b026..3ca8954 100644 --- a/tests/roam-files/tags/tag.org +++ b/tests/roam-files/tags/tag.org @@ -1,4 +1,5 @@ #+roam_tags: "t1" "t2 with space" t3 +#+roam_tags: "t4 second-line" #+title: Tags This file is used to test functionality for =(org-roam--extract-tags)= diff --git a/tests/roam-files/titles/aliases.org b/tests/roam-files/titles/aliases.org index 440dd78..a9431d8 100644 --- a/tests/roam-files/titles/aliases.org +++ b/tests/roam-files/titles/aliases.org @@ -1 +1,2 @@ #+roam_alias: "roam" "alias" +#+roam_alias: "second" "line" diff --git a/tests/test-org-roam.el b/tests/test-org-roam.el index 77cbb87..4a95b33 100644 --- a/tests/test-org-roam.el +++ b/tests/test-org-roam.el @@ -126,7 +126,7 @@ (expect (test #'org-roam--extract-titles-alias "titles/aliases.org") :to-equal - '("roam" "alias")) + '("roam" "alias" "second" "line")) (expect (test #'org-roam--extract-titles-alias "titles/headline.org") :to-equal @@ -192,7 +192,7 @@ (expect (test #'org-roam--extract-tags-prop "tags/tag.org") :to-equal - '("t1" "t2 with space" "t3")) + '("t1" "t2 with space" "t3" "t4 second-line")) (expect (test #'org-roam--extract-tags-prop "tags/no_tag.org") :to-equal @@ -246,13 +246,13 @@ (test #'org-roam--extract-tags "tags/tag.org")) :to-equal - '("t1" "t2 with space" "t3"))) + '("t1" "t2 with space" "t3" "t4 second-line"))) (it "'(prop all-directories)" (expect (let ((org-roam-tag-sources '(prop all-directories))) (test #'org-roam--extract-tags "tags/tag.org")) :to-equal - '("t1" "t2 with space" "t3" "tags")))))) + '("t1" "t2 with space" "t3" "t4 second-line" "tags")))))) (describe "ID extraction" (before-all