diff --git a/CHANGELOG.md b/CHANGELOG.md index b585ed0e2..8085c2f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +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 `:ObsidianTitles` command. ### Changed diff --git a/README.md b/README.md index d5d382b62..8f1d0fd1a 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ _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 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. - `:ObsidianYesterday` to open/create the daily note for the previous working day. diff --git a/doc/obsidian.txt b/doc/obsidian.txt index fc0539948..fb7af820c 100644 --- a/doc/obsidian.txt +++ b/doc/obsidian.txt @@ -76,6 +76,8 @@ 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 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 diff --git a/lua/obsidian/commands/init.lua b/lua/obsidian/commands/init.lua index f31eaf8ea..9b06d0121 100644 --- a/lua/obsidian/commands/init.lua +++ b/lua/obsidian/commands/init.lua @@ -13,6 +13,7 @@ local command_lookups = { ObsidianBacklinks = "obsidian.commands.backlinks", ObsidianSearch = "obsidian.commands.search", ObsidianTags = "obsidian.commands.tags", + ObsidianTitles = "obsidian.commands.titles", ObsidianTemplate = "obsidian.commands.template", ObsidianNewFromTemplate = "obsidian.commands.new_from_template", ObsidianQuickSwitch = "obsidian.commands.quick_switch", @@ -149,6 +150,8 @@ M.register("ObsidianBacklinks", { opts = { nargs = 0, desc = "Collect backlinks" M.register("ObsidianTags", { opts = { nargs = "*", range = true, desc = "Find tags" } }) +M.register("ObsidianTitles", { opts = { nargs = 0, desc = "Collect titles and aliases" } }) + M.register("ObsidianSearch", { opts = { nargs = "?", desc = "Search vault" } }) M.register("ObsidianTemplate", { opts = { nargs = "?", desc = "Insert a template" } }) diff --git a/lua/obsidian/commands/titles.lua b/lua/obsidian/commands/titles.lua new file mode 100644 index 000000000..ccaca0b01 --- /dev/null +++ b/lua/obsidian/commands/titles.lua @@ -0,0 +1,59 @@ +local log = require "obsidian.log" +local util = require "obsidian.util" + +---@param notes obsidian.Note[] +--- +---@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 + entries[#entries + 1] = { + value = note, + display = title, + ordinal = note:display_name() .. " " .. title, + filename = tostring(note.path), + } + end + + for _, alias in ipairs(note.aliases) do + if alias ~= title then + entries[#entries + 1] = { + value = note, + display = alias, + ordinal = note:display_name() .. " " .. alias, + filename = tostring(note.path), + } + end + end + end + + return entries +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) + vim.schedule(function() + picker:pick(convert_notes_to_picker_entries(notes), { + prompt_title = "Titles", + callback = function(...) + for _, note in ipairs { ... } do + util.open_buffer(note.path) + end + end, + allow_multiple = true, + }) + end) + end) +end