Skip to content

Commit e53072a

Browse files
authored
Add :ObsidianTOC command (#646)
* Add `:ObsidianTOC` command See #635 * fix docs workflow
1 parent e170641 commit e53072a

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

.github/workflows/docs.yml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ env:
1616
runtime: ~/.local/share/nvim/site/pack/vendor/start
1717
minidoc-git: https://github.com/echasnovski/mini.doc
1818
minidoc-path: ~/.local/share/nvim/site/pack/vendor/start/mini.doc
19+
nvim_url: https://github.com/neovim/neovim/releases/download/nightly/nvim-linux64.tar.gz
1920

2021
jobs:
2122
docs:
@@ -32,22 +33,11 @@ jobs:
3233
if: github.event_name == 'pull_request'
3334
uses: actions/checkout@v4
3435

35-
- run: date +%F > /tmp/todays-date
36-
37-
- name: Restore cache for today's nightly
38-
uses: actions/cache@v4
39-
with:
40-
path: |
41-
_neovim
42-
key: ${{ runner.os }}-${{ hashFiles('/tmp/todays-date') }}
43-
4436
- name: Install neovim and dependencies
4537
run: |
4638
mkdir -p ${{ env.runtime }}
47-
test -d _neovim || {
48-
mkdir -p _neovim
49-
curl -sL ${{ matrix.nvim_url }} | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
50-
}
39+
mkdir -p _neovim
40+
curl -sL ${{ env.nvim_url }} | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
5141
git clone --depth 1 ${{ env.minidoc-git }} ${{ env.minidoc-path }}
5242
ln -s $(pwd) ${{ env.runtime }}
5343

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Added
11+
12+
- Added `:ObsidianTOC` command for loading the table of contents of the current note into a picker list.
13+
1014
## [v3.8.1](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.8.1) - 2024-06-26
1115

1216
### Fixed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ _Keep in mind this plugin is not meant to replace Obsidian, but to complement it
8686

8787
- `:ObsidianToggleCheckbox` to cycle through checkbox options.
8888

89+
- `:ObsidianTOC` to load the table of contents of the current note into a picker list.
90+
8991
### Demo
9092

9193
[![2024-01-31 14 22 52](https://github.com/epwalsh/obsidian.nvim/assets/8812459/2986e1d2-13e8-40e2-9c9e-75691a3b662e)](https://github.com/epwalsh/obsidian.nvim/assets/8812459/2986e1d2-13e8-40e2-9c9e-75691a3b662e)

lua/obsidian/commands/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ local command_lookups = {
2424
ObsidianPasteImg = "obsidian.commands.paste_img",
2525
ObsidianExtractNote = "obsidian.commands.extract_note",
2626
ObsidianDebug = "obsidian.commands.debug",
27+
ObsidianTOC = "obsidian.commands.toc",
2728
}
2829

2930
local M = setmetatable({
@@ -185,4 +186,6 @@ M.register(
185186

186187
M.register("ObsidianDebug", { opts = { nargs = 0, desc = "Log some information for debugging" } })
187188

189+
M.register("ObsidianTOC", { opts = { nargs = 0, desc = "Load the table of contents into a picker" } })
190+
188191
return M

lua/obsidian/commands/toc.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
local util = require "obsidian.util"
2+
3+
---@param client obsidian.Client
4+
return function(client, _)
5+
local note = assert(client:current_note(0, { collect_anchor_links = true }))
6+
7+
---@type obsidian.PickerEntry[]
8+
local picker_entries = {}
9+
for _, anchor in pairs(note.anchor_links) do
10+
local display = string.rep("#", anchor.level) .. " " .. anchor.header
11+
table.insert(
12+
picker_entries,
13+
{ value = display, display = display, filename = tostring(note.path), lnum = anchor.line }
14+
)
15+
end
16+
17+
-- De-duplicate and sort.
18+
picker_entries = util.tbl_unique(picker_entries)
19+
table.sort(picker_entries, function(a, b)
20+
return a.lnum < b.lnum
21+
end)
22+
23+
local picker = assert(client:picker())
24+
picker:pick(picker_entries, { prompt_title = "Table of Contents" })
25+
end

lua/obsidian/util.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local util = {}
1616
---@return boolean
1717
util.tbl_contains = function(table, val)
1818
for i = 1, #table do
19-
if table[i] == val then
19+
if vim.deep_equal(table[i], val) then
2020
return true
2121
end
2222
end

0 commit comments

Comments
 (0)