Skip to content

Commit

Permalink
Add :ObsidianTOC command (#646)
Browse files Browse the repository at this point in the history
* Add `:ObsidianTOC` command

See #635

* fix docs workflow
  • Loading branch information
epwalsh committed Jul 11, 2024
1 parent e170641 commit e53072a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
16 changes: 3 additions & 13 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ env:
runtime: ~/.local/share/nvim/site/pack/vendor/start
minidoc-git: https://github.com/echasnovski/mini.doc
minidoc-path: ~/.local/share/nvim/site/pack/vendor/start/mini.doc
nvim_url: https://github.com/neovim/neovim/releases/download/nightly/nvim-linux64.tar.gz

jobs:
docs:
Expand All @@ -32,22 +33,11 @@ jobs:
if: github.event_name == 'pull_request'
uses: actions/checkout@v4

- run: date +%F > /tmp/todays-date

- name: Restore cache for today's nightly
uses: actions/cache@v4
with:
path: |
_neovim
key: ${{ runner.os }}-${{ hashFiles('/tmp/todays-date') }}

- name: Install neovim and dependencies
run: |
mkdir -p ${{ env.runtime }}
test -d _neovim || {
mkdir -p _neovim
curl -sL ${{ matrix.nvim_url }} | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
}
mkdir -p _neovim
curl -sL ${{ env.nvim_url }} | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
git clone --depth 1 ${{ env.minidoc-git }} ${{ env.minidoc-path }}
ln -s $(pwd) ${{ env.runtime }}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Added `:ObsidianTOC` command for loading the table of contents of the current note into a picker list.

## [v3.8.1](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.8.1) - 2024-06-26

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ _Keep in mind this plugin is not meant to replace Obsidian, but to complement it

- `:ObsidianToggleCheckbox` to cycle through checkbox options.

- `:ObsidianTOC` to load the table of contents of the current note into a picker list.

### Demo

[![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)
Expand Down
3 changes: 3 additions & 0 deletions lua/obsidian/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local command_lookups = {
ObsidianPasteImg = "obsidian.commands.paste_img",
ObsidianExtractNote = "obsidian.commands.extract_note",
ObsidianDebug = "obsidian.commands.debug",
ObsidianTOC = "obsidian.commands.toc",
}

local M = setmetatable({
Expand Down Expand Up @@ -185,4 +186,6 @@ M.register(

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

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

return M
25 changes: 25 additions & 0 deletions lua/obsidian/commands/toc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local util = require "obsidian.util"

---@param client obsidian.Client
return function(client, _)
local note = assert(client:current_note(0, { collect_anchor_links = true }))

---@type obsidian.PickerEntry[]
local picker_entries = {}
for _, anchor in pairs(note.anchor_links) do
local display = string.rep("#", anchor.level) .. " " .. anchor.header
table.insert(
picker_entries,
{ value = display, display = display, filename = tostring(note.path), lnum = anchor.line }
)
end

-- De-duplicate and sort.
picker_entries = util.tbl_unique(picker_entries)
table.sort(picker_entries, function(a, b)
return a.lnum < b.lnum
end)

local picker = assert(client:picker())
picker:pick(picker_entries, { prompt_title = "Table of Contents" })
end
2 changes: 1 addition & 1 deletion lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local util = {}
---@return boolean
util.tbl_contains = function(table, val)
for i = 1, #table do
if table[i] == val then
if vim.deep_equal(table[i], val) then
return true
end
end
Expand Down

0 comments on commit e53072a

Please sign in to comment.