Skip to content

Commit

Permalink
Some simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
xulongwu4 committed Oct 19, 2024
1 parent 2e8205e commit 9a75cd3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion doc/obsidian.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 24 additions & 48 deletions lua/obsidian/commands/titles.lua
Original file line number Diff line number Diff line change
@@ -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<string, obsidian.Note[]>
local function map_title_to_notes(notes)
---@type table<string, obsidian.Note[]>
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
Expand All @@ -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)
Expand Down

0 comments on commit 9a75cd3

Please sign in to comment.