Skip to content

Commit

Permalink
feat: add configuration for insert_file_link title
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed May 21, 2024
1 parent 92c1c60 commit dd07fc9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
6 changes: 6 additions & 0 deletions lua/neorg/modules/core/integrations/telescope/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
63 changes: 43 additions & 20 deletions lua/telescope/_extensions/neorg/insert_file_link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")

Expand All @@ -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 = {
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit dd07fc9

Please sign in to comment.