all repos — nvim-custom-links @ 9e46ec3454b9053923ac6751c7cb674eef8fe2b8

Neovim simple plugin for custom local links

plugin/custom_links.lua (view raw)

 1-- Default paths for custom links
 2local project_base_path = vim.fn.expand("~/5ika/brain/project")
 3local area_base_path = vim.fn.expand("~/5ika/brain/area")
 4
 5-- Template paths
 6local project_template_path = vim.fn.expand("~/5ika/brain/templates/project.md")
 7local area_template_path = vim.fn.expand("~/5ika/brain/templates/area.md")
 8
 9-- Configurable prefixes for custom links
10local link_prefixes = {
11	project = "+",
12	area = "@",
13}
14
15-- Function to create a file from a template
16local function create_file_from_template(full_path, template_path)
17	local template_content = vim.fn.readfile(template_path)
18	vim.fn.writefile(template_content, full_path)
19	print("Created new file from template: " .. full_path)
20end
21
22-- Function to follow custom links
23function follow_custom_link()
24	local word = vim.fn.expand("<cWORD>")
25
26	local full_path, template_path
27
28	if string.match(word, "^" .. link_prefixes.project .. "%w+$") then
29		local project_name = string.gsub(word, "^" .. link_prefixes.project, "")
30		full_path = project_base_path .. "/" .. project_name .. ".md"
31		template_path = project_template_path
32	elseif string.match(word, "^" .. link_prefixes.area .. "%w+$") then
33		local area_name = string.gsub(word, "^" .. link_prefixes.area, "")
34		full_path = area_base_path .. "/" .. area_name .. ".md"
35		template_path = area_template_path
36	else
37		print("Not a custom link")
38		return
39	end
40
41	if vim.fn.filereadable(full_path) == 1 or vim.fn.isdirectory(full_path) == 1 then
42		vim.cmd("edit " .. full_path)
43	else
44		create_file_from_template(full_path, template_path)
45		vim.cmd("edit " .. full_path)
46	end
47end
48
49-- Key mapping to follow custom links
50vim.api.nvim_set_keymap("n", "<leader>fl", ":lua follow_custom_link()<CR>", { noremap = true, silent = true })