Use Pandoc to configure simple Markdown preview function for vim under Windows

Table of Contents

    • Some basic knowledge about Pandoc
    • specific implementation method
    • Design improvements and strains
      • cmd checks if Edge is running
      • Combining the above design
    • final effect

I usually use vim to write Markdown, hoping to preview it conveniently. My idea is this: Press [F5] on the keyboard to open the html of the Markdown file directly in the browser. The existing solution is a real-time browser preview plug-in like MarkdownPreview, but it is not very convenient to install on Windows. My computer has already installed Pandoc, and I hope to use Pandoc plus vim programming to realize this Markdown preview function.

Some basic knowledge about Pandoc

pandoc is a powerful document conversion tool that can convert between multiple document formats, and can compile documents written in Markdown language into html format webpage files. After installing Pandoc, enter the following command on the command line to convert:

pandoc ./[the file name of the file you want to convert].md -f markdown + tex_math_dollars -t html --mathjax -s -o ./[the file name of the web page file you want to output].html

Among them, the -f parameter indicates the input file type. Add + tex_math_dollars after markdown to call the format of Latek formula, otherwise the formula edited in Markdown cannot be displayed; --mathjax appears below code> is the same. The -t parameter indicates the type of file you want to output, and -s indicates output in a standard format. If you don’t add it, the main problem is that the code cannot be highlighted. -o indicates the specified output file.

Specific implementation method

The desired effect is: press [F5] to automatically open the browser and display the compiled Markdown file, and delete this temporary preview file after each preview. We can easily write the following code and add it to the .\Vim\_vimrc file in the Vim installation directory under Windows:

" define shortcut key
map <F5> :w<CR>:call Run()<CR>
imap <F5> <ESC>:w<CR>:call Run()<CR>

" Write a function to implement the function
func! Run()
if expand("%:e") == "md"
pandoc "%" -f markdown + tex_math_dollars -t html --mathjax -s -o ./temprunfile.html & amp; & amp; start .\temprunfile.html
:! del .\temprunfile.html
redraw!
echohl WarningMsg | echo "Running on browser ."
else
        redraw!
        echo "This is not a Markdown File."
    endif
end function

Design improvements and strains

After the recent update of Edge, there have been some problems. I just don’t know what Microsoft has been doing recently. The edge browser has updated several useless smart functions. On the right side of the start page, there is this unexplained scroll bar, plus this “Bing Discovery” button and sidebar. Due to the domestic Internet policy, domestic users have no way to use New Bing, so this button appears in this position, which will cause people to feel helpless and sigh for their fate of not being able to use New Bing, which is very responsive. And the homepage is also very unsightly, and the loading of pictures has also been affected, and sometimes it will not be displayed completely. The function of the sidebar is basically useless. So many people choose to hide the side scroll bar and the Bing Discover button.

Unsightly Bing homepage

The way to hide the scroll bar is to add the following parameters when Bing starts:

--disable-features=msUndersideButton

The effect is to turn the scroll bar into a slim Windows 10 style.

To hide the “Bing Discovery” button is to add this parameter:

--enable-features=OverlayScrollbar, OverlayScrollbarWinStyle, OverlayScrollbarWinStyleAnimation

It is convenient to use Edge every day. You can first turn off Edge’s “Always display sidebar” and “Startup Enhancement”, and then add these two parameters to the “Target” column of the shortcut, so that every time you click Edge on the desktop to start Both of these can be hidden at any time.

Close the sidebar as shown
Turn off and start enhancement as shown
As shown in the picture, modify the shortcut of Bing here

Presumably everyone has realized the problem:

  1. When we wrote the above code in _vimrc, we did not specify that msedge.exe should use these two parameters when starting. Then when Edge is started, the user sees the previewed file, clicks on a new tab, and sees that the Edge initial interface will contain unhidden scroll bars and the “Bing Discovery” button, which is very unsightly, and the following This happens with all new tabs.
  2. If we add a code in _vimrc that specifies the use of these two parameters when msedge is started, it looks like this:
    pandoc "%" -f markdown + tex_math_dollars -t html --mathjax -s -o ./temprunfile.html & amp; & amp; start msedge --disable-features=msUndersideButton --enable-features= OverlayScrollbar,OverlayScrollbarWinStyle,OverlayScrollbarWinStyleAnimation & amp; start .\temprunfile.html & amp; & amp; start .\temprunfile.html
    Then if the user has opened the hidden Edge through the shortcut with modified parameters before pressing the [F5] button, vim will ignore the running Edge and open a new Edge window instead of adding an Edge label. If the user wants to merge two windows, the current window must be reduced first, then hold the mouse and drag to combine the two windows into one, and then use the Ctrl + Tab keys to switch between several windows , which is also very embarrassing.

So if we want to solve the situation we are facing now, we need to sort out our thinking. On the one hand, we hope to bring these two parameters when starting Edge, on the other hand, we hope to open the preview file directly in the existing Edge when Edge has been started with parameters and running. The flow of this procedure is as follows:

flow chart

Ideally, we hope that we can only press the keyboard twice, the first press [F5] to enter the preview, and the second press any key in the terminal opened by vim to close the preview and delete the temporary running file. Therefore, the above flow control must be implemented with two lines of code in the _vimrc file.

Therefore, we can use pipelines to implement if flow control.

cmd checks if Edge is running

Use tasklist to list all running programs, and the listed results can be passed to the find command to determine whether the process msedge.exe exists. The command used is as follows:

tasklist | find /i "msedge.exe"

On this basis, add the judgment of “if the find command is successfully executed (that is, the Edge process is successfully found), Edge will not be opened, otherwise Edge will be opened”. Use the pipe symbol || to concatenate the previous command and the command to run Edge with arguments:

tasklist | find /i "msedge.exe" || start msedge --disable-features=msUndersideButton --enable-features=OverlayScrollbar,OverlayScrollbarWinStyle,OverlayScrollbarWinStyleAnimation

Take a look at the effect of execution. When running in CMD for the first time, Edge was closed, and vim was successfully started at this time. Execute again, the feedback found Edge, but did not perform the operation of starting Edge.

The execution effect is shown in the picture

Integrating the above design

Plus a command that does this when Pandoc runs successfully and tries to open temprunfile.html regardless of the success of the above command:

pandoc "%" -f markdown + tex_math_dollars -t html --mathjax -s -o "temprunfile.html" & amp; & amp; tasklist | find /i "msedge.exe" | | start msedge --disable-features=msUndersideButton --enable-features=OverlayScrollbar,OverlayScrollbarWinStyle,OverlayScrollbarWinStyleAnimation & start .\temprunfile.html

Therefore, after modification, the complete function and shortcut key definitions in _vimrc should be:

" define shortcut key
map <F5> :w<CR>:call Run()<CR>
imap <F5> <ESC>:w<CR>:call Run()<CR>

" Write a function to implement the function
func! Run()
if expand("%:e") == "md"
pandoc "%" -f markdown + tex_math_dollars -t html --mathjax -s -o "temprunfile.html" & amp; & amp; tasklist | find /i "msedge.exe" || start msedge --disable-features=msUndersideButton --enable-features=OverlayScrollbar,OverlayScrollbarWinStyle,OverlayScrollbarWinStyleAnimation & amp; start .\temprunfile.html
:! del .\temprunfile.html
redraw!
echohl WarningMsg | echo "Running on browser ."
else
        redraw!
        echo "This is not a Markdown File."
    endif
end function

Final effect

When Edge has been started, the opened html preview will be added to the tab of the existing window, and when Edge is not started, Edge will be started with modified scroll bars and “Bing Discovery” will not be displayed, and preview files . Meanwhile, the LaTeχ formulas in the file appear fine.

The final preview effect is shown in the picture

And Edge’s new page tab is also clean, there is no annoying scroll bar, the picture loads normally, and there is no “Bing Discovery” warning.

This Bing looks much better

The only fly in the ointment is that vim will generate two small pop-up windows, you need to switch to the pop-up window and press any key twice.

Pop-up 1

Pop-up 2

For the browser, after deleting the local file, the browser can still display normally, so these two pop-up windows can be clicked casually.