all repos — dot @ fdaf6742b0823b088c58ac8e26f6556a0c12c43f

Shell, editor and terminal configuration, and the tools that go with them

config/nvim/lua/plugins/markdown.lua (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
-- In-buffer markdown rendering: headings, code blocks, tables, callouts and
-- checkboxes rendered in place, in the one buffer. No preview window or split.
-- The cursor line keeps its raw syntax (the plugin's anti_conceal), so editing
-- is unaffected. <leader>mt toggles rendering for the current buffer.
return {
    {
        "MeanderingProgrammer/render-markdown.nvim",
        ft = { "markdown" },
        dependencies = { "nvim-tree/nvim-web-devicons" },
        opts = {},
        config = function(_, opts)
            require("render-markdown").setup(opts)

            -- render-markdown takes its per-level heading colours from the
            -- treesitter captures @markup.heading.{1..6}.markdown. None of the
            -- colourschemes under config/nvim/colors/ define treesitter groups
            -- at all -- happy_hacking, onehalfdark and ayu were each checked --
            -- so those captures resolve to nothing and every heading level
            -- renders with no colour. Supply a ramp, but only where the theme
            -- left the group undefined, so a theme that does define them keeps
            -- its own. Re-applied on ColorScheme, since cfg-watch switches the
            -- theme at runtime and `:colorscheme` can clear highlight groups.
            local ramps = {
                dark = {
                    { "#F3F2CC", 230 },
                    { "#FAD566", 221 },
                    { "#B3EBBF", 157 },
                    { "#81A2C7", 103 },
                    { "#F77EBD", 211 },
                    { "#fcb280", 179 },
                },
                light = {
                    { "#2F2F2F", 236 },
                    { "#8C6D1F", 136 },
                    { "#3F6B3F", 22 },
                    { "#3A5A8C", 24 },
                    { "#8C3A6B", 89 },
                    { "#8C5A2F", 130 },
                },
            }
            local function set_heading_colors()
                local ramp = ramps[vim.o.background] or ramps.dark
                for level = 1, 6 do
                    local name = "RenderMarkdownH" .. level
                    -- Put render-markdown's own link back first, so a theme that
                    -- does define the capture wins, and so this stays correct
                    -- after a theme switch.
                    vim.api.nvim_set_hl(0, name, { link = "@markup.heading." .. level .. ".markdown" })
                    local hl = vim.api.nvim_get_hl(0, { name = name })
                    if not (hl.fg or hl.bg or hl.bold or hl.italic or hl.underline) then
                        vim.api.nvim_set_hl(0, name, {
                            fg = ramp[level][1],
                            ctermfg = ramp[level][2],
                            bold = true,
                        })
                    end
                end
            end
            set_heading_colors()
            vim.api.nvim_create_autocmd("ColorScheme", { callback = set_heading_colors })

            vim.api.nvim_create_autocmd("FileType", {
                pattern = "markdown",
                callback = function(event)
                    vim.keymap.set("n", "<leader>mt", "<cmd>RenderMarkdown buf_toggle<cr>", {
                        buffer = event.buf,
                        desc = "Toggle markdown rendering",
                    })
                end,
            })
        end,
    },
}