Skip to content

Commit 9f181fb

Browse files
authored
feat: arguments in template function (#28)
* feat: pass arguments to template function * test: add tests for template function with arguments * docs: add template args * fix: interpolate placeholders before passing args to template function * test: add template args tests * docs: rename placeholders to context
1 parent bfb7d05 commit 9f181fb

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ Templates in the plugin use placeholders that are dynamically replaced with the
198198
| `$LABEL` | Figure label, generated from the file name, converted to lower-case and with spaces replaced by dashes. | `the-image` (from `the image.png`) |
199199
| `$CURSOR` | Indicates where the cursor will be placed after insertion if `use_cursor_in_template` is true. | |
200200

201+
Templates can also be defined using functions with the above placeholders available as function parameters:
202+
203+
```lua
204+
template = function(context)
205+
return "![" .. context.cursor .. "](" .. context.file_path .. ")"
206+
end
207+
```
208+
201209
## Drag and drop
202210

203211
The drag and drop feature enables users to drag images from the web browser or file explorer into the terminal to automatically embed them, in **normal mode**. For this to work correctly, the following is required by the terminal emulator:

lua/img-clip/config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ M.options = {}
9797
---@param key string
9898
---@param opts? table The options passed to pasteImage function
9999
---@return string | nil
100-
M.get_option = function(key, opts)
100+
M.get_option = function(key, opts, args)
101101
local ft = vim.bo.filetype
102102
local val
103103

@@ -136,7 +136,7 @@ M.get_option = function(key, opts)
136136
return nil
137137
end
138138

139-
return type(val) == "function" and val() or val -- execute if function
139+
return type(val) == "function" and val(args or {}) or val -- execute if function
140140
end
141141

142142
function M.setup(opts)

lua/img-clip/markup.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ end
6161
---@param opts? table
6262
---@return boolean
6363
function M.insert_markup(file_path, opts)
64-
local template = config.get_option("template", opts)
65-
if not template then
66-
return false
67-
end
68-
6964
local file_name = vim.fn.fnamemodify(file_path, ":t")
7065
local file_name_no_ext = vim.fn.fnamemodify(file_path, ":t:r")
7166
local label = file_name_no_ext:gsub("%s+", "-"):lower()
@@ -80,6 +75,19 @@ function M.insert_markup(file_path, opts)
8075
file_path = fs.relpath(file_path, current_dir_path)
8176
end
8277

78+
-- pass args to template
79+
local template_args = {
80+
file_path = file_path,
81+
file_name = file_name,
82+
file_name_no_ext = file_name_no_ext,
83+
cursor = "$CURSOR",
84+
label = label,
85+
}
86+
local template = config.get_option("template", opts, template_args)
87+
if not template then
88+
return false
89+
end
90+
8391
-- url encode path
8492
if config.get_option("url_encode_path", opts) then
8593
file_path = M.url_encode(file_path)

tests/markup_spec.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,29 @@ describe("markup", function()
8989

9090
assert.is_true(success)
9191
end)
92+
93+
it("inserts correct markup into a file when using template args", function()
94+
config.setup({
95+
default = {
96+
template = function(context)
97+
return context.cursor
98+
.. " "
99+
.. context.file_path
100+
.. " "
101+
.. context.file_name
102+
.. " "
103+
.. context.file_name_no_ext
104+
.. " "
105+
.. context.label
106+
end,
107+
},
108+
})
109+
110+
local success = markup.insert_markup("/path/to/file.png")
111+
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
112+
113+
assert.equal(" /path/to/file.png file.png file file", lines[2])
114+
assert.is_true(success)
115+
end)
92116
end)
93117
end)

vimdoc.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ Templates in the plugin use placeholders that are dynamically replaced with the
198198
| `$LABEL` | Figure label, generated from the file name, converted to lower-case and with spaces replaced by dashes. | `the-image` (from `the image.png`) |
199199
| `$CURSOR` | Indicates where the cursor will be placed after insertion if `use_cursor_in_template` is true. | |
200200

201+
Templates can also be defined using functions with the above placeholders available as function parameters:
202+
203+
```lua
204+
template = function(context)
205+
return "![" .. context.cursor .. "](" .. context.file_path .. ")"
206+
end
207+
```
208+
201209
## Drag and drop
202210

203211
The drag and drop feature enables users to drag images from the web browser or file explorer into the terminal to automatically embed them, in **normal mode**. For this to work correctly, the following is required by the terminal emulator:

0 commit comments

Comments
 (0)