Skip to content

About

Neovim markdown previewer by mo cli

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

32 Commits

Folders and files

Repository files navigation

mo-markdown-preview.nvim

Preview the file backing the current Neovim buffer in your default browser, powered by mo.

The plugin is deliberately small: it reads the current buffer's file name, calls the mo CLI with that path, and opens the resulting URL. It does not copy buffer contents to a temporary file or sync unsaved changes to mo.

Features

  • :MoPreview command to open the current buffer's file in mo.
  • :MoPreview --close to close the file again from the same group.
  • :MoPreview --clear to empty a whole mo port in one call.
  • :MoPreview! as a shorthand for --open that always wins.
  • Uses the real file on disk, so mo's native live-reload works on save.
  • Opens the exact file's deeplink, not just the mo server root.
  • Supports mo groups and custom ports.
  • Per-call flags --target, --port, and --no-open/--open override the configured target, port, and open; --close switches the command to closing.
  • Puts each file in its own mo group named after its URI-encoded full path; pass --target to share a group explicitly.

Requirements

  • Neovim 0.10 or newer (vim.system is required).
  • mo 1.7.0 or newer, available as mo on $PATH (or set cmd in the setup).

Installation

lazy.nvim

{
  "linrongbin16/mo-markdown-preview.nvim",
  cmd = { "MoPreview" },
  opts = {},
}

packer.nvim

use({
  "linrongbin16/mo-markdown-preview.nvim",
  config = function()
    require("mo-markdown-preview").setup()
  end,
})

vim-plug

Plug 'linrongbin16/mo-markdown-preview.nvim'

Manual

git clone https://github.com/linrongbin16/mo-markdown-preview.nvim \
  ~/.local/share/nvim/site/pack/plugins/start/mo-markdown-preview.nvim

Usage

Open a file and run:

:MoPreview

The default browser opens on that file in mo. Running :MoPreview again adds the same file to its group (deduplicated by absolute path) and opens its URL again.

By default each file gets its own group named after its full path (with forward slashes on Windows), so opening several files does not mix them in one mo sidebar. The path is URI-encoded with the RFC 3986 rules: URL-path characters (letters, digits, and symbols such as -._~/:&+=) are kept, and everything else (spaces, CJK characters, ?, #, %, control characters) becomes %XX, for example a space becomes %20 and 中 becomes %E4%B8%AD.

All flags are optional. With no flags, target, port, and open come from the plugin configuration (config.lua defaults, optionally overridden by setup()); a nil target uses the URI-encoded full path.

Pass --target to choose a group yourself:

:MoPreview --target docs
:MoPreview --target default

Use --port to talk to a mo server on a specific port for this call. It overrides port from setup():

:MoPreview --port 6276

Use --no-open to only report the preview URL without opening a browser:

:MoPreview --no-open

Use --open to force opening the browser for this call, even when open = false is configured:

:MoPreview --open

! is a shorthand for --open and :MoPreview! is the same as :MoPreview --open. --no-open and --open override each other, so the last one wins, while ! has the highest priority: it wins no matter where --open or --no-open appear, so :MoPreview! --no-open still opens the browser.

The target and port flags can be combined with one of the mode flags, and --flag=value is accepted as well. Quote values that contain spaces:

:MoPreview --target docs --port 6276 --no-open
:MoPreview --target="my group"

Close the file again with --close:

:MoPreview --close
:MoPreview --close --target docs
:MoPreview --close --port 6276

Empty the whole mo port with --clear:

:MoPreview --clear
:MoPreview --clear --port 6276

--close runs mo --close with the same target and port resolution as opening, so the defaults match. If you opened with --target or --port, pass the same flags (or configure them) to close the file. Opening and closing are different modes, so --close conflicts with --open, --no-open, and !; combining them is an error.

--clear runs mo --clear: it shuts the server down, drops the saved session for that port, and starts a fresh, empty server on the same port. Already open preview tabs reconnect to the new server and show an empty list. Because it works on a whole port instead of a file, it takes no buffer, needs no file on disk, and conflicts with --close, --no-open, --open, !, and --target. Only --port applies.

The command requires the buffer to be backed by a readable file. If it is not, the plugin reports an error and does not call mo. Save the buffer to refresh the preview. mo watches the file and reloads it, but unsaved edits are never sent to mo.

Configuration

setup() is optional; the defaults below are already active.

require("mo-markdown-preview").setup({
  -- Command used to run mo: name on $PATH or absolute path.
  cmd = "mo",

  -- Default mo group. nil uses the URI-encoded full path.
  -- Overridden by --target for one call.
  target = nil,

  -- mo server port. nil uses mo's default port (6275).
  -- Overridden by --port for one call.
  port = nil,

  -- Open the preview URL in the default browser.
  -- Overridden by --no-open / --open / ! for one call.
  open = true,
})
Option Default Description
cmd "mo" Command used to run mo: name or absolute path.
target nil Default mo group. nil uses the URI-encoded full path.
port nil mo --port. nil means mo's default (6275). --port overrides it.
open true Open the URL in the default browser. Set false to only show the URL.

Command flags override these options for a single call: :MoPreview accepts --target, --port, --close, --clear, --no-open, and --open, plus the ! modifier as a higher-priority --open. --close conflicts with --open, --no-open, and !. --clear conflicts with all of them and with --target.

The Lua API is also available:

local mo = require("mo-markdown-preview")
mo.open()                      -- open the current buffer
mo.open({ bufnr = 3 })         -- another buffer
mo.open({ target = "docs" })   -- explicit mo group
mo.open({ bufnr = 3, target = "api" })
mo.open({ port = 6276 })       -- one call on another port
mo.open({ open = false })      -- only report the URL
mo.open({ open = true })       -- force opening the browser for this call
mo.open({ target = "docs", port = 6276, open = false })
mo.close()                     -- close the current file
mo.close({ target = "docs" })  -- close from a specific group
mo.close({ port = 6276 })      -- close on another port
mo.clear()                     -- empty the configured port
mo.clear({ port = 6276 })      -- empty another port

How it works

  1. Get the file name from the current buffer.
  2. Check that the path exists and is readable. If not, report an error.
  3. Run mo --no-open --json [--port PORT] --target GROUP <file>. PORT comes from --port, then the port option. GROUP comes from --target, then the target option; when both are nil, GROUP is the file's full path (with forward slashes on Windows) URI-encoded with RFC 3986; URL-path characters are kept and everything else becomes %XX.
  4. Parse mo's JSON output, find the deeplink for <file>, and open it with vim.ui.open. With --no-open (or open = false) the URL is only reported.

mo always runs with --no-open; the --open flag and the ! modifier only control whether the plugin calls vim.ui.open. That keeps a single call to one browser tab, and the tab always lands on the exact file's deeplink.

:MoPreview --close runs mo --close [--port PORT] --target GROUP <file> with the same target and port resolution, then reports success or the error from mo.

:MoPreview --clear runs mo --clear [--port PORT] with y written to its standard input. mo exits 0 both when it clears and when it cancels, so the plugin reads mo's stderr to tell the two apart and reports which one happened.

No file is written, no buffer contents are copied, and no autocmds are installed. mo reads the file from disk and handles reloading when Neovim saves it.

The plugin does not manage the mo server lifecycle by itself: it never shuts the server down unless you ask for it. mo is a shared, long-lived preview server that restores sessions across invocations. Use :MoPreview --clear to empty a port and restart its server, and mo --shutdown to stop it without touching the saved session.

Notes and limitations

  • The default group is the URI-encoded full path. Characters allowed in a URL path (letters, digits, and symbols such as -._~/:&+=) are kept; spaces, CJK characters, ?, #, %, and control characters become %XX (a space becomes %20, 中 becomes %E4%B8%AD). The mo sidebar shows the encoded group name, while file entries keep their real names. Encoding matters because mo compares the group name with the percent-encoded URL path without decoding it, and it is injective, so distinct files always get distinct groups.
  • Unsaved changes are not previewed. Save the file and let mo reload it.
  • Buffers without a file on disk (unnamed buffers, oil://, terminal buffers, and so on) are rejected with an error message.
  • On Windows, mo servers started in WSL (or in any other environment that shares the port) cannot read Windows paths such as C:\.... The Windows mo client still exits 0 when such a server rejects the file, so the plugin reads the mo: added 0 item(s) line on stderr and reports it, naming the port. Stop the other server with mo --shutdown in that environment, or preview on a free port with --port or the port option.

Development

The tests run with headless Neovim and a fake mo executable, so they do not need mo or a browser:

make test

Run them from a POSIX shell (Linux, macOS, or WSL): the fake mo is a shell script, and Windows cannot execute it, so every test that calls mo fails there. On Windows, run make test from WSL against the checkout on /mnt/c.

Lua sources are formatted with StyLua using the settings in .stylua.toml (2-space indent, 120 columns):

make fmt         # reformat lua/ plugin/ tests/
make fmt-check   # fail if anything is unformatted

License

MIT

About

Neovim markdown previewer by mo cli

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages