From 798c6eb608c094d4fc7b965494ae9c6d3ce78b0e Mon Sep 17 00:00:00 2001 From: CronyAkatsuki Date: Wed, 27 Dec 2023 20:31:31 +0100 Subject: [PATCH] Implement promoting and demoting headers. --- lua/md-tools/init.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lua/md-tools/init.lua diff --git a/lua/md-tools/init.lua b/lua/md-tools/init.lua new file mode 100644 index 0000000..27d31da --- /dev/null +++ b/lua/md-tools/init.lua @@ -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