Skip to content

Commit ae39ba3

Browse files
committed
Add option to customize default image name
See #355
1 parent b6eba26 commit ae39ba3

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
- Added configuration option `image_name_func: (fun(): string)|?` for customizing the default image name/prefix when pasting images via `:ObsidianPasteImg`.
1213
- Added client method `Client:current_note()` to get the note corresponding to the current buffer.
1314
- Added note method `Note.add_field(key: string, value: any)` to add/update an additional field in the frontmatter.
1415
- Added note method `Note.get_field(key: string)`.

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ This is a complete list of all of the options that can be passed to `require("ob
316316
return tostring(os.time()) .. "-" .. suffix
317317
end,
318318

319+
-- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
320+
image_name_func = function()
321+
-- Prefix image names with timestamp.
322+
return string.format("%s-", os.time())
323+
end,
324+
319325
-- Optional, boolean or a function that takes a filename and returns a boolean.
320326
-- `true` indicates that you don't want obsidian.nvim to manage frontmatter.
321327
disable_frontmatter = false,

lua/obsidian/commands/paste_img.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ return function(client, data)
99
img_folder = client.dir / client.opts.attachments.img_folder
1010
end
1111

12-
local path = paste_img(data.args, img_folder)
12+
---@type string|?
13+
local default_name
14+
if client.opts.image_name_func then
15+
default_name = client.opts.image_name_func()
16+
end
17+
18+
local path = paste_img(data.args, img_folder, default_name)
1319

1420
if path ~= nil then
1521
util.insert_text(client.opts.attachments.img_text_func(client, path))

lua/obsidian/config.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ local config = {}
1111
---@field log_level integer
1212
---@field notes_subdir string|?
1313
---@field templates obsidian.config.TemplateOpts
14-
---@field note_id_func function|?
15-
---@field follow_url_func function|?
16-
---@field note_frontmatter_func function|?
14+
---@field note_id_func (fun(title: string): string)|?
15+
---@field follow_url_func fun(url: string)|?
16+
---@field image_name_func (fun(): string)|?
17+
---@field note_frontmatter_func fun(note: obsidian.Note)|?
1718
---@field disable_frontmatter (fun(fname: string?): boolean)|boolean|?
1819
---@field backlinks obsidian.config.LocationListOpts
1920
---@field tags obsidian.config.LocationListOpts

lua/obsidian/img_paste.lua

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ local function get_clip_check_command()
2828
return check_cmd
2929
end
3030

31-
---Check if clipboard contains image data.
31+
--- Check if clipboard contains image data.
32+
---
3233
---@return boolean
3334
local function clipboard_is_img()
3435
local content = {}
@@ -49,8 +50,9 @@ local function clipboard_is_img()
4950
end
5051
end
5152

52-
---Save image from clipboard to `path`.
53+
--- Save image from clipboard to `path`.
5354
---@param path string
55+
---
5456
---@return boolean|integer|? result
5557
local function save_clipboard_image(path)
5658
local this_os = util.get_os()
@@ -84,15 +86,17 @@ end
8486

8587
---@param fname string|?
8688
---@param default_dir Path|string
89+
---@param default_name string|?
90+
---
8791
---@return Path|? image_path the absolute path to the image file
88-
M.paste_img = function(fname, default_dir)
92+
M.paste_img = function(fname, default_dir, default_name)
8993
if not clipboard_is_img() then
9094
log.err "There is no image data in the clipboard"
9195
return
9296
else
9397
-- Get filename to save to.
9498
if fname == "" then
95-
fname = vim.fn.input { prompt = "Enter file name: " }
99+
fname = vim.fn.input { prompt = "Enter file name: ", default = default_name }
96100
end
97101

98102
if fname == "" then

0 commit comments

Comments
 (0)