diff --git a/lua/neorg/modules/core/integrations/telescope/module.lua b/lua/neorg/modules/core/integrations/telescope/module.lua index afe51f2..2d77b7a 100644 --- a/lua/neorg/modules/core/integrations/telescope/module.lua +++ b/lua/neorg/modules/core/integrations/telescope/module.lua @@ -33,6 +33,12 @@ module.load = function() }) end +module.config.public = { + insert_file_link = { + show_title_preview = true, + }, +} + module.public = { find_linkable = require("telescope._extensions.neorg.find_linkable"), find_norg_files = require("telescope._extensions.neorg.find_norg_files"), diff --git a/lua/telescope/_extensions/neorg/insert_file_link.lua b/lua/telescope/_extensions/neorg/insert_file_link.lua index 982f44b..0d1ff05 100644 --- a/lua/telescope/_extensions/neorg/insert_file_link.lua +++ b/lua/telescope/_extensions/neorg/insert_file_link.lua @@ -10,7 +10,7 @@ local neorg_loaded, neorg = pcall(require, "neorg.core") assert(neorg_loaded, "Neorg is not loaded - please make sure to load Neorg first") --- Get a list of all norg files in current workspace. Returns { workspace_path, norg_files } ---- @return table +--- @return table|nil local function get_norg_files() local dirman = neorg.modules.get_module("core.dirman") @@ -25,9 +25,31 @@ local function get_norg_files() return { current_workspace[2], norg_files } end +--- Get the title set in the metadata block of file +--- @param file string +--- @return string? +local function get_file_title(file) + local dirman = neorg.modules.get_module("core.dirman") + if not dirman then + return nil + end + + local ts = neorg.modules.get_module("core.integrations.treesitter") + if not ts then + return nil + end + + local bufnr = dirman.get_file_bufnr(tostring(file)) + local metadata = ts.get_document_metadata(bufnr) + if not metadata or not metadata.title then + return nil + end + return metadata.title +end + --- Generate links for telescope ---- @return table -local function generate_links() +--- @return table|nil +local function generate_links(preview) local res = {} local dirman = neorg.modules.get_module("core.dirman") @@ -37,30 +59,26 @@ local function generate_links() local files = get_norg_files() - if not files[2] then - return + if not (files and files[2]) then + return nil end if not (pcall(require, "pathlib")) then error("neorg-telescope Dependency Error: pysan3/pathlib.nvim is a required dependency.") end - local ts = neorg.modules.get_module("core.integrations.treesitter") - local Path = require("pathlib") for _, file in pairs(files[2]) do local bufnr = dirman.get_file_bufnr(tostring(file)) - - local title = nil - local title_display = "" - if ts then - local metadata = ts.get_document_metadata(bufnr) - if metadata and metadata.title then - title = metadata.title - title_display = " [" .. title .. "]" + if vim.api.nvim_get_current_buf() ~= bufnr then + local title = nil + local title_display = "" + if preview then + title = get_file_title(file) + if title then + title_display = " [" .. title .. "]" + end end - end - if vim.api.nvim_get_current_buf() ~= bufnr then file = Path(file) local relative = file:relative_to(Path(files[1])) local links = { @@ -78,20 +96,21 @@ end return function(opts) opts = opts or {} - local mode = vim.api.nvim_get_mode().mode + local config = require("neorg").modules.get_module_config("core.integrations.telescope").insert_file_link pickers .new(opts, { prompt_title = "Insert Link to Neorg File", results_title = "Linkables", finder = finders.new_table({ - results = generate_links(), + results = generate_links(config.show_title_preview), entry_maker = function(entry) return { value = entry, display = entry.display, ordinal = entry.display, relative = entry.relative, + file = entry.file, title = entry.title, } end, @@ -104,9 +123,13 @@ return function(opts) local entry = state.get_selected_entry() actions.close(prompt_bufnr) + local title = entry.title + if not config.show_title_preview then + title = get_file_title(entry.file) + end vim.api.nvim_put({ - "{" .. ":$/" .. entry.relative .. ":" .. "}" .. "[" .. (entry.title or entry.relative) .. "]", + "{" .. ":$/" .. entry.relative .. ":" .. "}" .. "[" .. (title or entry.relative) .. "]", }, "c", false, true) vim.api.nvim_feedkeys("hf]a", "t", false) end)