Skip to content
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

Fix markdown relative paths with "./" #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lua/hologram/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,24 @@ function fs.bytes2int(bufp)
return bor(lsh(bufp[0],24), lsh(bufp[1],16), lsh(bufp[2],8), bufp[3])
end

function fs.get_absolute_path(path)
if fs._is_root_path(path) then
return path
else
local folder_path = vim.fn.expand("%:p:h")
local eventual_path = folder_path .. "/" .. path
local absolute_path = vim.loop.fs_realpath(eventual_path, nil)
return absolute_path
end
end

function fs._is_root_path(path)
local first_path_char = string.sub(path, 0, 1)
if first_path_char == "/" then
return true
else
return false
end
end

return fs
28 changes: 5 additions & 23 deletions lua/hologram/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,14 @@ function hologram.find_source(line)
local inline_link = line:match('!%[.-%]%(.-%)')
if inline_link then
local source = inline_link:match('%((.+)%)')
local path = hologram._to_absolute_path(source)
if fs.check_sig_PNG(path) then
return path
local path = fs.get_absolute_path(source)
if path then
if fs.check_sig_PNG(path) then
return path
else return nil end
else return nil end
end
end
end

function hologram._to_absolute_path(path)
if hologram._is_root_path(path) then
return path
else
-- absolute_path: folder_path + relative_path
local folder_path = vim.fn.expand("%:p:h")
local absolute_path = folder_path .. "/" .. path
return absolute_path
end
end

function hologram._is_root_path(path)
local first_path_char = string.sub(path, 0, 1)
if first_path_char == "/" then
return true
else
return false
end
end

return hologram