Implement promoting and demoting headers.

This commit is contained in:
CronyAkatsuki 2023-12-27 20:31:31 +01:00
parent 515ef9654e
commit 798c6eb608

38
lua/md-tools/init.lua Normal file
View File

@ -0,0 +1,38 @@
local M = {}
local header_regex = "^(#+) (.+)"
M.promote = function()
if vim.api.nvim_get_mode().mode == "n" then
local current_line = vim.api.nvim_get_current_line()
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local new_line = ""
if current_line:match(header_regex) then
if current_line:match("^# (.+)") then
vim.notify("You can't promote this header anymore")
return
end
new_line = current_line:gsub("^#", "", 1)
vim.api.nvim_buf_set_lines(0, row - 1, row, true, { new_line })
end
end
end
M.demote = function()
if vim.api.nvim_get_mode().mode == "n" then
local current_line = vim.api.nvim_get_current_line()
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local new_line = ""
if current_line:match(header_regex) then
if current_line:match("^###### (.+)") then
vim.notify("You can't demote this header anymore")
return
end
new_line = current_line:gsub("^#", "##", 1)
vim.api.nvim_buf_set_lines(0, row - 1, row, true, { new_line })
end
end
end
return M