t0vim/ftplugin/markdown.lua
t0xa 04072337f4 Markdown: Add some tools to work with markdown files
- Add links snippet
- Add Telescope picker for headers
2025-10-17 10:09:33 +03:00

50 lines
1.5 KiB
Lua

local keymap = vim.keymap
-- Picker for markdown headers
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local function markdown_headings()
local bufnr = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local headings = {}
for lnum, line in ipairs(lines) do
local heading = line:match('^(#+%s+.+)$')
if heading then
table.insert(headings, {
lnum = lnum,
text = heading,
display = string.format("%4d: %s", lnum, heading)
})
end
end
pickers.new({}, {
prompt_title = 'Markdown Headings',
finder = finders.new_table {
results = headings,
entry_maker = function(entry)
return {
value = entry,
display = entry.display,
ordinal = entry.text,
lnum = entry.lnum,
}
end
},
sorter = conf.generic_sorter({}),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
vim.api.nvim_win_set_cursor(0, {selection.value.lnum, 0})
end)
return true
end,
}):find()
end
keymap.set("n", "<leader>fdh", markdown_headings, { desc = "Grep: Markdown headings" })