Programming

How to Embed Mermaid Diagrams in CodeMirror with the Mermaid 13.0 Language-Service Plugin

A source-bounded implementation workflow for investigating the Mermaid 13.0 CodeMirror language-service plugin, checking its published package metadata, and avoiding invented APIs when the available release notes do not document its setup.

A Mermaid diagram can be useful in an editor before it is rendered: authors may want to recognize diagram syntax, edit it in context, and see a preview. But wiring that experience into CodeMirror depends on the exact extension API exposed by the package you install. A plausible-looking import is not enough; a package may expose different extensions, peers, or setup steps across releases.

The supplied first-party release URL identifies a Mermaid CodeMirror language-service package at 13.0.0-beta.5. The supplied Mermaid v13 alpha page says diagram-type support comes from diagram packages that load on demand. Those are useful starting points, but the available excerpts do not document the CodeMirror plugin’s exports, installation command, configuration, or rendering API. This tutorial therefore takes a verification-first approach: establish what the release actually contains, then integrate only the API documented by the package itself.

That distinction matters. The independent CodeMirror language-support project supplied here is a different package, codemirror-lang-mermaid. Its repository is useful background on Mermaid language support, but it is not evidence that the Mermaid 13.0 language-service plugin has the same API. Do not copy its imports or configuration into an application without checking the target package.

What this tutorial can and cannot establish

The release URL points to @mermaid-js/language-service-codemirror version 13.0.0-beta.5. The version is a beta release, not evidence that it is a stable or generally recommended production dependency. The supplied excerpts do not say whether this version provides syntax highlighting, diagnostics, completion, folding, live rendering, or any particular CodeMirror extension. Nor do they show a minimum CodeMirror version or a supported bundler setup.

The Mermaid v13 alpha page supplies one relevant architectural detail: diagram-type support comes from diagram packages and loads on demand. That points to a potential distinction between editor language services and diagram rendering. Do not assume that installing a CodeMirror language-service package also installs, configures, or renders every Mermaid diagram type.

The workflow below is deliberately hands-on, but it does not invent the plugin’s exports. It shows how to inspect the exact release and use its own package documentation as the source of truth before changing application code. If the published package does not include enough documentation to complete those steps, stop and obtain the missing API details rather than guessing.

1. Inspect the exact release before installing it

Start by checking the package metadata for the version named in the release URL. This command asks npm for metadata; it does not install or execute the package:

npm view @mermaid-js/[email protected] \
  name version description dist-tags dependencies peerDependencies exports types readme

Check the output for the fields relevant to your app:

  • version confirms which version npm resolves for that exact package specifier.
  • peerDependencies may identify CodeMirror packages the plugin expects the application to provide.
  • exports and types can show which entry points and type declarations are published.
  • readme may provide package-specific installation or configuration instructions.

These fields are inspection targets, not facts about this release: the supplied source excerpts do not provide their values. If the command reports that a field is absent, that does not prove the package lacks the corresponding behavior; it means you need another authoritative source, such as the package README or release notes, to establish it.

You can also inspect the package archive without adding it to your project:

npm pack @mermaid-js/[email protected] --dry-run

Review the reported file list for documentation, type declarations, and generated JavaScript entry points. A file list can help you locate the API description, but it cannot establish what an extension does. Read the actual documentation and declarations before writing imports.

If you need to inspect the package contents locally, pack the exact version and extract the archive in a temporary directory:

mkdir -p /tmp/mermaid-codemirror-inspect
cd /tmp/mermaid-codemirror-inspect
npm pack @mermaid-js/[email protected]
 tar -xzf mermaid-js-language-service-codemirror-13.0.0-beta.5.tgz

The archive filename is subject to npm’s package naming rules; use the filename printed by npm pack if it differs. Inspect package/package.json, its README, and any declaration files. Do not infer a public API just from internal file names.

2. Confirm CodeMirror compatibility in your project

Before adding the package, identify which CodeMirror generation and packages your application already uses. CodeMirror 6 is modular: applications compose extensions from packages rather than relying on one monolithic editor package. The independent codemirror-lang-mermaid repository supplied as background is explicitly about Mermaid language support for CodeMirror 6, but that does not establish compatibility for the Mermaid 13.0 plugin.

Use your project’s package manager to see what is already installed:

npm ls @codemirror/state @codemirror/view @codemirror/language

If you use pnpm or Yarn, use the equivalent dependency-list command for that package manager. Compare the installed versions with the target plugin’s declared peer dependencies, if it declares any. Resolve mismatches deliberately; do not add a second version of a CodeMirror package merely to silence an install warning without checking whether the editor and extension share compatible package instances.

If the package metadata does not reveal supported CodeMirror versions, treat compatibility as unverified. A successful package installation alone does not prove the extension can be used with your editor setup.

3. Find the documented extension entry point

Once you have the package documentation or declarations, identify the public API by answering these questions:

  1. Which named or default export should be imported?
  2. Does it return a CodeMirror extension, a language object, a service, or some other value?
  3. Does it accept options, and what are their documented types and defaults?
  4. Does it require a Mermaid renderer or any additional package?
  5. Does it provide only editor assistance, or does it also render a diagram?
  6. Are there documented limitations for diagram types or incomplete source text?

Write down the exact import and call signature from the package’s own documentation. Do not substitute an API from the older codemirror-lang-mermaid project. The two package names indicate separate projects, and the supplied material does not establish that their interfaces are interchangeable.

At this point, do not paste a guessed snippet such as mermaid() or mermaidLanguage(). Those names may look reasonable but are not supported by the excerpts available for this release. If the README is missing or the declaration files are ambiguous, consult the release’s linked source or ask the maintainers for a minimal example before proceeding.

4. Add the confirmed extension to a minimal editor

After you have verified the public API, integrate it into the smallest CodeMirror instance in your application. Keep the editor setup separate from any preview renderer until you know what the plugin itself provides.

A typical CodeMirror 6 application has an editor state, a view, and an array of extensions. The following is intentionally a structure, not executable plugin code: the placeholder must be replaced with the exact export and invocation documented for the installed release.

import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
// Import the plugin's documented public export here.

const state = EditorState.create({
  doc: `flowchart TD
  A[Start] --> B[Edit diagram]`,
  extensions: [
    // Add the verified plugin extension here.
  ],
});

const view = new EditorView({
  state,
  parent: document.querySelector("#editor"),
});

This snippet demonstrates the placement of extensions in a CodeMirror state; it does not claim that the Mermaid package has a particular export or that the example will work before you replace the comments. The supplied source excerpts do not include the plugin’s API, so a complete runnable integration cannot be responsibly provided from them alone.

Keep the initial document small and valid according to the Mermaid syntax you intend to test. Then test an incomplete line as well. An editor language service may behave differently while a user is in the middle of typing than it does on a complete diagram. The available excerpts do not establish how this plugin handles incomplete input, so observe the behavior rather than promising diagnostics or completion in advance.

5. Decide whether rendering is a separate feature

Do not equate editor support with preview rendering. The Mermaid v13 alpha page says diagram-type support comes from diagram packages that load on demand. That establishes an on-demand diagram-package model in the supplied source, but it does not document how to render from a CodeMirror document, which renderer entry point to call, or how to load a particular diagram package.

First verify whether the CodeMirror plugin’s documentation explicitly includes rendering. If it describes only language services, treat rendering as a separate integration. In that case, find Mermaid’s documentation for the exact v13 version and rendering API you are using, then implement the preview using that documented API. Do not assume a package import from an earlier Mermaid version remains valid in v13.

A safe editor-to-preview workflow is conceptually simple: read the current document, pass the text to a documented renderer, and display the result in a separate preview region. The specifics—render function, required imports, asynchronous behavior, error handling, and diagram package loading—must come from the version-specific rendering documentation. They are not present in the supplied excerpts, so this tutorial does not fabricate a render call.

Keep errors visible to the author. A preview should not silently show an old diagram after the source becomes invalid. The exact error-handling mechanism depends on the renderer API, so implement it according to that API’s documentation rather than assuming a particular exception or return shape.

6. Test the integration in the browser

Once the package API is verified and the editor builds, test the behavior you actually need. A useful manual test matrix is:

Input or action What to check
A short complete diagram The editor accepts the text and remains usable.
An incomplete diagram Editing does not depend on the source always being valid.
A syntax change Any documented language-service feedback updates as expected.
A different diagram type Support is verified rather than assumed.
A large document The editor remains responsive for your use case.
A preview, if implemented separately Preview errors are visible and stale output is not mistaken for current output.

These are test cases, not claims that the beta provides a particular feature or performance level. Record which checks pass against the exact package version and CodeMirror dependency set you ship.

If the editor fails during bundling, inspect the package’s documented exports and your bundler’s handling of the published entry points. If runtime behavior differs from the README, reduce the application to a minimal reproduction containing only the editor packages and the plugin. Include the package version and CodeMirror versions when reporting the issue; otherwise, it is difficult to distinguish an API mismatch from a dependency-resolution problem.

7. Pin and document the beta dependency

The release URL identifies 13.0.0-beta.5, which is a prerelease version. If you choose to test it, pin the exact version so a future install does not silently move to a different release:

npm install --save-exact @mermaid-js/[email protected]

This command is an example for npm. Use the equivalent exact-version option for your package manager, and review the resulting lockfile change. Pinning makes the selected version explicit; it does not make a beta stable or guarantee compatibility with your application.

In your project notes, record the verified import, required peer dependencies, and the behaviors you tested. Revisit that record when changing the plugin version. In particular, re-check whether the public API, CodeMirror compatibility, or Mermaid diagram-package behavior has changed; the supplied sources do not establish the compatibility policy for future releases.

Troubleshooting checklist

The package installs, but the import fails. Check the package’s exports field, README, and type declarations for the documented entry point. Do not guess an import based on the package name.

The editor rejects the extension value. Confirm from the plugin documentation that the value belongs in the CodeMirror extension array. The supplied excerpts do not say what the package exports, so an extension-shaped name is not proof that it returns an extension.

A diagram is highlighted but not shown as a preview. Language support and rendering may be separate concerns. The Mermaid v13 alpha excerpt describes diagram packages loading on demand, but does not say the CodeMirror plugin renders diagrams.

One diagram type works and another does not. Check the version-specific Mermaid documentation for that type and its package-loading requirements. Do not generalize from one diagram type to all of Mermaid.

The beta’s README does not explain setup. Stop short of production integration and obtain documentation or a maintainer example for the exact release. A tutorial that invents an API may appear runnable while sending you toward the wrong package surface.

The practical takeaway

The exact release URL gives a useful version to investigate: @mermaid-js/[email protected]. The Mermaid v13 alpha excerpt also supports one important caution: diagram-type support is associated with packages loaded on demand. But the supplied material does not document the plugin’s actual CodeMirror API, supported versions, or rendering behavior.

So the reliable implementation path is to inspect the exact package, verify its public exports and peer requirements, add only its documented extension to a minimal editor, and treat rendering as separate unless the package documentation says otherwise. If you cannot verify those details, the honest next step is to obtain the release documentation—not to fill in the gaps with a plausible-looking code sample.

External Sources used for this article

Related Articles

View all →