Skip to content

Commit

Permalink
Merge branch 'main' into ISSUE-632-support-for-obsidian-callouts
Browse files Browse the repository at this point in the history
  • Loading branch information
aquilesg committed Jul 29, 2024
2 parents b4c0dcd + d1ca121 commit f151e3b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 19 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added: Support for UI highlights on calloutblocks
- Added: Support for custom callout block defintions
- Added `opts.follow_img_func` option for customizing how to handle image paths.
- Added better handling for undefined template fields, which will now be prompted for.

### Changed

- Renamed `opts.image_name_func` to `opts.attachments.img_name_func`.

### Fixed

- Fixed an edge case with collecting backlinks.
- Fixed typo in `ObsidianPasteImg`'s command description

## [v3.9.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.9.0) - 2024-07-11

Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,6 @@ This is a complete list of all of the options that can be passed to `require("ob
-- Either 'wiki' or 'markdown'.
preferred_link_style = "wiki",

-- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
---@return string
image_name_func = function()
-- Prefix image names with timestamp.
return string.format("%s-", os.time())
end,

-- Optional, boolean or a function that takes a filename and returns a boolean.
-- `true` indicates that you don't want obsidian.nvim to manage frontmatter.
disable_frontmatter = false,
Expand Down Expand Up @@ -633,6 +626,14 @@ This is a complete list of all of the options that can be passed to `require("ob
-- If this is a relative path it will be interpreted as relative to the vault root.
-- You can always override this per image by passing a full path to the command instead of just a filename.
img_folder = "assets/imgs", -- This is the default

-- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
---@return string
img_name_func = function()
-- Prefix image names with timestamp.
return string.format("%s-", os.time())
end,

-- A function that determines the text to insert in the note when pasting an image.
-- It takes two arguments, the `obsidian.Client` and an `obsidian.Path` to the image file.
-- This is the default implementation.
Expand Down
15 changes: 8 additions & 7 deletions doc/obsidian.txt
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,6 @@ carefully and customize it to your needs:
-- Either 'wiki' or 'markdown'.
preferred_link_style = "wiki",

-- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
---@return string
image_name_func = function()
-- Prefix image names with timestamp.
return string.format("%s-", os.time())
end,

-- Optional, boolean or a function that takes a filename and returns a boolean.
-- `true` indicates that you don't want obsidian.nvim to manage frontmatter.
disable_frontmatter = false,
Expand Down Expand Up @@ -584,6 +577,14 @@ carefully and customize it to your needs:
-- If this is a relative path it will be interpreted as relative to the vault root.
-- You can always override this per image by passing a full path to the command instead of just a filename.
img_folder = "assets/imgs", -- This is the default

-- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
---@return string
img_name_func = function()
-- Prefix image names with timestamp.
return string.format("%s-", os.time())
end,

-- A function that determines the text to insert in the note when pasting an image.
-- It takes two arguments, the `obsidian.Client` and an `obsidian.Path` to the image file.
-- This is the default implementation.
Expand Down
3 changes: 2 additions & 1 deletion lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,8 @@ Client.find_backlinks_async = function(self, note, callback, opts)

-- Prepare search terms.
local search_terms = {}
for raw_ref in iter { tostring(note.id), note:fname(), self:vault_relative_path(note.path) } do
local note_path = Path.new(note.path)
for raw_ref in iter { tostring(note.id), note_path.name, note_path.stem, self:vault_relative_path(note.path) } do
for ref in
iter(util.tbl_unique {
raw_ref,
Expand Down
2 changes: 1 addition & 1 deletion lua/obsidian/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ M.register(

M.register(
"ObsidianPasteImg",
{ opts = { nargs = "?", complete = "file", desc = "Paste and image from the clipboard" } }
{ opts = { nargs = "?", complete = "file", desc = "Paste an image from the clipboard" } }
)

M.register(
Expand Down
4 changes: 2 additions & 2 deletions lua/obsidian/commands/paste_img.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ return function(client, data)

---@type string|?
local default_name
if client.opts.image_name_func then
default_name = client.opts.image_name_func()
if client.opts.attachments.img_name_func then
default_name = client.opts.attachments.img_name_func()
end

local path = paste_img {
Expand Down
7 changes: 6 additions & 1 deletion lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ local config = {}
---@field preferred_link_style obsidian.config.LinkStyle
---@field follow_url_func fun(url: string)|?
---@field follow_img_func fun(img: string)|?
---@field image_name_func (fun(): string)|?
---@field note_frontmatter_func (fun(note: obsidian.Note): table)|?
---@field disable_frontmatter (fun(fname: string?): boolean)|boolean|?
---@field completion obsidian.config.CompletionOpts
Expand Down Expand Up @@ -208,6 +207,11 @@ config.ClientOpts.normalize = function(opts, defaults)
opts.templates.subdir = nil
end

if opts.image_name_func then
opts.attachments.img_name_func = opts.image_name_func
opts.image_name_func = nil
end

--------------------------
-- Merge with defaults. --
--------------------------
Expand Down Expand Up @@ -573,6 +577,7 @@ end
---@class obsidian.config.AttachmentsOpts
---
---@field img_folder string Default folder to save images to, relative to the vault root.
---@field img_name_func (fun(): string)|?
---@field img_text_func fun(client: obsidian.Client, path: obsidian.Path): string
---@field confirm_img_paste boolean Whether to confirm the paste or not. Defaults to true.
config.AttachmentsOpts = {}
Expand Down
10 changes: 10 additions & 0 deletions lua/obsidian/templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ M.substitute_template_variables = function(text, client, note)
methods["path"] = tostring(note.path)
end

-- Replace known variables.
for key, subst in pairs(methods) do
for m_start, m_end in util.gfind(text, "{{" .. key .. "}}", nil, true) do
---@type string
Expand All @@ -90,6 +91,15 @@ M.substitute_template_variables = function(text, client, note)
end
end

-- Find unknown variables and prompt for them.
for m_start, m_end in util.gfind(text, "{{[^}]+}}") do
local key = util.strip_whitespace(string.sub(text, m_start + 2, m_end - 2))
local value = util.input(string.format("Enter value for '%s' (<cr> to skip): ", key))
if value and string.len(value) > 0 then
text = string.sub(text, 1, m_start - 1) .. value .. string.sub(text, m_end + 1)
end
end

return text
end

Expand Down

0 comments on commit f151e3b

Please sign in to comment.