dotfiles/nvim/lua/plugins/vimwiki.lua

70 lines
2.2 KiB
Lua

local pickers = require("telescope.pickers")
local actions = require "telescope.actions"
local action_state = require("telescope.actions.state")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local root_path = '/home/user/Workspace/DnD/Lumentis Campaign Setting'
local function get_title(file)
for line in io.lines(file) do
line = string.gsub(line, "#", "")
line = string.gsub(line, "^%s*", "")
line = string.gsub(line, "%s*$", "")
return line
end
end
local function choose_link(opts, titles, links)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Choose a file",
finder = finders.new_table {
results = titles
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
vim.api.nvim_put({ links[selection['index']] }, "", false, true)
end)
return true
end,
}):find()
end
local function insert_link()
local scan = require("plenary.scandir").scan_dir(root_path)
local titles = {}
local links = {}
for _, v in ipairs(scan) do
if v:match("^.+(%..+)$") == ".md" then
local absolute_filename = string.sub(v, root_path:len()+1, v:len())
local title = get_title(v)
if title:len() > 0 then
local link = "[" .. title .. "](" .. absolute_filename .. ")"
table.insert(titles, title)
table.insert(links, link)
end
end
end
choose_link(require("telescope.themes").get_dropdown{}, titles, links)
end
return {
'vimwiki/vimwiki',
dependencies = {'nvim-lua/plenary.nvim', 'nvim-telescope/telescope.nvim'},
init = function ()
vim.g.vimwiki_list = {{path = '~/Workspace/DnD/Lumentis Campaign Setting/', syntax = 'markdown', ext = '.md'}}
vim.g.vimwiki_key_mappings = { table_mappings = 0 }
vim.g.vimwiki_markdown_link_ext = 1
vim.g.vimwiki_global_ext = 0
end,
lazy = false,
keys = {
{"<leader>l", insert_link},
{"<leader>l", insert_link, mode="i"}
}
}