Skip to content

About

Neovim plugin: two-way markdown preview in the browser. Edit paragraphs, headings, lists and tables on the page and they are written back into the buffer.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

markbridge.nvim

Edit your markdown in the browser and watch it land in Neovim.

CI Release Neovim 0.10+ License: MIT

markbridge renders the file you are editing in a real page (diagrams, math, task lists, tables, highlighted code) and lets you type straight into the rendered text. Every change is written back to your Neovim buffer as markdown, within about half a second. Edit in Neovim and the page follows.

More Drumandbytes projects

markbridge showing a launch plan: formatting bar on top, frontmatter fields, a checklist and a table, all editable in the page

Why

Browser previewers for Neovim are one-way: you read the rendering and go back to the source for every tweak. That is awkward when you co-write a document with a person, or with an AI agent, who wants to click into the rendered text. markbridge closes the loop without taking Neovim out of the middle:

  • Neovim stays the source of truth. The page edits your buffer; it never owns the file.
  • Edits are patched by line range, one block at a time, so changing a paragraph never rewrites the rest of your document.
  • Conflicts are handled like a git merge instead of being clobbered or silently dropped.

Features

  • Edit paragraphs, headings, lists, task items and table cells in place, with a formatting bar and keyboard shortcuts. Tab nests list items, Shift+Tab moves them out.
  • Click a code block, mermaid diagram, $$ math block or raw HTML block to edit its source; HTML blocks re-render as you type.
  • Preview and edit .html files too: edit text right in the rendered page or in the source next to it. Linked local stylesheets open as tabs and restyle the page live, including unsaved edits in Neovim; remote ones are shown read-only.
  • Arrange the HTML view like a window manager: preview, source and stylesheets are tabs that you split side by side or stacked, drag between panes, resize and hide. Markdown gets the same idea in a smaller form: a source panel beside or under the page.
  • Frontmatter fields (key: value) become inputs; table rows and columns can be added and removed.
  • Block menu: move, delete, convert or insert blocks. Paste or drop an image and it is saved next to the file.
  • Mermaid, KaTeX math, emoji, syntax highlighting, task lists and GitHub-flavoured tables render out of the box.
  • Light, dark or auto theme, configurable.
  • Save from the page, undo and redo through Neovim's own undo.
  • Changes made to the file by other tools are picked up, and a three-way merge settles real conflicts.
  • No Node, no build step, no runtime dependencies: the browser libraries are vendored and checksum-pinned.

The block menu: move up, move down, delete, turn into, insert below   A table with buttons above it to add and remove rows and columns

Requirements

  • Neovim 0.10 or newer. It is developed on 0.12; CI runs the current stable release.
  • A browser. The end-to-end suite runs against Chromium (Brave or Chrome), Firefox and Safari on macOS, and Chromium and Firefox on Linux. Safari cannot edit the rendered HTML page unless you allow it (see Safari).
  • macOS and Linux are used day to day. Windows is untested.

Install

With the built-in package manager (Neovim 0.12):

vim.pack.add { { src = 'https://github.com/drumandbytes/markbridge.nvim', version = vim.version.range '*' } }
require('markbridge').setup {}

With lazy.nvim:

{
  'drumandbytes/markbridge.nvim',
  version = '*',
  cmd = 'Markbridge',
  opts = {},
}

Nothing to build. Any other plugin manager that puts the repository on the runtime path works.

Quick start

  1. Open a markdown file in Neovim.
  2. Run :Markbridge (works on markdown and html buffers). Your browser opens http://markbridge.localhost:6274/….
  3. Click into the page and type. Look at your buffer.
  4. :Markbridge stop (or just quit Neovim) when you are done.

Try it on the bundled examples/demo.md, which has every kind of block, or examples/demo.html with its stylesheet.

A mapping to start it:

vim.keymap.set('n', '<leader>mp', '<Cmd>Markbridge toggle<CR>', { desc = 'Markdown browser preview' })

Commands

Command Does
:Markbridge / :Markbridge toggle Start for the current buffer, or stop if running
:Markbridge start Start, or retarget to the current markdown buffer, and open the browser
:Markbridge stop Stop the server
:Markbridge url Print the page URL (it carries a per-session token)
:Markbridge status Running or stopped
:Markbridge theme [auto|light|dark] Show or set the page's default theme; open pages adopt it right away

Lua: require('markbridge').start() / .stop() / .toggle() / .url() / .is_running() / .set_theme(name). Health check: :checkhealth markbridge. Help: :help markbridge.

Configuration

These are the defaults:

require('markbridge').setup {
  port = 6274,           -- preferred port; a free one is used if it is busy. 0 = always a free one
  open = true,           -- open the browser on start; or a function(url)
  friendly_url = true,   -- open http://markbridge.localhost:<port>; false uses 127.0.0.1
  follow = true,         -- retarget when you enter another markdown buffer
  scroll_sync = true,    -- scroll the page to the block under the Neovim cursor
  watch = true,          -- reload the buffer when the file changes on disk (never over unsaved edits)
  theme = 'auto',        -- page theme: 'auto' (follow the OS), 'light' or 'dark'
  images_dir = 'assets', -- where pasted images go, relative to the markdown or html file
  remote_css = 'ask',    -- https stylesheets of an HTML page: 'ask' (click to load a read-only copy), 'auto' or 'never'
  safari_edit = false,   -- let Safari edit the rendered HTML page (see Safari below); the page also has a switch
}

Editing in the browser

Block How you edit it
Paragraphs, headings (#), list items, task items, table cells, also inside quotes and lists Type, use the formatting bar or shortcuts, paste (as plain text), click a task checkbox
Fenced code, mermaid, $$ math, top-level HTML blocks Click to open the source in a text box; the page re-renders when you leave it (Esc). HTML blocks update live while you type
Frontmatter Flat key: value lines are fields at the top of the page; lists and block values stay read-only
Enter Splits the block at the caret; at the end it opens an empty block below
Shift+Enter A hard line break
Tables Tab and Enter move between cells, and Tab in the last cell adds a row. Buttons above the focused table add or remove rows and columns after the current cell; column alignment (:--, :-:, --:) stays with its column
Images Paste or drop an image into a paragraph: it is saved to assets/ next to the file and linked
Click a block Moves the Neovim cursor to its first line

Lists

  • Tab nests an item under the one above it; its children move with it. Shift+Tab moves it back out.
  • Enter at the end of an item opens a new one. Enter on that empty item moves it out a level, and at the top level it becomes a paragraph after the list.
  • Turning an item into a bullet, numbered or task item converts the whole list. Nested lists keep their own type.
  • An item that has nested items is editable too: you edit its own text, and the nested list is left alone.

A code block opened for editing as plain text

HTML files

With an .html buffer the page shows the rendered page on the left and the source and stylesheets on the right, as tabs in panes you can rearrange (see Panes below).

  • Edit in the rendered page. Text with inline formatting (headings, paragraphs, list items, table cells, links, <div>s that hold only text) is editable in place. Cmd/Ctrl+B and I work, Enter is a line break, paste is plain text, Esc leaves. An edit rewrites only that element's content in the source; everything else stays byte for byte. Inside the edited element, markup is re-serialised by the browser: attribute quoting is normalised and &copy; comes back as ©. Elements that mix text with blocks (an outer <div> holding <p>s), tables, lists, forms, SVG, anything with an implied end tag (<li> without </li>) and <script>/<style> content are not editable in the page; use the source for those. Click an element and the Neovim cursor goes to its line.
  • Edit the source. The text box on the right patches only the changed lines of your buffer.
  • Stylesheets open themselves. Every local <link rel="stylesheet"> (and @import) in the file's folder gets a tab and is loaded as a buffer in Neovim (listed, not shown in a window), so edits from the page, from Neovim, or from another tool all restyle the preview live, including unsaved ones. Save writes the HTML and the stylesheets you changed. CSS is plain text: no reformatting.
  • Remote stylesheets (https://…) get a locked, read-only tab with a warning. Your browser loads them for the preview; click Load a read-only copy and Neovim fetches it with curl (only a URL that is in your file). With the default remote_css = 'ask' nothing is fetched until you click; 'auto' loads them as soon as the page does, 'never' turns it off.
  • The preview runs in a sandbox with scripts disabled, so <script> and inline handlers do nothing, and links don't navigate it away.

Panes

Dragging the stylesheet tab under the preview, then changing a colour in it and watching the page restyle

The HTML view is a small window manager. The preview, the source and every stylesheet are tabs; each tab lives in a pane.

  • Drag a tab by its title onto another pane's tab bar to move it there. Drop it on the edge of a pane (the outer 28%) to split that pane: left or right makes side-by-side panes, top or bottom stacks them. Splits nest, so you can mix both.
  • Drag the line between two panes to resize them. A pane left empty by a move goes away.
  • The buttons in the top bar put the source beside the page, put it under the page, hide or show it, or reset the layout. Hidden shows only the preview and keeps the rest of the layout for when you bring it back.
  • The layout is remembered in your browser. It is stored per address, so a run on a fallback port starts from the default.

An HTML page with its stylesheet split under the preview, both editable

Markdown source panel

The raw markdown of your buffer, next to the rendered page. It is hidden until you ask for it with the top bar buttons: beside the page (right), under it (bottom), or hide. Drag its edge to resize it and double-click the edge to reset the size. Typing in it edits the Neovim buffer through the same path as the page, and the page re-renders from it; edits in the page show up in it. The choice of side and size is remembered in your browser.

The rendered page with its markdown source in a panel on the right

Safari

Safari delivers no events to a frame that has scripts turned off, which is how every rendered HTML page is isolated. So in Safari the rendered HTML page is read-only by default, with a note that says so, and you edit the source tab instead. Markdown is not affected.

To edit the rendered page in Safari, turn on the pencil button in the top bar (it only appears in Safari, and the choice is remembered), or set safari_edit = true for everyone. Safari then gives the frame allow-scripts and allow-same-origin, and the page puts a Content-Security-Policy that blocks every script, inline handler and eval at the very top of the document. Scripts in your HTML still cannot run. The end-to-end suite checks both states. If you open untrusted HTML files in Safari, keep the default.

Markdown that is read-only in the page

HTML blocks nested in lists or quotes, indented code nested in lists, footnote definitions and definition lists (Term / : definition), inline footnotes (^[note]), link definitions ([ref]: url), and frontmatter that is not a flat key: value. Reference links, autolinks, reference images and footnote references are shown and kept as written; their text is not editable in the page, the text around them is. Setext headings keep their underline, indented code edits as source like a fenced block.

Shortcuts (Cmd on macOS, Ctrl elsewhere): B bold, I italic, Shift+X strikethrough, E inline code, K link, Alt+0 to Alt+6 paragraph or heading level, Tab / Shift+Tab (no modifier) nest and outdent a list item, Shift+7 / Shift+8 / Shift+9 numbered, bullet and task list, S save, Z / Shift+Z undo and redo, / for the in-page help.

Top bar

  • Formatting, block type (paragraph, headings) and list buttons.
  • Undo and redo run Neovim's own undo, so browser edits and Neovim edits share one history.
  • Save writes the file.
  • Refresh reloads from Neovim, and re-reads the file from disk when the buffer has no unsaved edits.
  • Theme and help.

The in-page help panel listing the shortcuts

What a browser edit does to your markdown

An edited block is rewritten from the page; everything outside the block is untouched, byte for byte. Inside the block:

  • Soft-wrapped lines are kept. Emphasis normalises to *x* and **x**, and some characters get escaped (2 * 3 becomes 2 \* 3).
  • Tables are re-padded.
  • Math ($x$), emoji (:smile:) and inline HTML round-trip: editing text around them writes their source back unchanged.

Keeping in sync

Neovim's buffer is the source of truth, and nothing is overwritten without you choosing it.

  • You edit in the page while Neovim changed something else. The page reloads from Neovim and re-applies your edit where its block now is. Nothing to do.
  • You edit a block that Neovim changed too. A dialog shows your version next to Neovim's: keep Neovim's, use yours, or copy yours.
  • The file changed on disk (another tool, another person, an AI agent):
    • If the buffer is clean, a file watcher reloads it and the page follows.
    • If the buffer has unsaved edits, markbridge keeps them and offers Resolve. It does a three-way merge against the version Neovim last read. Changes in different places combine on their own; overlapping changes are listed, and for each you pick mine, the file's or both. The result is written as one undo step.

The Resolve dialog: one conflict between the buffer and the file, with options to keep mine, take the file's or both

  • Undo is Neovim's: one browser edit is one undo step.
  • Saving is the Save button, Cmd/Ctrl+S, or :w. Saving from the page refuses to overwrite a file that changed on disk and opens Resolve instead. A buffer without a file name needs :w <name> first.
  • Several buffers: with follow the preview retargets to whichever markdown buffer you enter. Closing the previewed buffer stops the server.

Themes

Auto follows the OS and switches with it; light and dark force a theme. theme sets the default and the button in the top bar overrides it per browser (remembered). :Markbridge theme dark wins again whenever the Neovim-side setting changes. Mermaid diagrams and code highlighting follow the theme. The page is responsive: the top bar scrolls on narrow windows.

The page at phone width: the top bar scrolls sideways

Security and privacy

  • The server listens on 127.0.0.1 only, on port 6274 (or a free port when that is busy). The page opens at markbridge.localhost, which browsers resolve to your own machine; friendly_url = false uses the bare IP.
  • Every request that reads or writes the buffer needs a random per-session token in an X-Token header (a custom header, so another website can't send it cross-origin). Requests must also carry our own Host (<label>.localhost, localhost or 127.0.0.1 on our port), which defends against DNS rebinding.
  • Images, stylesheets and fonts are served only from the previewed file's folder, with a same-origin resource policy. The preview frame fetches them itself and can't send a header, so they need a cookie (HttpOnly, SameSite=Strict, named for the port) that the page sets when it loads with the token. Another website or another local user does not have it. Source files are never served. Request bodies over 25 MB are refused, and a request that never finishes its header is cut off at 64 KB.
  • The page is served with a Content-Security-Policy that only allows its own scripts, so raw HTML in a document (<img onerror=…>, javascript: links, <script>) cannot run in it or reach the token; HTML blocks and HTML files render in a script-less sandboxed iframe. Responses carry nosniff and Referrer-Policy: no-referrer, and the page refuses to be framed.
  • No telemetry. The only network access is a read-only copy of a remote stylesheet (on click by default, see remote_css), fetched by Neovim with curl for a URL that appears in your file. With remote_css = 'auto' redirects are not followed, so a document cannot send Neovim to an address it did not name. The browser libraries are vendored in the repository.

Report a vulnerability as described in SECURITY.md.

Limitations

  • One preview server per Neovim instance; a second Neovim falls back to another port.
  • Safari cannot edit the rendered HTML page unless you allow it (see Safari).
  • HTML previews are static: no scripts, and only local stylesheets (with @import), images and fonts load live from the file's folder.
  • Markdown flavour is CommonMark plus GitHub-style tables and task lists, emoji, footnotes, definition lists, $…$ / $$…$$ math, mermaid and flat frontmatter.
  • Not everything is editable in the page (see above); edit those parts in Neovim.
  • Browser edits change the buffer, not the file, until you save.
  • Pre-1.0: details, including configuration, may still change between minor versions.

Troubleshooting

  • "nvim preview stopped" in the page: the Neovim session ended. Start it again and open the new URL.
  • The browser can't resolve markbridge.localhost: set friendly_url = false.
  • Port in use: markbridge falls back to a free port and prints it; :Markbridge url shows the current URL.
  • Over SSH: the server runs where Neovim runs. Forward the port (ssh -L 6274:localhost:6274 host), start with open = false, and open the printed URL locally.
  • Nothing happens on :Markbridge: the buffer must have the markdown or html filetype. :checkhealth markbridge checks the rest.
  • "nvim changed first" or a conflict dialog: you and Neovim edited the same block. See Keeping in sync.

Documentation

License

MIT, see LICENSE.

About

Neovim plugin: two-way markdown preview in the browser. Edit paragraphs, headings, lists and tables on the page and they are written back into the buffer.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages