Skip to content

Add the ObsidianTitles command #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions doc/obsidian.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lua/obsidian/commands/init.lua
Original file line number Diff line number Diff line change
@@ -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" } })
59 changes: 59 additions & 0 deletions lua/obsidian/commands/titles.lua
Original file line number Diff line number Diff line change
@@ -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