Skip to content

Commit 7e3ae5a

Browse files
committed
Add method Note:get_field(key: string)
1 parent 6231702 commit 7e3ae5a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Added client method `Client:current_note()` to get the note corresponding to the current buffer.
1313
- Added note method `Note.add_field(key: string, value: any)` to add/update an additional field in the frontmatter.
14+
- Added note method `Note.get_field(key: string)`.
1415
- Added note method ` Note.save_to_buffer(bufnr: integer|?, frontmatter: table|?)` for saving the frontmatter to a buffer.
1516

1617
## [v2.8.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v2.8.0) - 2024-01-26

lua/obsidian/note.lua

+17
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ Note.add_field = function(self, key, value)
148148
self.metadata[key] = value
149149
end
150150

151+
--- Get a field in the frontmatter.
152+
---
153+
---@param key string
154+
---
155+
---@return any result
156+
Note.get_field = function(self, key)
157+
if key == "id" or key == "aliases" or key == "tags" then
158+
error "Getting field '%s' this way is not allowed. Please use the corresponding attribute directly instead"
159+
end
160+
161+
if not self.metadata then
162+
return nil
163+
end
164+
165+
return self.metadata[key]
166+
end
167+
151168
--- Initialize a note from a file.
152169
---
153170
---@param path string|Path

0 commit comments

Comments
 (0)