Is it possible to send all headings to the location list to produce an "outline"? #635
Replies: 2 comments 1 reply
-
Documenting some of my findings after further investigation. I was surprised to learn that parsing is currently done via regex and not by walking the treesitter parse tree nodes or via a treesitter query. The query file used for markdown syntax highlighting illustrates how this could be done in a way that is more concrete than the treesitter query docs. It may also be reasonable to just walk the parse tree using Lua logic, though I don't have enough experience with treesitter to know what the pros or cons of each approach are. |
Beta Was this translation helpful? Give feedback.
-
Hey @scienceplease, here's a snippet that sends the table of contents into a picker list. I may turn this into a command unless someone wants to beat me to it. local util = require "obsidian.util"
---@type obsidian.Client
local client = require("obsidian").get_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" }) |
Beta Was this translation helpful? Give feedback.
-
Is it possible to get an "outline" AKA "table of contents" of the markdown buffer by sending all headings to the location list? This would be equivalent to how the location list is populated by
:gO
when on a Vim help/doc buffer.I know that this can be achieved by writing a Treesitter query to get all the appropriate node types and their locations, though I assume the components needed to do this already exist somewhere in the plugin (or in someone's personal config).
Beta Was this translation helpful? Give feedback.
All reactions