iamcco / markdown-preview.nvim

markdown preview plugin for (neo)vim
MIT License
6.67k stars 277 forks source link

No command :MarkdownPreview #585

Open tobebot opened 1 year ago

tobebot commented 1 year ago

I installed markdown-preview with yarn, but when I want to start the preview with :MarkdownPreview, this fails with code 'E492: This is not an editor command: MarkdownPreview'

TSoli commented 1 year ago

This seems to happen to me too when I open a markdown file with neovim. However it begins working again when I open a new buffer (i.e with :Lex or :e). I am lazy loading it with packer so my plugins.lua file has.

use {'iamcco/markdown-preview.nvim', run = 'cd app && yarn install', cmd = 'MarkdownPreview'}

It seems if I reinstall it without the cmd part (i.e do not lazy load it) then it works straight away. I think it is probably something to do with the lazy loading.

Seems it may be related to #94 . #582 might also be related but I don't understand all the options they use so I will need to do some reading and try to understand better. I haven't tried their solution yet.

My full config can be found at https://github.com/TSoli/dotfiles/tree/d5bec98315e8c3442eb9251cd117b1bd2632742d

UPDATE: When I open the file I can see that the plugin is not loaded (with :PackerStatus) as expected however when I open a new buffer and go back I can see it is loaded even though I never ran the :MarkdownPreview command yet. Actually, even if I run :MarkdownPreview once (and it fails) I can run it as many times as I want but it won't work. But if I then open :PackerStatus, I see that is loaded and then if I try to run :MarkdownPreview after that it works. I am so confused.

UPDATE 2: Tried with lazy.nvim and the behaviour is similar (see my full config at https://github.com/TSoli/dotfiles/tree/3aef39b65ccb52e6bdf1174e6d99492095895530 ). As a simple workaround I just sepcified ft = "markdown" instead of using cmd so it will now load only for markdown files. Of course, this is not the same as lazy loading on command but probably good enough until the issue is fixed.

ellisfk commented 1 year ago

It seems that the MarkdownPreview command is only available when editing a .md file. This is probably intended behaviour but it should be made clearer in the documentation.

nvim test.txt
:MarkdownPreview
E492: This is not an editor command: MarkdownPreview'
nvim test.md
:MarkdownPreview
// Plugin works as expected
TSoli commented 1 year ago

It seems that the MarkdownPreview command is only available when editing a .md file. This is probably intended behaviour but it should be made clearer in the documentation.

nvim test.txt
:MarkdownPreview
E492: This is not an editor command: MarkdownPreview'
nvim test.md
:MarkdownPreview
// Plugin works as expected

I am not sure what your installation settings looked like, but the issue seems to be that it won't lazy load on the MarkdownPreview command when this option is set, even in markdown files.

ellisfk commented 1 year ago

I don't use lazy loading so I wouldn't know, perhaps that merits opening a separate issue.

In my case - my issue was simply not knowing that only .md files can be used with the MarkdownPreview command by default. Reading the documentation further I can see

" recognized filetypes
" these filetypes will have MarkdownPreview... commands
let g:mkdp_filetypes = ['markdown'] 

But I feel like this should be made explicit in the documentation to prevent other people (like me) trying it in a fresh vim buffer without a filetype and wondering why it isn't working.

TSoli commented 1 year ago

I don't use lazy loading so I wouldn't know, perhaps that merits opening a separate issue.

Yeah perhaps I might try that.

But I feel like this should be made explicit in the documentation to prevent other people (like me) trying it in a fresh vim buffer without a filetype and wondering why it isn't working.

Maybe but what were you expecting to happen if it's not a markdown file? Is there a specific example you can give where it would make sense to use this plugin with something that isn't a markdown file?

ellisfk commented 1 year ago

I agree that there is no reason why the plugin should activate by default with non-markdown files. What I'm saying however is that this should be made clear in the documentation.

When I install the plugin and open a fresh buffer, write some basic Markdown to test the plugin, then I see that there is no MarkdownPreview command, my intuition thinks the plugin is broken. My first thought is not "I will try changing the extension of the buffer I am editing to .md".

anuramat commented 1 year ago

as a workaround, you can manually trigger BufEnter event to make it lazy load on :MarkdownPreview commands:

specs.mdpreview = {
  'iamcco/markdown-preview.nvim',
  init = function()
    local function load_then_exec(cmd)
      return function()
        vim.cmd.delcommand(cmd)
        require('lazy').load({ plugins = { 'markdown-preview.nvim' } })
        vim.api.nvim_exec_autocmds('BufEnter', {}) -- commands appear only after BufEnter
        vim.cmd(cmd)
      end
    end
    for _, cmd in pairs({ 'MarkdownPreview', 'MarkdownPreviewStop', 'MarkdownPreviewToggle' }) do
      vim.api.nvim_create_user_command(cmd, load_then_exec(cmd), {})
    end
  end,
  -- etc
}

the plugin is way too heavy to not be lazy loaded imo

geofflangenderfer commented 1 year ago

as a workaround, you can manually trigger BufEnter event to make it lazy load on :MarkdownPreview commands:

specs.mdpreview = {
  'iamcco/markdown-preview.nvim',
  init = function()
    local function load_then_exec(cmd)
      return function()
        vim.cmd.delcommand(cmd)
        require('lazy').load({ plugins = { 'markdown-preview.nvim' } })
        vim.api.nvim_exec_autocmds('BufEnter', {}) -- commands appear only after BufEnter
        vim.cmd(cmd)
      end
    end
    for _, cmd in pairs({ 'MarkdownPreview', 'MarkdownPreviewStop', 'MarkdownPreviewToggle' }) do
      vim.api.nvim_create_user_command(cmd, load_then_exec(cmd), {})
    end
  end,
  -- etc
}

the plugin is way too heavy to not be lazy loaded imo

@anuramat

anuramat commented 1 year ago

NB: this is all lazy.nvim specific

  1. I wrap the plugin specs for convenience:
    
    local specs = {}
    local u = require('utils')

specs.a = spec_a specs.b = spec_b

return u.values(specs)

is equivalent to

return { spec_a, spec_b }


2. Lua indeed
metal3d commented 11 months ago

I've got a weird behavior also.

If I open an md file, the command isn't found.

I tried to save, reload, reopen, nothing makes the command to be launched.

I then launch "PlugUpdate", then "q" to close the vim-plug buffer, and the MarkdownPreview command is back.

If I close neovim, reopen the markdown file => the command is not present. I had to relaunch PlugUpdate and so on...

metal3d commented 11 months ago

Forget... I misplaced the Plug directive...

serranomorante commented 7 months ago

as a workaround, you can manually trigger BufEnter event to make it lazy load on :MarkdownPreview commands:

specs.mdpreview = {
  'iamcco/markdown-preview.nvim',
  init = function()
    local function load_then_exec(cmd)
      return function()
        vim.cmd.delcommand(cmd)
        require('lazy').load({ plugins = { 'markdown-preview.nvim' } })
        vim.api.nvim_exec_autocmds('BufEnter', {}) -- commands appear only after BufEnter
        vim.cmd(cmd)
      end
    end
    for _, cmd in pairs({ 'MarkdownPreview', 'MarkdownPreviewStop', 'MarkdownPreviewToggle' }) do
      vim.api.nvim_create_user_command(cmd, load_then_exec(cmd), {})
    end
  end,
  -- etc
}

the plugin is way too heavy to not be lazy loaded imo

Yep, this fixed it for me. Thanks!

using lazy.nvim

return {
  "iamcco/markdown-preview.nvim",
  enabled = vim.fn.executable("npm") == 1,
  cmd = { "MarkdownPreviewToggle", "MarkdownPreview", "MarkdownPreviewStop" },
  build = "cd app && npm install",
  init = function()
    vim.g.mkdp_filetypes = { "markdown" }
    vim.g.mkdp_auto_close = 0
    vim.g.mkdp_command_for_global = 1
    vim.g.mkdp_combine_preview = 1

    local function load_then_exec(cmd)
      return function()
        vim.cmd.delcommand(cmd)
        require("lazy").load({ plugins = { "markdown-preview.nvim" } })
        vim.api.nvim_exec_autocmds("BufEnter", {}) -- commands appear only after BufEnter
        vim.cmd(cmd)
      end
    end

    ---Fixes "No command :MarkdownPreview"
    ---https://github.com/iamcco/markdown-preview.nvim/issues/585#issuecomment-1724859362
    for _, cmd in pairs({ "MarkdownPreview", "MarkdownPreviewStop", "MarkdownPreviewToggle" }) do
      vim.api.nvim_create_user_command(cmd, load_then_exec(cmd), {})
    end
  end,
}