iamcco / markdown-preview.nvim

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

need help on 'mkdp_browserfunc' with custom shell script #534

Open wisonye opened 1 year ago

wisonye commented 1 year ago

Describe the bug Can't make it works with a customized shell script in LUA setup

To Reproduce Steps to reproduce the behavior:

  1. Paste the following setting in Lua config file:

    function open_preview_in_brave(url)
        os.execute('/home/wison/my-shell/start_brave_browser.sh')
        print(">>> my_markdown_preview >>> open_preview_in_brave | url: "..url)
    end
    
    -- Custom open browser function
    -- vim.cmd 'let g:mkdp_browserfunc = \'/home/wison/my-shell/start_brave_browser.sh\''
    vim.cmd 'let g:mkdp_browserfunc = \'open_preview_in_brave\''
  2. Open .md file and run :MarkDdownPreview command
  3. See error:
    [markdown-preview.nvim] UnhandledRejection
    Error: Vim:E117: Unknown function: open_preview_in_brave

Expected behavior I think the open_preview_in_brave function should be call.

Screenshots

// Error in neovim

error-2

// I also added a little bit of debug code in app/server.js to print out the settings to a temp log file, g:mkdp_browserfunc has been set with the correct function value:

error-1

Desktop (please complete the following information):

Log:

davmacario commented 8 months ago

Hi there! I have been stuck on a similar problem (trying to pass a Lua function to g:mkdp_browserfunc) for a couple hours today, but I came up with a solution.

Since I couldn't figure out a way to pass the function by name in Lua, I opted for using Vimscript in this way:

vim.api.nvim_exec([[
    vimscript here
]]], false)

Then, for the function you want to execute, I believe the code will be:

vim.api.nvim_exec([[
function! open_preview_in_brave(URL)
    execute "/home/wison/my-shell/start_brave_browser.sh"
    echom ">>> my_markdown_preview >>> open_preview_in_brave | url: " . a:url
endfunction

let g:mkdp_browserfunc = 'open_preview_in_brave'
]], false)

In any case, may I suggest using the variables:

I apologize in advance but I am still learning Lua, so the code might not be the best in terms of efficiency :).