From 9a75cd3e897ea76b45e9614d9f01c3a1866cf42d Mon Sep 17 00:00:00 2001 From: Longwu Ou Date: Fri, 18 Oct 2024 21:55:54 -0400 Subject: [PATCH] Some simplification --- README.md | 2 +- doc/obsidian.txt | 2 +- lua/obsidian/commands/titles.lua | 72 +++++++++++--------------------- 3 files changed, 26 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 7bf5613b1..8f1d0fd1a 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ _Keep in mind this plugin is not meant to replace Obsidian, but to complement it - `:ObsidianTags [TAG ...]` for getting a picker list of all occurrences of the given tags. -- `:ObsidianTitles` for getting a picker list of titles and aliases of all notes in the vault. +- `:ObsidianTitles` for getting a picker list of all titles and aliases of notes in the vault. - `:ObsidianToday [OFFSET]` to open/create a new daily note. This command also takes an optional offset in days, e.g. use `:ObsidianToday -1` to go to yesterday's note. Unlike `:ObsidianYesterday` and `:ObsidianTomorrow` this command does not differentiate between weekdays and weekends. diff --git a/doc/obsidian.txt b/doc/obsidian.txt index 60a0d0b5b..fb7af820c 100644 --- a/doc/obsidian.txt +++ b/doc/obsidian.txt @@ -76,7 +76,7 @@ COMMANDS *obsidian-commands* buffer. - `:ObsidianTags [TAG ...]` for getting a picker list of all occurrences of the given tags. -- `:ObsidianTitles` for getting a picker list of titles and aliases of all notes +- `:ObsidianTitles` for getting a picker list of all titles and aliases of notes in the vault. - `:ObsidianToday [OFFSET]` to open/create a new daily note. This command also takes an optional offset in days, e.g. use `:ObsidianToday -1` to go to diff --git a/lua/obsidian/commands/titles.lua b/lua/obsidian/commands/titles.lua index 8fa19699d..a64457824 100644 --- a/lua/obsidian/commands/titles.lua +++ b/lua/obsidian/commands/titles.lua @@ -1,39 +1,38 @@ local log = require "obsidian.log" local util = require "obsidian.util" ----@param note obsidian.Note ---- ----@return obsidian.PickerEntry -local function convert_note_to_picker_entry(note) - return { - value = note, - display = note:display_name(), - ordinal = note:display_name(), - filename = tostring(note.path), - } -end - ---@param notes obsidian.Note[] --- ----@return table -local function map_title_to_notes(notes) - ---@type table - local title_to_notes = {} +---@return obsidian.PickerEntry[] +local function convert_notes_to_picker_entries(notes) + ---@type obsidian.PickerEntry[] + local entries = {} + for _, note in ipairs(notes) do local title = note.title + if title then - title_to_notes[title] = title_to_notes[title] or {} - table.insert(title_to_notes[title], note) + entries[#entries + 1] = { + value = note, + display = title, + ordinal = note:display_name(), + filename = tostring(note.path), + } end + for _, alias in ipairs(note.aliases) do if alias ~= title then - title_to_notes[alias] = title_to_notes[alias] or {} - table.insert(title_to_notes[alias], note) + entries[#entries + 1] = { + value = note, + display = alias, + ordinal = note:display_name(), + filename = tostring(note.path), + } end end end - return title_to_notes + return entries end ---@param client obsidian.Client @@ -45,36 +44,13 @@ return function(client) end client:find_notes_async("", function(notes) - local title_to_notes = map_title_to_notes(notes) - ---@type obsidian.PickerEntry[] - local items = {} - for title, notes_with_title in pairs(title_to_notes) do - table.insert(items, { - value = title, - display = title, - ordinal = title, - filename = tostring(notes_with_title[1].path), - }) - end vim.schedule(function() - picker:pick(items, { + picker:pick(convert_notes_to_picker_entries(notes), { prompt_title = "Titles", - callback = function(title) - local selected_notes = title_to_notes[title] - if #selected_notes == 1 then - util.open_buffer(selected_notes[1].path) - else - local entries = vim.tbl_map(convert_note_to_picker_entry, selected_notes) - vim.schedule(function() - picker:pick(entries, { - prompt_title = title, - callback = function(note) - util.open_buffer(note.path) - end, - }) - end) - end + callback = function(note) + util.open_buffer(note.path) end, + allow_multiple = true, }) end) end)