feat: adds wiki vim config with obsidian style link resolution
All checks were successful
gitea-deepak/nixconf/pipeline/head This commit looks good

This commit is contained in:
Deepak Mallubhotla 2023-09-27 16:00:25 -05:00
parent b673398888
commit 6263a9f3c3
Signed by: deepak
GPG Key ID: 976F3357369149AB
2 changed files with 27 additions and 4 deletions

View File

@ -69,6 +69,8 @@
lua << EOF
require'lspconfig'.nil_ls.setup{}
${builtins.readFile ./neovim/wiki-vim.lua}
EOF
'';
};

View File

@ -1,5 +1,4 @@
-- vim.g.wiki_root = "/path/to/obsidian/vault"
vim.g.wiki_root = "~/wiki"
vim.g.wiki_root = "/home/deepak/wiki"
local function find_wiki_path_for_file(filename)
-- recursively search for the file name in the wiki_root using ripgrep
@ -26,29 +25,51 @@ end
local function resolve_wiki_link(url)
local components = {}
-- print(vim.inspect(url))
for element in (url.stripped .. "#"):gmatch("([^#]*)#") do
table.insert(components, element)
end
-- print(vim.inspect(components))
-- print("spot A")
local filename = components[1]
url.anchor = components[2] or ""
-- print("spot B")
-- infer the .md file extension
if filename:sub(-3) ~= ".md" then filename = filename .. ".md" end
if url.origin:sub(1, #vim.g.wiki_root) == vim.g.wiki_root then
-- print("spot C")
-- print(filename)
if (url.origin:sub(1, #vim.g.wiki_root) == vim.g.wiki_root) or url.origin == '' then
-- print("in the wiki root start block")
--
-- if the "origin" (the file that contains the link) is in the wiki_root,
-- the wiki_root directory is recursively searched for the file name;
--
-- an empty origin, like you'd get from <leader>ww, is also mapped to this block
url.path = find_wiki_path_for_file(filename)
-- print("in the wiki root")
else
-- print("not in the wiki root start block")
-- if the origin is not in the wiki_root,
-- fall back to only looking in the same directory as the origin
url.path = url.origin:match(".*/") .. filename
-- print("not in the wiki root")
end
-- print(vim.inspect(url))
return url
end
vim.g.wiki_link_schemes = {
wiki = { resolver = resolve_wiki_link, },
wiki = {
resolver = resolve_wiki_link,
-- handler = function(x) print(vim.inspect(x)) end,
},
}