Skip to content

Commit

Permalink
Rename ObsidianAliases to ObsidianTitles
Browse files Browse the repository at this point in the history
  • Loading branch information
xulongwu4 committed Oct 17, 2024
1 parent fcbf8b7 commit b7ce310
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 68 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added `opts.follow_img_func` option for customizing how to handle image paths.
- Added better handling for undefined template fields, which will now be prompted for.
- Added `:ObsidianAliases` command.
- Added `:ObsidianTitles` command.

### Changed

Expand Down
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.

- `:ObsidianAliases` for getting a picker list of aliases of all notes in the vault.
- `:ObsidianTitles` for getting a picker list of titles and aliases of all 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
4 changes: 2 additions & 2 deletions doc/obsidian.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ COMMANDS *obsidian-commands*
buffer.
- `:ObsidianTags [TAG ...]` for getting a picker list of all occurrences of the
given tags.
- `:ObsidianAliases` for getting a picker list of aliases of all notes in the
vault.
- `:ObsidianTitles` for getting a picker list of titles and aliases of all 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
Expand Down
62 changes: 0 additions & 62 deletions lua/obsidian/commands/aliases.lua

This file was deleted.

4 changes: 2 additions & 2 deletions lua/obsidian/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local command_lookups = {
ObsidianBacklinks = "obsidian.commands.backlinks",
ObsidianSearch = "obsidian.commands.search",
ObsidianTags = "obsidian.commands.tags",
ObsidianAliases = "obsidian.commands.aliases",
ObsidianTitles = "obsidian.commands.titles",
ObsidianTemplate = "obsidian.commands.template",
ObsidianNewFromTemplate = "obsidian.commands.new_from_template",
ObsidianQuickSwitch = "obsidian.commands.quick_switch",
Expand Down Expand Up @@ -150,7 +150,7 @@ M.register("ObsidianBacklinks", { opts = { nargs = 0, desc = "Collect backlinks"

M.register("ObsidianTags", { opts = { nargs = "*", range = true, desc = "Find tags" } })

M.register("ObsidianAliases", { opts = { nargs = "*", range = true, desc = "Find aliases" } })
M.register("ObsidianTitles", { opts = { nargs = 0, desc = "Collect titles and aliases" } })

M.register("ObsidianSearch", { opts = { nargs = "?", desc = "Search vault" } })

Expand Down
74 changes: 74 additions & 0 deletions lua/obsidian/commands/titles.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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)
local title_to_notes = {}
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)
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)
end
end
end

for title, notes_share_title in pairs(title_to_notes) do
title_to_notes[title] = util.tbl_unique(notes_share_title)
end

return title_to_notes
end

---@param client obsidian.Client
return function(client)
local picker = client:picker()
if not picker then
log.err "No picker configured"
return
end

client:find_notes_async("", function(notes)
local title_to_notes = map_title_to_notes(notes)
vim.schedule(function()
picker:pick(vim.tbl_keys(title_to_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(value)
util.open_buffer(value.path)
end,
})
end)
end
end,
})
end)
end)
end

0 comments on commit b7ce310

Please sign in to comment.