Fix #39 : new option subdirs_in_links = false. Defaults to true,

though.
This commit is contained in:
Rene Schallner
2021-12-13 10:20:54 +01:00
parent e84ca9615f
commit ac10a1d1b3

View File

@@ -61,6 +61,10 @@ M.Cfg = {
-- markdown: ![](image_subdir/xxxxx.png) -- markdown: ![](image_subdir/xxxxx.png)
image_link_style = "markdown", image_link_style = "markdown",
-- when linking to a note in subdir/, create a [[subdir/title]] link
-- instead of a [[title only]] link
subdirs_in_links = true,
-- integrate with calendar-vim -- integrate with calendar-vim
plug_into_calendar = true, plug_into_calendar = true,
calendar_opts = { calendar_opts = {
@@ -270,9 +274,12 @@ local function num_path_elems(p)
return #vim.split(p, "/") return #vim.split(p, "/")
end end
local function path_to_linkname(p) local function path_to_linkname(p, opts)
local ln local ln
opts = opts or {}
opts.subdirs_in_links = opts.subdirs_in_links or M.Cfg.subdirs_in_links
local special_dir = false local special_dir = false
if if
M.Cfg.dailies M.Cfg.dailies
@@ -294,6 +301,13 @@ local function path_to_linkname(p)
ln = p:gsub(M.Cfg.home .. "/", "") ln = p:gsub(M.Cfg.home .. "/", "")
end end
if not opts.subdirs_in_links then
-- strip all subdirs
local pp = Path:new(ln)
local splits = pp:_split()
ln = splits[#splits]
end
local title = vim.split(ln, M.Cfg.extension) local title = vim.split(ln, M.Cfg.extension)
title = title[1] title = title[1]
return title return title
@@ -547,10 +561,12 @@ function picker_actions.yank_tag(opts)
end end
function picker_actions.paste_link(opts) function picker_actions.paste_link(opts)
opts = opts or {}
opts.subdirs_in_links = opts.subdirs_in_links or M.Cfg.subdirs_in_links
return function(prompt_bufnr) return function(prompt_bufnr)
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
local fn = path_to_linkname(selection.value) local fn = path_to_linkname(selection.value, opts)
local title = "[[" .. fn .. "]]" local title = "[[" .. fn .. "]]"
vim.api.nvim_put({ title }, "", true, true) vim.api.nvim_put({ title }, "", true, true)
if opts.insert_after_inserting or opts.i then if opts.insert_after_inserting or opts.i then
@@ -562,11 +578,12 @@ end
function picker_actions.yank_link(opts) function picker_actions.yank_link(opts)
return function(prompt_bufnr) return function(prompt_bufnr)
opts = opts or {} opts = opts or {}
opts.subdirs_in_links = opts.subdirs_in_links or M.Cfg.subdirs_in_links
if opts.close_after_yanking then if opts.close_after_yanking then
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
end end
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
local fn = path_to_linkname(selection.value) local fn = path_to_linkname(selection.value, opts)
local title = "[[" .. fn .. "]]" local title = "[[" .. fn .. "]]"
vim.fn.setreg('"', title) vim.fn.setreg('"', title)
print("yanked " .. title) print("yanked " .. title)
@@ -704,6 +721,8 @@ local function InsertLink(opts)
or M.Cfg.insert_after_inserting or M.Cfg.insert_after_inserting
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
opts.subdirs_in_links = opts.subdirs_in_links or M.Cfg.subdirs_in_links
find_files_sorted({ find_files_sorted({
prompt_title = "Insert link to note", prompt_title = "Insert link to note",
cwd = M.Cfg.home, cwd = M.Cfg.home,
@@ -711,7 +730,7 @@ local function InsertLink(opts)
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
local fn = path_to_linkname(selection.value) local fn = path_to_linkname(selection.value, opts)
vim.api.nvim_put({ "[[" .. fn .. "]]" }, "", true, true) vim.api.nvim_put({ "[[" .. fn .. "]]" }, "", true, true)
if opts.i then if opts.i then
vim.api.nvim_feedkeys("A", "m", false) vim.api.nvim_feedkeys("A", "m", false)
@@ -1365,7 +1384,7 @@ end
-- Create and yank a [[link]] from the current note. -- Create and yank a [[link]] from the current note.
-- --
local function YankLink() local function YankLink()
local title = "[[" .. path_to_linkname(vim.fn.expand("%:p")) .. "]]" local title = "[[" .. path_to_linkname(vim.fn.expand("%:p"), M.Cfg) .. "]]"
vim.fn.setreg('"', title) vim.fn.setreg('"', title)
print("yanked " .. title) print("yanked " .. title)
end end
@@ -1551,7 +1570,7 @@ local function ShowBacklinks(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
local title = path_to_linkname(vim.fn.expand("%:p")) local title = path_to_linkname(vim.fn.expand("%:p"), M.Cfg)
-- or vim.api.nvim_buf_get_name(0) -- or vim.api.nvim_buf_get_name(0)
builtin.live_grep({ builtin.live_grep({
results_title = "Backlinks to " .. title, results_title = "Backlinks to " .. title,