From 1771a6133463c2a3cc7c201e619a4ca751f63aca Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 6 Jun 2024 20:50:37 +0200 Subject: [PATCH] nvim: Use peek for markdown preview where possible On machines that have deno installed, we use peek instead of markdown-preview for html-based previews of md files. The preview is more responsive and in a neater package, as well as just not relying on any vim plugin stuff to the same degree. We still fall back to the old markdown-preview if no deno executable is available. We also change the key maps slightly to prepare for future 'prose' or 'preview' based mappings: All mappings are registered under the `p` layer, with md preview being `pp` and various mindmap operations moved to `pm`. --- nvim/.config/nvim/after/ftplugin/markdown.lua | 23 +++++++++++++++---- nvim/.config/nvim/lua/plugins/prose.lua | 15 +++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/nvim/.config/nvim/after/ftplugin/markdown.lua b/nvim/.config/nvim/after/ftplugin/markdown.lua index 19696ba..7f2c06f 100644 --- a/nvim/.config/nvim/after/ftplugin/markdown.lua +++ b/nvim/.config/nvim/after/ftplugin/markdown.lua @@ -5,7 +5,7 @@ if require("core.util").is_available("which-key") then require("which-key").register({ ["e"] = { name = "+criticmarkup" } }) end -if require("zk.util").notebook_root(vim.fn.expand("%:p")) ~= nil then +if require("core.util").is_available("zk") and require("zk.util").notebook_root(vim.fn.expand("%:p")) ~= nil then map("n", "", "lua vim.lsp.buf.definition()", { silent = true }) end @@ -22,11 +22,26 @@ map("n", "[c", "?^```n}:nohl", { desc = "previous code cell" }) map("n", "co", "o```python```k", { desc = "Insert quarto cell below" }) map("n", "cO", "O```python```k", { desc = "Insert quarto cell above" }) +if require("core.util").is_available("which-key") then + require("which-key").register({ ["p"] = { name = "+prose" } }) +end -- show nice md preview in browser (auto-syncs scrolling) -map("n", "cp", "MarkdownPreviewToggle", { desc = "show md preview" }) +if require("core.util").is_available("peek") then + local peek = require("peek") + local function togglePeek() + if peek.is_open() then + peek.close() + else + peek.open() + end + end + map("n", "pp", togglePeek, { desc = "show md preview" }) +else + map("n", "pp", "MarkdownPreviewToggle", { desc = "show md preview" }) +end -- create mindmaps directly from markdown! requires external executable if vim.fn.executable("markmap") then - map("n", "cm", "MarkmapOpen", { desc = "open md mindmap" }) - map("n", "cM", "MarkmapWatch", { desc = "watch for md mindmap" }) + map("n", "pm", "MarkmapOpen", { desc = "open md mindmap" }) + map("n", "pM", "MarkmapWatch", { desc = "watch for md mindmap" }) end diff --git a/nvim/.config/nvim/lua/plugins/prose.lua b/nvim/.config/nvim/lua/plugins/prose.lua index 29b60c6..7caadba 100644 --- a/nvim/.config/nvim/lua/plugins/prose.lua +++ b/nvim/.config/nvim/lua/plugins/prose.lua @@ -8,7 +8,7 @@ local prose_plugs = { config = true, cmd = { "ZenMode" }, dependencies = { "folke/twilight.nvim" }, - keys = { { "sz", ":ZenMode", { silent = true } } }, + keys = { { "sz", ":ZenMode", silent = true, desc = "toggle zen mode" } }, }, { "andrewferrier/wrapping.nvim", @@ -48,8 +48,21 @@ local prose_plugs = { ft = writing_ft, }, -- generate an auto-updating html preview for md files + -- uses the very nice peek if deno is available, otherwise falls back to markdown-preview + { + "toppair/peek.nvim", + event = { "VeryLazy" }, + cond = vim.fn.executable("deno") == 1, + build = "deno task --quiet build:fast", + config = function() + require("peek").setup() + vim.api.nvim_create_user_command("PeekOpen", require("peek").open, {}) + vim.api.nvim_create_user_command("PeekClose", require("peek").close, {}) + end, + }, { "iamcco/markdown-preview.nvim", + cond = vim.fn.executable("deno") == 0, build = function() vim.fn["mkdp#util#install"]() end,