From e0f27677f3898e4691cd22a5a9240db3a2bdd61d Mon Sep 17 00:00:00 2001 From: Hawtian Wang Date: Sun, 3 Mar 2024 16:00:32 +0800 Subject: [PATCH] feat: previews notes in tag --- lua/dotvim/extra/obsidian/telescope.lua | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lua/dotvim/extra/obsidian/telescope.lua b/lua/dotvim/extra/obsidian/telescope.lua index e83178ef..d5131d5c 100644 --- a/lua/dotvim/extra/obsidian/telescope.lua +++ b/lua/dotvim/extra/obsidian/telescope.lua @@ -174,9 +174,31 @@ function M.find_notes_tags(opts, next_opts) local finders = require("telescope.finders") local action_state = require("telescope.actions.state") local actions = require("telescope.actions") + local previewers = require("telescope.previewers.buffer_previewer") opts = opts or {} + local previewer = previewers.new_buffer_previewer { + title = "Tag Preview", + dyn_title = function(_, entry) + return ("󰓼 %s %d"):format(entry.value.tag, #entry.value.notes) + end, + get_buffer_by_name = function(_, entry) + return entry.value.tag + end, + define_preview = function(self, entry, _) + ---@type dotvim.extra.obsidian.ObsidianNote[] + local notes = entry.value.notes + local lines = {} + for _, note in ipairs(notes) do + table.insert(lines, ("- %s"):format(note:title())) + end + + vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", "markdown") + vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines) + end, + } + pickers .new(opts, { prompt_title = "Obsidian Tags", @@ -185,7 +207,7 @@ function M.find_notes_tags(opts, next_opts) results = values, entry_maker = tag_entry_maker(opts), }, - previewer = false, + previewer = previewer, attach_mappings = function(bufnr, map) map("i", "", function() local entry = action_state.get_selected_entry()