Skip to content

Commit 423a768

Browse files
ErlanRGneo451
andauthored
feat(img_paste): mimic Obsidian's behavior when pasting images. (#15)
* feat(img_paste): mimic Obsidian's behavior when pasting images. This commit attempts to implement behavior similar to Obsidian's when pasting an image, without requiring a name (#13). It also includes a string validation for the filename. Although Obsidian allows filenames to contain invalid characters, it warns you about potential issues with links not working as a result. * add default img_name_func, respects confirm and user define * use vim.uri_encode to escape file path * fix: use the builtin urlencode so that unicode path are readable * fix: remove unneeded annotation * fix: more descriptive CHANGELOG --------- Co-authored-by: zizhou teng (n451) <[email protected]> Co-authored-by: neo451 <[email protected]>
1 parent 6704029 commit 423a768

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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 default `image_name_func` similar to Obsidian's.
1213
- Added support `text/uri-list` to `ObsidianPasteImg`.
1314

1415
### Changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ This is a complete list of all of the options that can be passed to `require("ob
564564
-- You can always override this per image by passing a full path to the command instead of just a filename.
565565
img_folder = "assets/imgs", -- This is the default
566566

567-
-- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
567+
-- A function that determines default name or prefix when pasting images via `:ObsidianPasteImg`.
568568
---@return string
569569
img_name_func = function()
570570
-- Prefix image names with timestamp.
571-
return string.format("%s-", os.time())
571+
return string.format("Pasted image %s", os.date "%Y%m%d%H%M%S")
572572
end,
573573

574574
-- A function that determines the text to insert in the note when pasting an image.

lua/obsidian/config.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,12 @@ config.AttachmentsOpts = {}
483483
config.AttachmentsOpts.default = function()
484484
return {
485485
img_folder = "assets/imgs",
486-
---@param client obsidian.Client
487-
---@param path obsidian.Path the absolute path to the image file
488-
---@return string
489486
img_text_func = function(client, path)
490487
path = client:vault_relative_path(path) or path
491-
return string.format("![%s](%s)", path.name, path)
488+
return string.format("![%s](%s)", path.name, util.urlencode(tostring(path)))
489+
end,
490+
img_name_func = function()
491+
return string.format("Pasted image %s", os.date "%Y%m%d%H%M%S")
492492
end,
493493
confirm_img_paste = true,
494494
}

lua/obsidian/img_paste.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ M.paste_img = function(opts)
115115
fname = opts.default_name
116116
else
117117
fname = util.input("Enter file name: ", { default = opts.default_name, completion = "file" })
118-
if not fname then
118+
if fname == "" then
119+
fname = opts.default_name
120+
elseif not fname then
119121
log.warn "Paste aborted"
120122
return
121123
end
@@ -125,7 +127,10 @@ M.paste_img = function(opts)
125127
assert(fname)
126128
fname = util.strip_whitespace(fname)
127129

128-
if fname == "" then
130+
-- Verify filename
131+
if util.contains_invalid_characters(fname) then
132+
log.warn "Links will not work with file names containing any of these characters in Obsidian: # ^ [ ] |"
133+
elseif fname == "" then
129134
log.err "Invalid file name"
130135
return
131136
end

lua/obsidian/util.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,16 @@ util.buffer_is_empty = function(bufnr)
13741374
end
13751375
end
13761376

1377+
--- Check if a string contains invalid characters.
1378+
---
1379+
--- @param fname string
1380+
---
1381+
--- @return boolean
1382+
util.contains_invalid_characters = function(fname)
1383+
local invalid_chars = "#^%[%]|"
1384+
return string.find(fname, "[" .. invalid_chars .. "]") ~= nil
1385+
end
1386+
13771387
---Check if a string is NaN
13781388
---
13791389
---@param v any

0 commit comments

Comments
 (0)