Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Extensions

This page demonstrates the theme extension functionality in myst-theme. Extensions are built from Federated Modules and export a well-defined interface.

What is an extension?

A MyST Theme extension is a JavaScript module that the theme loads at runtime. It ships new node renderers, and may ship more in the future. These renderers re-use the main theme application’s React context and other shared modules.

Example

Once MyST Theme has loaded an extension, it uses the extension’s renderers whenever it renders a matching node.

For example, here is a snippet that creates a fancyNote node. The {fancy-note} directive comes from a small MyST plugin that this site loads.

:::{fancy-note} A custom node
This node type does not exist in the theme. Its renderer and CSS were loaded at runtime.
:::

produces:

Writing an extension

Any extension module must provide a v1 named export with the following specification:

const v1: ThemeExtensionV1['v1'] = {
  renderers: NodeRenderers
};
export { v1 };

The @myst-theme/providers package exports the NodeRenderers type, which is defined in packages/providers/src/renderers.tsx. For an example of NodeRenderers in use, see ANY_RENDERERS in the @myst-theme/anywidget package.

Building an extension

Each extension must be built from a JavaScript or TypeScript project with the Vite build system. The Vite config needs two important pieces:

This configuration can be a little fiddly, so we recommend copying the demo’s vite.config.mjs as a starting point. In the future, we will likely build tooling to bootstrap this process. We track this work in jupyter-book/mystmd#2620.

The two most important parts of the MF config are:

To build the project, you’ll need several Vite and MF dependencies. See the demo’s package.json for the list. The demo installs @myst-theme/federation from a folder in this repository, so replace that path with the published package when you copy it.

After you run vite build and vite build --ssr, the built extension bundle should contain at least these three files:

Loading an extension

To load an extension into MyST Theme, add it to the site.options.extensions array as a string of the form name@entrypoint, with no spaces. Use the same name that you gave the federation plugin in your vite.config.mjs. Both mf-manifest.json and <entry>.js are valid entrypoints. We recommend using mf-manifest.json.

For now, we strongly encourage you to ship theme extensions with the project, as static files under _public. To do this, add the build output directory, such as dist/fancy-note/, to project.static_files. Then define the entrypoint relative to the public path, as name@/<folder>/mf-manifest.json. Here, / means the folder where project.static_files puts its files. During SSR, the theme loads these files from the content server. In the browser, it loads them from URLs that start with /_public/.

For example, this documentation site loads the demo extension with this config in its myst.yml:

project:
  static_files:
    - src/theme-extension/dist/fancy-note # served at /fancy-note/
site:
  options:
    extensions:
      - theme-extension@/fancy-note/mf-manifest.json

Running the demo locally

To build the demo extension and preview this page, start from the root of this repository:

make build-book    # builds the theme that these docs use
cd docs/src/theme-extension
bun install
bun run build      # runs `vite build` and `vite build --ssr`
cd ../..
myst start