-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help with rendering images within ObsidianVault #190
Comments
Hey, we could probably add a hook for the Markdown integration, it must be trying to load the image from the same directory as the document when it should search for it in a special place. You can patch it in the markdown integration meanwhile. |
Will do. Here's what I have hard coded and got it to render correctly. elseif current_image and key == "url" then
current_image.url = "~/Documents/ObsidianVault/Attachments/" .. value
table.insert(images, current_image)
current_image = nil
end |
@exosyphon In case that helps, you can use the resolve_image_path = function(document_path, image_path, fallback)
local working_dir = vim.fn.getcwd()
-- Format image path for Obsidian notes
if (working_dir:find("path/to/your/vault")) then
return working_dir .. "/" .. image_path
end
-- Fallback to the default behavior
return fallback(document_path, image_path)
end, |
This is very helpful! One addition: if (working_dir:find("path/to/match",1,true)) then |
Does work if the file is in the same directory Does not work yet for obsidian vaults 3rd/image.nvim#190
I'm having a problem getting this to work. The return paths are correct, but the plugin is not rendering any images. resolve_image_path = function(document_path, image_path, fallback)
local working_dir = vim.fn.getcwd()
local notes_dir = vim.fn.expand(vim.env.NOTES_DIR)
-- Format image path for Obsidian notes
if working_dir:find(notes_dir, 1, true) then
return vim.fn.shellescape(notes_dir .. '/' .. image_path)
end
-- Fallback to the default behavior
return fallback(document_path, image_path)
end, Here is an example doc: File: ![[assets/something/hashmd5.png]]
The returned value from the If I run
But I don't see any previews, if I go an open a different |
Does work if the file is in the same directory Does not work yet for obsidian vaults 3rd/image.nvim#190
@ahmedelgabri I investigated your problem a bit. I had to slightly modify your function to set the environment variable to my own Obsidian path: vim.env.NOTES_DIR = "/home/fic/second-brain"
I was able to fix your function by removing the call to local function resolver_ahmed_fixed(document_path, image_path, fallback)
-- I had to set my own vault root, but you
-- shouldn't need this line yourself
-- vim.env.NOTES_DIR = "/home/fic/second-brain"
local working_dir = vim.fn.getcwd()
local notes_dir = vim.fn.expand(vim.env.NOTES_DIR)
-- Format image path for Obsidian notes
if working_dir:find(notes_dir, 1, true) then
return (notes_dir .. "/" .. image_path)
end
-- Fallback to the default behavior
return fallback(document_path, image_path)
end With this I was able to get your function to work with the environment variable. |
Better Obsidian.nvim IntegrationIf you are like me, you often open Neovim in some other directory like The following solution works for me: local function resolver(document_path, image_path, fallback)
local vault_dir = "path/to/your/vault"
-- Format path for Obsidian vault
if document_path:find(vault_dir, 1, true) then
return vault_dir .. "/" .. image_path
end
-- fallback to default
return fallback(document_path, image_path)
end Simply checking whether the vault path is inside the |
@ficcdaf thanks, I tried your fix but it still doesn't work. I don't use |
I found the way to make it work for me, you can try it. Here's the snipppet: config = function()
require("image").setup({
backend = "kitty",
integrations = {
markdown = {
enabled = true,
clear_in_insert_mode = false,
download_remote_images = true,
only_render_image_at_cursor = true,
filetypes = { "markdown", "vimwiki" }, -- markdown extensions (ie. quarto) can go here
-- Custom function to resolve image paths
resolve_image_path = function(document_path, image_path, fallback)
-- Define the absolute path to your Assets directory
local assets_dir = vim.fn.expand("~/path/to/your/images/dir") -- not the path to vault, but to the assets dir
-- Check if the image_path is already an absolute path
if image_path:match("^/") then
-- If it's an absolute path, leave it unchanged
return image_path
end
-- Construct the new image path by prepending the Assets directory
local new_image_path = assets_dir .. "/" .. image_path
-- Check if the constructed path exists
if vim.fn.filereadable(new_image_path) == 1 then
return new_image_path
else
-- If the file doesn't exist in Assets, fallback to default behavior
return fallback(document_path, image_path)
end
end,
}, |
Sorry cos this is closed, I do it like this.. given that it is about obsidian integration, if you setup obsidian attachments like: -- Specify how to handle attachments.
attachments = {
img_folder = "assets/imgs",
---@param client obsidian.Client
---@param path obsidian.Path the absolute path to the image file
---@return string
img_text_func = function(client, path)
path = client:vault_relative_path(path) or path
return string.format("![%s](%s)", path.name, path)
end,
}, you can use the command ObsidianImgPaste to paste images from clipboard. and it will create a link like setup before in img_text_func. and you could use this configuration in image: require("image").setup({
backend = "kitty",
kitty_method = "normal",
integrations = {
markdown = {
enabled = true,
resolve_image_path = function(document_path, image_path, fallback)
local obsidian_client = require("obsidian").get_client()
local new_image_path = obsidian_client:vault_relative_path(image_path).filename
if vim.fn.filereadable(new_image_path) == 1 then
return new_image_path
else
return fallback(document_path, image_path)
end
end, so if you change the assets directory in obsidian it keeps the integration with image. (you can add some checks on the require, in my case obsidian is loaded on markdown files so it just works) |
@marcocofano I had to change the integration, the local full_path = vault_img_folder .. "/" .. image_path
local new_image_path = obsidian_client:vault_relative_path(full_path).filename
vim.notify("new image path " .. new_image_path) |
For anyone needing to format Obsidian links that have size properties set (if you use the Image Converter plugin these are set): resolve_image_path = function(document_path, image_path, fallback)
local working_dir = vim.fn.getcwd()
-- Format image path for Obsidian notes
local obsidian_client = require("obsidian").get_client()
if working_dir:find(obsidian_client:vault_root().filename) then
if image_path:find("|") then
image_path = vim.split(image_path, "|")[1]
end
local assets_dir = "assets"
local result =
string.format("%s/%s/%s", obsidian_client:vault_root().filename, assets_dir, image_path)
return result
end
-- Fallback to the default behavior
return fallback(document_path, image_path)
end, |
Hi, thank you for all your work in getting images inside of Neovim. I am really excited to try and get this working.
I am able to use the minimal setup and also configured my setup to work with this style image:
Inside of my ObsidianVault, I have an Attachments folder at the root where all of my images are stored.
How can I configure image.nvim to look in this directory when it sees something like this?
![[Test.png]]
I am probably missing something obvious or unsure how to configure
resolve_image_path
to make this work.Thank you again for your time and work on this plugin!
The text was updated successfully, but these errors were encountered: