Skip to content

Commit

Permalink
update how templates folder is configured
Browse files Browse the repository at this point in the history
- Renamed `templates.subdir` to `templates.folder`.
- Allow `templates.folder` to point to a dir outside of vault.

Addresses #565.
  • Loading branch information
epwalsh committed May 1, 2024
1 parent 754d341 commit dbc2b0a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed

- Renamed config field `templates.subdir` to `templates.folder` (`subdir` still supported for backwards compat).
- You can now use a templates folder outside of your vault.

## [v3.7.11](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.11) - 2024-04-30

### Added
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ This is a complete list of all of the options that can be passed to `require("ob

-- Optional, for templates (see below).
templates = {
subdir = "templates",
folder = "templates",
date_format = "%Y-%m-%d",
time_format = "%H:%M",
-- A map for custom variables, the key should be the variable and the value a function
Expand Down Expand Up @@ -654,7 +654,7 @@ For example, with the following configuration
-- other fields ...

templates = {
subdir = "my-templates-folder",
folder = "my-templates-folder",
date_format = "%Y-%m-%d-%a",
time_format = "%H:%M",
},
Expand Down Expand Up @@ -718,7 +718,7 @@ For example, to extend the configuration above this way:
+ notes_subdir = vim.NIL, -- have to use 'vim.NIL' instead of 'nil'
+ new_notes_location = "current_dir",
+ templates = {
+ subdir = vim.NIL,
+ folder = vim.NIL,
+ },
+ disable_frontmatter = true,
+ },
Expand Down
24 changes: 13 additions & 11 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,19 @@ Client.templates_dir = function(self, workspace)
opts = self:opts_for_workspace(workspace)
end

if opts.templates ~= nil and opts.templates.subdir ~= nil then
local templates_dir = self:vault_root(workspace) / opts.templates.subdir
if not templates_dir:is_dir() then
log.err("'%s' is not a valid directory for templates", templates_dir)
return nil
else
return templates_dir
end
else
if opts.templates == nil or opts.templates.folder == nil then
return nil
end

local paths_to_check = { Path.new(opts.templates.folder), self:vault_root(workspace) / opts.templates.folder }
for _, path in ipairs(paths_to_check) do
if path:is_dir() then
return path
end
end

log.err("'%s' is not a valid templates directory", opts.templates.folder)
return nil
end

--- Determines whether a note's frontmatter is managed by obsidian.nvim.
Expand Down Expand Up @@ -350,8 +352,8 @@ Client._prepare_search_opts = function(self, opts, additional_opts)
search_opts.sort_reversed = self.opts.sort_reversed
end

if not opts.include_templates and self.opts.templates ~= nil and self.opts.templates.subdir ~= nil then
search_opts:add_exclude(self.opts.templates.subdir)
if not opts.include_templates and self.opts.templates ~= nil and self.opts.templates.folder ~= nil then
search_opts:add_exclude(tostring(self.opts.templates.folder))
end

if opts.ignore_case then
Expand Down
9 changes: 7 additions & 2 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ config.ClientOpts.normalize = function(opts, defaults)
end
end

if opts.templates and opts.templates.subdir then
opts.templates.folder = opts.templates.subdir
opts.templates.subdir = nil
end

--------------------------
-- Merge with defaults. --
--------------------------
Expand Down Expand Up @@ -369,7 +374,7 @@ end

---@class obsidian.config.TemplateOpts
---
---@field subdir string
---@field folder string|obsidian.Path|?
---@field date_format string|?
---@field time_format string|?
---@field substitutions table<string, function|string>|?
Expand All @@ -380,7 +385,7 @@ config.TemplateOpts = {}
---@return obsidian.config.TemplateOpts
config.TemplateOpts.default = function()
return {
subdir = nil,
folder = nil,
date_format = nil,
time_format = nil,
substitutions = {},
Expand Down

0 comments on commit dbc2b0a

Please sign in to comment.