From 6263a9f3c3130035616ba010934a7ec1cf881da1 Mon Sep 17 00:00:00 2001 From: Deepak Mallubhotla Date: Wed, 27 Sep 2023 16:00:25 -0500 Subject: [PATCH] feat: adds wiki vim config with obsidian style link resolution --- home/deepak/home.nix | 2 ++ home/deepak/neovim/wiki-vim.lua | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/home/deepak/home.nix b/home/deepak/home.nix index 4051119..41324ca 100644 --- a/home/deepak/home.nix +++ b/home/deepak/home.nix @@ -69,6 +69,8 @@ lua << EOF require'lspconfig'.nil_ls.setup{} ${builtins.readFile ./neovim/wiki-vim.lua} + + EOF ''; }; diff --git a/home/deepak/neovim/wiki-vim.lua b/home/deepak/neovim/wiki-vim.lua index 94e8e81..ba67772 100644 --- a/home/deepak/neovim/wiki-vim.lua +++ b/home/deepak/neovim/wiki-vim.lua @@ -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 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, + }, }