Skip to content

Commit 50c194c

Browse files
committed
feat: Add support for saving images to relative directories
from pull request epwalsh#798
1 parent 14e0427 commit 50c194c

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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 `opts.follow_img_func` option for customizing how to handle image paths.
1313
- Added better handling for undefined template fields, which will now be prompted for.
14+
- Added Support for saving pasted images to a relative directory based on the currently opened file's path, when attachments.relative_dir is set to true.
1415

1516
### Changed
1617

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ This is a complete list of all of the options that can be passed to `require("ob
524524
-- You can always override this per image by passing a full path to the command instead of just a filename.
525525
img_folder = "assets/imgs", -- This is the default
526526

527+
-- Set to true if you want newly added images to be saved in a subdirectory under current file's directory.
528+
-- If enabled and your current file is under vault/file_dir, newly pasted images will be saved under vault/file_dir/{img_folder}
529+
relative_dir = false, -- This is the default
530+
527531
-- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
528532
---@return string
529533
img_name_func = function()

lua/obsidian/commands/paste_img.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ return function(client, data)
1818
local path = paste_img {
1919
fname = data.args,
2020
default_dir = img_folder,
21+
relative_dir = client.opts.attachments.relative_dir,
2122
default_name = default_name,
2223
should_confirm = client.opts.attachments.confirm_img_paste,
2324
}

lua/obsidian/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ end
472472
---@class obsidian.config.AttachmentsOpts
473473
---
474474
---@field img_folder string Default folder to save images to, relative to the vault root.
475+
---@field relative_dir boolean Whether newly added attachments should be saved in a relative subfolder
475476
---@field img_name_func (fun(): string)|?
476477
---@field img_text_func fun(client: obsidian.Client, path: obsidian.Path): string
477478
---@field confirm_img_paste boolean Whether to confirm the paste or not. Defaults to true.
@@ -481,6 +482,7 @@ config.AttachmentsOpts = {}
481482
config.AttachmentsOpts.default = function()
482483
return {
483484
img_folder = "assets/imgs",
485+
relative_dir = false,
484486
---@param client obsidian.Client
485487
---@param path obsidian.Path the absolute path to the image file
486488
---@return string

lua/obsidian/img_paste.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ local function save_clipboard_image(path)
8484
end
8585
end
8686

87-
---@param opts { fname: string|?, default_dir: obsidian.Path|string|?, default_name: string|?, should_confirm: boolean|? }|? Options.
87+
---@param opts { fname: string|?, default_dir: obsidian.Path|string|?, relative_dir: boolean|?, default_name: string|?, should_confirm: boolean|? }|? Options.
8888
---
8989
--- Options:
9090
--- - `fname`: The filename.
9191
--- - `default_dir`: The default directory to put the image file in.
92+
--- - `relative_dir`: Save in a subdirectory under current directory.
9293
--- - `default_name`: The default name to assign the image.
9394
--- - `should_confirm`: Prompt to confirm before proceeding.
9495
---
@@ -139,7 +140,12 @@ M.paste_img = function(opts)
139140
-- fname is a full path
140141
path = path:resolve()
141142
elseif opts.default_dir ~= nil then
142-
path = (Path.new(opts.default_dir) / path):resolve()
143+
if opts.relative_dir then
144+
local opened_file_dir = Path.new(path.buffer()):parent()
145+
path = (opened_file_dir / opts.default_dir.name / path):resolve()
146+
else
147+
path = (Path.new(opts.default_dir) / path):resolve()
148+
end
143149
else
144150
log.err "'default_dir' must be provided"
145151
return

0 commit comments

Comments
 (0)