diff --git a/home/deepak/home.nix b/home/deepak/home.nix index 227434a..4051119 100644 --- a/home/deepak/home.nix +++ b/home/deepak/home.nix @@ -61,12 +61,14 @@ ctrlp-vim # lsp stuff nvim-lspconfig + wiki-vim ]; extraConfig = '' inoremap jj inoremap kk lua << EOF require'lspconfig'.nil_ls.setup{} + ${builtins.readFile ./neovim/wiki-vim.lua} ''; }; diff --git a/home/deepak/neovim/wiki-vim.lua b/home/deepak/neovim/wiki-vim.lua new file mode 100644 index 0000000..94e8e81 --- /dev/null +++ b/home/deepak/neovim/wiki-vim.lua @@ -0,0 +1,54 @@ +-- vim.g.wiki_root = "/path/to/obsidian/vault" +vim.g.wiki_root = "~/wiki" + +local function find_wiki_path_for_file(filename) + -- recursively search for the file name in the wiki_root using ripgrep + local rg_result_pipe = assert(io.popen(string.format( + "rg -g '%s' --files \"%s\"", + filename, + vim.g.wiki_root + ))) + local rg_result = rg_result_pipe:read("*line") + rg_result_pipe:close() + + -- if ripgrep found a result, return that + if rg_result then return rg_result end + + -- if it didn't find a result, the file does not exist; + -- in that case, the link will point to the (not yet existing) + -- corresponding file in the wiki_root + if vim.g.wiki_root:sub(-1) == "/" then + return vim.g.wiki_root .. filename + else + return vim.g.wiki_root .. "/" .. filename + end +end + +local function resolve_wiki_link(url) + local components = {} + for element in (url.stripped .. "#"):gmatch("([^#]*)#") do + table.insert(components, element) + end + + local filename = components[1] + url.anchor = components[2] or "" + + -- 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 + -- 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; + url.path = find_wiki_path_for_file(filename) + else + -- 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 + end + + return url +end + +vim.g.wiki_link_schemes = { + wiki = { resolver = resolve_wiki_link, }, +}