mirror of
https://github.com/org-roam/org-roam
synced 2025-08-01 12:17:21 -05:00
(feat): add 'first-directory option for org-roam-tag-sources (#851)
Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
- [#814] Implement `org-roam-insert-immediate`
|
||||
- [#833](https://github.com/org-roam/org-roam/pull/833) Add customization of file titles with `org-roam-title-to-slug-function`.
|
||||
- [#839](https://github.com/org-roam/org-roam/pull/839) Return selected file from `org-roam-insert`
|
||||
- [#851](https://github.com/org-roam/org-roam/pull/851) Add `'first-directory'` option for `org-roam-tag-sources`
|
||||
|
||||
### Bugfixes
|
||||
|
||||
|
@ -306,6 +306,9 @@ extraction methods supported are:
|
||||
3. ='last-directory=: Extracts the last directory relative to
|
||||
=org-roam-directory= as the tag. That is, if a file is located at relative
|
||||
path =foo/bar/file.org=, the file will have tag =bar=.
|
||||
4. ='first-directory=: Extracts the first directory relative to
|
||||
=org-roam-directory= as the tag. That is, if a file is located at relative
|
||||
path =foo/bar/file.org=, the file will have tag =foo=.
|
||||
|
||||
By default, only the ='prop= extraction method is enabled. To enable the other
|
||||
extraction methods, you may modify =org-roam-tag-sources=:
|
||||
|
@ -468,6 +468,10 @@ extracted as tags. That is, if a file is located at relative path
|
||||
@samp{'last-directory}: Extracts the last directory relative to
|
||||
@samp{org-roam-directory} as the tag. That is, if a file is located at relative
|
||||
path @samp{foo/bar/file.org}, the file will have tag @samp{bar}.
|
||||
@item
|
||||
@samp{'first-directory}: Extracts the first directory relative to
|
||||
@samp{org-roam-directory} as the tag. That is, if a file is located at relative
|
||||
path @samp{foo/bar/file.org}, the file will have tag @samp{foo}.
|
||||
@end enumerate
|
||||
|
||||
By default, only the @samp{'prop} extraction method is enabled. To enable the other
|
||||
|
18
org-roam.el
18
org-roam.el
@ -202,10 +202,16 @@ extraction methods:
|
||||
`last-directory'
|
||||
Extract the last directory relative to `org-roam-directory'.
|
||||
That is, if a file is located at relative path foo/bar/file.org,
|
||||
the file will have tag \"bar\"."
|
||||
the file will have tag \"bar\".
|
||||
|
||||
`first-directory'
|
||||
Extract the first directory relative to `org-roam-directory'.
|
||||
That is, if a file is located at relative path foo/bar/file.org,
|
||||
the file will have tag \"foo\"."
|
||||
:type '(set (const :tag "#+roam_tags" prop)
|
||||
(const :tag "sub-directories" all-directories)
|
||||
(const :tag "parent directory" last-directory)))
|
||||
(const :tag "parent directory" last-directory)
|
||||
(const :tag "first sub-directory" first-directory)))
|
||||
|
||||
(defcustom org-roam-title-to-slug-function #'org-roam--title-to-slug
|
||||
"Function to be used in converting a title to the filename slug.
|
||||
@ -642,6 +648,14 @@ The final directory component is used as a tag."
|
||||
(file-relative-name file org-roam-directory))))
|
||||
(last (f-split dir-relative))))
|
||||
|
||||
(defun org-roam--extract-tags-first-directory (file)
|
||||
"Extract tags from path FILE.
|
||||
The first directory component after `org-roam-directory' is used as a
|
||||
tag."
|
||||
(when-let ((dir-relative (file-name-directory
|
||||
(file-relative-name file org-roam-directory))))
|
||||
(list (car (f-split dir-relative)))))
|
||||
|
||||
(defun org-roam--extract-tags-prop (_file)
|
||||
"Extract tags from the current buffer's \"#roam_tags\" global property."
|
||||
(let* ((prop (cdr (assoc "ROAM_TAGS" (org-roam--extract-global-props '("ROAM_TAGS"))))))
|
||||
|
@ -211,6 +211,20 @@
|
||||
:to-equal
|
||||
'("deeply")))
|
||||
|
||||
(it "extracts from first directory"
|
||||
(expect (test #'org-roam--extract-tags-first-directory
|
||||
"base.org")
|
||||
:to-equal
|
||||
nil)
|
||||
(expect (test #'org-roam--extract-tags-first-directory
|
||||
"tags/tag.org")
|
||||
:to-equal
|
||||
'("tags"))
|
||||
(expect (test #'org-roam--extract-tags-first-directory
|
||||
"nested/deeply/deeply_nested_file.org")
|
||||
:to-equal
|
||||
'("nested")))
|
||||
|
||||
(describe "uses org-roam-tag-sources correctly"
|
||||
(it "'(prop)"
|
||||
(expect (let ((org-roam-tag-sources '(prop)))
|
||||
|
Reference in New Issue
Block a user