Skip to content

Lightweight Markdown Preview, One Year Later: D2, Mermaid, and Math in VS Code

A year ago, I released Lightweight Markdown Preview, my first VS Code extension. Over the following year, I added D2 diagrams, an outline of your headings, support for every VS Code color theme, and the security work from my security check-up.

The extension is free and open source. It shows a live preview beside your Markdown file with D2 and Mermaid diagrams, math formulas, and highlighted code. This post shows what the preview does today and where to get it.

Navigate this post

What it does

Open a Markdown file and click the eye icon at the top of the editor. The preview opens beside your file and updates as you type. When you switch to another Markdown file, the preview switches with you.

One preview page showing a D2 network diagram, a Mermaid flowchart, and a math formula in a dark theme

  • D2 diagrams
    Architecture diagrams written in D2 appear in the preview as you type.

  • Mermaid diagrams
    Mermaid, another text format for diagrams, works in both the GitHub and the Azure DevOps style.

  • Math and code
    LaTeX formulas display as formatted math, and code is colored by language.

  • Outline sidebar
    A panel lists your headings and follows where you read.

Architecture diagrams with D2

D2 is a text language for architecture diagrams. It draws nested containers, database tables, and sequence diagrams. Because each diagram is text in your repository, you review a change to a diagram the same way you review a change to code.

VS Code with a D2 diagram written in a Markdown file on the left and the drawn diagram in the preview on the right

D2 diagrams need a renderer, the program that turns D2 text into a picture. You pick one once: d2-renderer, a small container I also built, running on your own computer; the public Kroki service; or a server of your own. With d2-renderer running locally, your diagram text never leaves your computer.

Mermaid, math, and code

Mermaid diagrams written for GitHub and Mermaid diagrams written for Azure DevOps both work, even in the same file.

The same Mermaid sequence diagram drawn from a GitHub-style block and from an Azure DevOps-style block

Math uses the usual $...$ and $$...$$ markers. Dollar amounts such as "$5 and $10" and shell variables such as $HOME display as ordinary text. Code blocks are colored by language.

A math formula and a colored Python function in the preview, light theme

Outline and themes

The ☰ button opens an outline of your headings. The outline follows where you read, and links to headings scroll the preview to the right place.

The outline sidebar listing the headings of a design document beside the preview

The preview uses your VS Code color theme, whether light, dark, or high contrast, and redraws when you change it.

A Mermaid flowchart and a formula in the preview, matching the editor's color theme A Mermaid flowchart and a formula in the preview, matching the editor's color theme

Privacy and security

  • Document scripts blocked: the preview does not run scripts that a Markdown file contains, so a file from the internet, an email, or an unfamiliar repository cannot run code in the preview.
  • No tracking: the extension collects no data and needs no account.
  • Known network requests: the preview downloads its libraries from jsDelivr at fixed versions, loads the images your document names, and sends D2 diagram text to the renderer you choose.
  • Works in untrusted folders: the preview runs in VS Code's Restricted Mode, which VS Code uses for folders you have not marked as trusted.

Get started

Lightweight Markdown Preview is available for VS Code on the Visual Studio Marketplace, and for editors such as VSCodium and Cursor on Open VSX. The source code and documentation are on GitHub.

Conclusion

A year on, Lightweight Markdown Preview shows D2 and Mermaid diagrams, math, and code beside your Markdown, with an outline for long documents. Install it from the Marketplace or Open VSX, and find the source and documentation on GitHub.

References and further reading

Open the complete reference catalog

Primary Sources