MeanderingProgrammer / render-markdown.nvim

Plugin to improve viewing Markdown files in Neovim
MIT License
1.37k stars 32 forks source link

bug: commit 'checkbox position' introduced a nil error #144

Closed balpert89 closed 3 weeks ago

balpert89 commented 3 weeks ago

Neovim version (nvim -v)

0.10.0

Operating system

Linux

Terminal emulator / GUI

Alacritty

Describe the bug

Opening a formerly working buffer with an .md file in it. Caused by a removed nil check in pad:

https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/275f28943ab9ce6017f90bab56c5b5b3c651c269#diff-be254f6b492811ae05ef31fed1bb6212b73ec873b0363247bf0fccf2b5cd2306L38-R45

Expected behavior

Opens correctly.

Healthcheck output


==============================================================================
render-markdown: require("render-markdown.health").check()

render-markdown.nvim [version] ~
- OK plugin 6.2.0
- OK neovim >= 0.10

render-markdown.nvim [configuration] ~
- ERROR Failed to run healthcheck for "render-markdown" plugin. Exception:
  .../lazy/render-markdown.nvim/lua/render-markdown/state.lua:199: bad argument #1 to 'pairs' (table expected, got string)

Plugin configuration

return {
    {
        "iamcco/markdown-preview.nvim",
        lazy = true,
        ft = { "md", "markdown" },
        cmd = { "MarkdownPreviewToggle", "MarkdownPreview", "MarkdownPreviewStop" },
        build = function()
            vim.fn["mkdp#util#install"]()
        end,
        init = function()
            vim.g.mkdp_filetypes = { "markdown" }
        end,
    },
    {
        "dhruvasagar/vim-table-mode",
        lazy = true,
        ft = "markdown",
        init = function()
            vim.g.table_mode_map_prefix = "<leader>md"
            vim.g.table_mode_corner = "|"
        end,
    },
}

Plugin error log

Stacktrace

Error executing vim.schedule lua callback: ...im/lazy/render-markdown.nvim/lua/render-markdown/str.lua:45: attempt to concatenate local 's' (a nil value)
stack traceback:
    ...im/lazy/render-markdown.nvim/lua/render-markdown/str.lua:45: in function 'pad_to'
    ...r-markdown.nvim/lua/render-markdown/handler/markdown.lua:504: in function 'checkbox'
    ...r-markdown.nvim/lua/render-markdown/handler/markdown.lua:53: in function 'callback'
    ...azy/render-markdown.nvim/lua/render-markdown/context.lua:104: in function 'query'
    ...r-markdown.nvim/lua/render-markdown/handler/markdown.lua:37: in function 'parse'
    ...vim/lazy/render-markdown.nvim/lua/render-markdown/ui.lua:173: in function 'parse_tree'
    ...vim/lazy/render-markdown.nvim/lua/render-markdown/ui.lua:141: in function 'parse_buffer'
    ...vim/lazy/render-markdown.nvim/lua/render-markdown/ui.lua:86: in function 'update'
    ...vim/lazy/render-markdown.nvim/lua/render-markdown/ui.lua:57: in function <...vim/lazy/render-markdown.nvim/lua/render-markdown/ui.lua:56>

Confirmations

Additional information

No response

MeanderingProgrammer commented 3 weeks ago

Based on these errors

- ERROR Failed to run healthcheck for "render-markdown" plugin. Exception:
  .../lazy/render-markdown.nvim/lua/render-markdown/state.lua:199: bad argument #1 to 'pairs' (table expected, got string)

Something is very wrong in your config. I suspect you may have recently upgraded after a while and something that used to take a string property is now a table. There have a been several breaking changes all documented in the releases: https://github.com/MeanderingProgrammer/render-markdown.nvim/releases.

Can you send the configuration you have for this plugin as it is definitely not the default one, I am not interested in the iamcco/markdown-preview.nvim / dhruvasagar/vim-table-mode configurations.

balpert89 commented 3 weeks ago

Ahh sorry I have copied over the wrong content. Here is the one for render-markdown:

return {
    {
        "MeanderingProgrammer/render-markdown.nvim",
        lazy = true,
    -- TODO: re-enable when https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/144 is fixed
        enabled = false,
        ft = { "markdown", "pandoc" },
        commands = { "RenderMarkdown" },
        opts = {
            bullets = { "", "", "◆", "◇" },
            headings = { "󰼏 ", "󰎨 ", "󰼑 ", "󰎲 ", "󰼓 ", "󰎴 " },
            checkbox = {
                unchecked = "󰄱 ",
                checked = "󰄲 ",
            },
            file_types = { "markdown", "pandoc" },
            highlights = {
                checkbox = {
                    unchecked = "@markup.list.unchecked",
                    checked = "@markdown_check_done",
                },
            },
        },
        dependencies = { "nvim-treesitter/nvim-treesitter", "nvim-tree/nvim-web-devicons" },
        config = function(_, opts)
            require("render-markdown").setup(opts)

            local function toggle_checkbox(char)
                local current_line = vim.api.nvim_get_current_line()

                local _, _, checkbox_state = string.find(current_line, "%[([ " .. char .. string.upper(char) .. "])%]")

                if checkbox_state then
                    local new_state = checkbox_state == " " and char or " "
                    local new_line = string.gsub(current_line, "%[.-%]", "[" .. new_state .. "]")

                    vim.api.nvim_set_current_line(new_line)
                end
            end
            vim.keymap.set("n", "<Leader>XC", function()
                toggle_checkbox("x")
            end, { noremap = true, silent = true })

            vim.keymap.set("n", "<Leader>XU", function()
                toggle_checkbox("o")
            end, { noremap = true, silent = true })
        end,
    },
}
balpert89 commented 3 weeks ago

Yes, I have missed the last breaking change it seems. Sorry for the inconvenience.

I have adapted the configuration, now it renders correctly:

return {
    {
        "MeanderingProgrammer/render-markdown.nvim",
        lazy = true,
        ft = { "markdown", "pandoc" },
        commands = { "RenderMarkdown" },
        opts = {
            bullet = {
                icons = { "", "", "◆", "◇" },
            },
            heading = {
                icons = { "󰼏 ", "󰎨 ", "󰼑 ", "󰎲 ", "󰼓 ", "󰎴 " },
            },
            checkbox = {
                unchecked = {
                    highlight = "@markup.list.unchecked",
                    icon = "󰄱 ",
                },
                checked = {
                    highlight = "@markdown_check_done",
                    icon = "󰄲 ",
                },
            },
            file_types = { "markdown", "pandoc", "vimwiki" },
        },
        dependencies = { "nvim-treesitter/nvim-treesitter", "nvim-tree/nvim-web-devicons" },
        config = function(_, opts)
            require("render-markdown").setup(opts)

            local function toggle_checkbox(char)
                local current_line = vim.api.nvim_get_current_line()

                local _, _, checkbox_state = string.find(current_line, "%[([ " .. char .. string.upper(char) .. "])%]")

                if checkbox_state then
                    local new_state = checkbox_state == " " and char or " "
                    local new_line = string.gsub(current_line, "%[.-%]", "[" .. new_state .. "]")

                    vim.api.nvim_set_current_line(new_line)
                end
            end
            vim.keymap.set("n", "<Leader>XC", function()
                toggle_checkbox("x")
            end, { noremap = true, silent = true })

            vim.keymap.set("n", "<Leader>XU", function()
                toggle_checkbox("o")
            end, { noremap = true, silent = true })
        end,
    },
}
MeanderingProgrammer commented 3 weeks ago

Perfect, was going to help get it in order since I know it can be a pain, but glad you got it figured out, sorry for the inconvenience! Breaking changes should be fairly rare going forward.

Interesting checkbox toggle logic, if you've not come across it before I'd recommend you checkout this plugin: https://github.com/tadmccorkle/markdown.nvim. Has support for toggling tasks and some more conveniences.