Skip to content

Commit c8aa3f9

Browse files
committed
feat: allow MarkdownNewTemplate outside markdown files
1 parent 9a24630 commit c8aa3f9

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
- Allow `MarkdownNewTemplate` command to be used in any filetype.
17+
1618
### Deprecated
1719

1820
### Removed
1921

2022
### Fixed
2123

24+
- Visual mode keymap for inserting headers now correctly exits visual mode before executing the command.
25+
2226
### Security
2327

2428
## [1.0.0] - 2025-05-05
2529

2630
### Added
31+
2732
- Added default keybinding `<leader>mH` for inserting headers.
2833
- Added ability to use generator functions for frontmatter and placeholders in templates.
2934
- Added health checks (`:checkhealth markdown-tools`).
3035
- Initial release of `markdown-tools.nvim`.
3136

3237
### Changed
38+
3339
- Updated health checks (`:checkhealth markdown-tools`).
3440

3541
### Deprecated

lua/markdown-tools/keymaps.lua

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ local function setup_keymap(mode, key, cmd, desc, opts)
1111
if key and key ~= "" then
1212
opts = opts or {}
1313
opts.desc = desc
14-
opts.buffer = true
1514
vim.keymap.set(mode, key, cmd, opts)
1615
end
1716
end
@@ -21,22 +20,26 @@ end
2120
---@param commands_enabled table Command enable configuration
2221
---@param file_types string[] List of file types to apply keymaps to
2322
function M.setup_keymaps(keymaps, commands_enabled, file_types)
24-
-- Create a dedicated augroup for keymaps
25-
local augroup = vim.api.nvim_create_augroup("MarkdownShortcutsKeymaps", { clear = true })
23+
-- Register the global keymap for create_from_template first
24+
if commands_enabled.create_from_template then
25+
setup_keymap(
26+
"n", -- Mode
27+
keymaps.create_from_template, -- Key
28+
"<cmd>MarkdownNewTemplate<CR>", -- Command
29+
"Create from template", -- Description
30+
{} -- Global keymap, no buffer option needed
31+
)
32+
end
33+
34+
-- Create a dedicated augroup for buffer-local keymaps
35+
local augroup = vim.api.nvim_create_augroup("MarkdownShortcutsBufferKeymaps", { clear = true })
2636

2737
vim.api.nvim_create_autocmd("FileType", {
2838
group = augroup,
2939
pattern = file_types, -- Use the configured file types
3040
callback = function()
31-
-- Define keymap configurations
41+
-- Define keymap configurations (excluding create_from_template)
3242
local keymap_configs = {
33-
{
34-
command_key = "create_from_template",
35-
mode = "n",
36-
key = keymaps.create_from_template,
37-
cmd = "<cmd>MarkdownNewTemplate<CR>",
38-
desc = "Create from template",
39-
},
4043
{
4144
command_key = "insert_header",
4245
mode = { "n", "v" },
@@ -177,7 +180,7 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
177180
command_key = "insert_checkbox",
178181
mode = "v", -- Visual mode only
179182
key = keymaps.insert_checkbox,
180-
-- Call the Lua function directly, which handles line insertion
183+
-- Call the Lua function directly, which handles line insertion
181184
cmd = function()
182185
-- Exit visual mode before calling the command
183186
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
@@ -202,11 +205,12 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
202205
},
203206
}
204207

205-
-- Apply all keymap configurations
208+
-- Apply all buffer-local keymap configurations
206209
for _, config in ipairs(keymap_configs) do
207210
-- Only set keymap if the command is enabled
208211
if commands_enabled[config.command_key] then
209212
-- Combine base opts with config-specific opts
213+
-- Add buffer = true for these buffer-local keymaps
210214
local base_opts = { desc = config.desc, buffer = true }
211215
local final_opts = vim.tbl_extend("force", base_opts, config.opts or {})
212216

@@ -215,7 +219,7 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
215219
end
216220
end
217221

218-
-- Add keymap for continuing lists on Enter if enabled
222+
-- Add keymap for continuing lists on Enter if enabled (buffer-local)
219223
if require("markdown-tools.config").options.continue_lists_on_enter then
220224
vim.keymap.set("i", "<CR>", function()
221225
local line = vim.api.nvim_get_current_line()

0 commit comments

Comments
 (0)