Skip to content

Commit

Permalink
Use metadata title instead of file name when inserting file link (#45)
Browse files Browse the repository at this point in the history
- It's more readable and reasonable to display title in link.
- Only apply this behavior when treesitter and metadata title are
  available.
- Provide the telescope fuzzy search on title as well.

Co-authored-by: Kait Wang <sibevin@gmail.com>
  • Loading branch information
sibevin and Kait Wang authored Nov 12, 2023
1 parent c3d25cc commit 6a7a677
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lua/telescope/_extensions/neorg/insert_file_link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,27 @@ local function generate_links()
return
end

local ts = neorg.modules.get_module("core.integrations.treesitter")

for _, file in pairs(files[2]) do
local bufnr = dirman.get_file_bufnr(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 .. "]"
end
end

if vim.api.nvim_get_current_buf() ~= bufnr then
local links = {
file = file,
display = "$" .. file:sub(#files[1] + 1, -1),
display = "$" .. file:sub(#files[1] + 1, -1) .. title_display,
relative = file:sub(#files[1] + 1, -1):sub(0, -6),
title = title,
}
table.insert(res, links)
end
Expand All @@ -72,6 +85,7 @@ return function(opts)
display = entry.display,
ordinal = entry.display,
relative = entry.relative,
title = entry.title,
}
end,
}),
Expand All @@ -95,7 +109,7 @@ return function(opts)
local file_name, _ = path_no_extension:gsub(".*%/", "")

vim.api.nvim_put({
"{" .. ":$" .. entry.relative .. ":" .. "}" .. "[" .. file_name .. "]",
"{" .. ":$" .. entry.relative .. ":" .. "}" .. "[" .. (entry.title or file_name) .. "]",
}, "c", false, true)
vim.api.nvim_feedkeys("hf]a", "t", false)
end)
Expand Down

0 comments on commit 6a7a677

Please sign in to comment.