Skip to content

hustuhao/markdown-codeblock-format.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

markdown-codeblock-format.nvim

A simple plugin to format selected code block in markdown file based on conform.nvim.

How to use it ?

1.Make sure that you select code block ```{lang}\n{your code}``` in v model

2.Then press <leader>fm to call the lua function to format the code

markdown-codeblock-format.nvim

Setup

1.You need to setup some formatters by language.

It support all formatters that can be used with conform.nvim becuase the format function is just a invoking of

require("mcf").setup({
	formatters = {
		sh = { "shfmt" },
		-- language = { formatter1, formatter2,... }
	},
})

2.Set key mapping for formatting code block.

vim.api.nvim_set_keymap(
    "v",
    "<leader>fm",
    ':lua require("mcf").format_selected_code()<CR>',
    { noremap = true, silent = true }
)

Installation

lazy.nvim
return {
	"hustuhao/markdown-codeblock-format.nvim",
	dependencies = {
		"stevearc/conform.nvim",
	},
	opts = {
		-- Define the formatters to use for each language
		formatters = {
			lua = { "stylua" },
			python = { "black" },
			javascript = { "prettier" },
			sh = { "shfmt" },
			sql = { "sqlfmt" },
			go = { "goimports", "gofmt" },
			rust = { "rustfmt" },
		},
		log_level = vim.log.levels.ERROR, -- default log level is warn
	},

	keys = {
		-- Define key mappings using the 'keys' configuration in LazyVim
		{
			"<leader>fm",
			':lua require("mcf").format_selected_code()<CR>',
			mode = "v", -- Visual mode
			noremap = true,
			silent = true,
			desc = "Format selected code block", -- Description for the key mapping
		},
	},
}
Packer
return require("packer").startup(function()
	use({
		"hustuhao/markdown-codeblock-format.nvim",
		config = function()
			local opts = {
				-- Define the formatters to use for each language
				formatters = {
					lua = { "stylua" },
					python = { "black" },
					javascript = { "prettier" },
					sh = { "shfmt" },
					sql = { "sqlfmt" },
					go = { "goimports", "gofmt" },
					rust = { "rustfmt" },
				},
			}
			require("mcf").setup(opts)
		end,
	})

	-- Key mapping
	vim.api.nvim_set_keymap(
		"v",
		"<leader>fm",
		':lua require("mcf").format_selected_code()<CR>',
		{ noremap = true, silent = true }
	)
end)

About

markdown-codeblock-format.nvim

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages