mkdnflow.nvim
mkdnflow.nvim copied to clipboard
Is there a way to autocomplete links to headers?
I often find myself linking to headers and it's hard to remember full text of the headers in some cases so I find myself jumping back and forth between files to copy the exact text. It would be great if we there's a way to get autocompletion suggestions for headers.
Hi, thanks for the issue. I agree that completions for headers would be nice, although I'll have to do a bit of learning to figure out how to implement that. In the mean time, are you aware of the ya
("yank anchor") mapping built into the plug-in? With your cursor on the heading, it will add a formatted anchor link to the register, and you can then go and paste that link wherever you like with p
. Then if you want to paste an anchor link for the subsequent heading, you can do ''
and you'll be taken back to the header you last copied, where you can do ]]
to go to the subsequent heading, do ya
again, and so on and so forth.
I wasn't aware of how "yank anchor" works. This is really useful. Thanks.
Meanwhile, I am working on adding auto-completion for anchors using ctags. I am half way through the implementation and I think it would work. But I may be wrong. Will share my findings here either way.
@krankur you can use the telescope, but you will need to create a custom action after pressing Enter on whatever note you find to insert the link in the format you want
@krankur
in this code you can find any line you want from files in the directory where you call the function (if I understand in right), in your case, as I understand you need # something like this
when you press Enter
you will create link with anchor (and I'm sorry, because I don't actually know which syntax you need in your notes, so you can change it in selection
variable)
local previewers = require("telescope.previewers")
local pickers = require("telescope.pickers")
local sorters = require("telescope.sorters")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
local make_entry = require "telescope.make_entry"
local M = {}
function M.test()
opts = opts or {}
local vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments
local search_dirs = opts.search_dirs
local grep_open_files = opts.grep_open_files
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
local live_grepper = finders.new_job(function(prompt)
-- TODO: Probably could add some options for smart case and whatever else rg offers.
if not prompt or prompt == "" then
return nil
end
local search_list = {}
if grep_open_files then
search_list = filelist
elseif search_dirs then
search_list = search_dirs
end
return flatten { vimgrep_arguments, additional_args, "--", prompt, search_list }
end, opts.entry_maker or make_entry.gen_from_vimgrep(opts), opts.max_results, opts.cwd)
pickers
.new(opts, {
prompt_title = "Live Grep",
finder = live_grepper,
previewer = conf.grep_previewer(opts),
sorter = sorters.highlighter_only(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
print(vim.inspect(selection)) -- for debugging and understanding what you get from picker
-- you need to add transform to lowercase
-- and regex spaces to dashes
-- and hastags to one
-- and maybe promt to write link field
selection = "[link](" .. selection.filename .. selection.text .. ")"
vim.api.nvim_put({ selection }, "", false, true)
end)
return true
end,
})
:find()
end
return M