Markdown: Add some tools to work with markdown files

- Add links snippet
- Add Telescope picker for headers
This commit is contained in:
t0xa 2025-10-17 10:09:33 +03:00
parent 6174a9dcbc
commit 04072337f4
3 changed files with 77 additions and 3 deletions

50
ftplugin/markdown.lua Normal file
View file

@ -0,0 +1,50 @@
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" })

View file

@ -0,0 +1,22 @@
require("luasnip.session.snippet_collection").clear_snippets "markdown"
local ls = require "luasnip"
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
ls.add_snippets(
"markdown",
{
s("link", {
t("["), i(1, "description"), t("]("),
f(function()
local clip = vim.fn.getreg("+")
if clip == "" then
return "url"
end
return clip
end),
t(")"), i(0),
}),
})

View file

@ -11,11 +11,12 @@ pcall(require("telescope").load_extension, "fzf")
local function live_grep_with_glob()
vim.ui.input({
prompt = "Glob pattern: ",
default = "", -- Optional default value
default = "/home/user/Work/root/ivideon/cloud_analytics/person_detection_node", -- Optional default value
}, function(input)
if input then
require('telescope.builtin').live_grep({
glob_pattern = input
-- glob_pattern = input,
search_dirs = { input }
})
end
end)
@ -23,6 +24,7 @@ end
local builtin = require("telescope.builtin")
local keymap = vim.keymap
-- Create a Neovim command
vim.api.nvim_create_user_command('TelescopeGrepGlob', live_grep_with_glob, {})
@ -33,5 +35,5 @@ keymap.set("n", "<leader>fg", builtin.live_grep, { desc = "FZF: find string in c
keymap.set("n", "<leader>fb", builtin.buffers, { desc = "FZF: display opened buffers" })
keymap.set("n", "<leader>fh", builtin.help_tags, { desc = "FZF: display help tags" })
keymap.set("n", "<leader>ft", "<cmd>TodoTelescope<cr>", { desc = "FZF: display TODO comments" })
keymap.set("n", "<leader>fds", builtin.lsp_document_symbols, { desc = "FZF: Document symbols" })
keymap.set("n", "<leader>fds", builtin.lsp_document_symbols, { desc = "Grep: Document symbols" })
keymap.set('n', '<leader>fp', live_grep_with_glob, { desc = 'Live grep with glob pattern' })