# Using a plugin

A Récit plugin is an ordinary npm package, added to `recit.config.ts`. It can bring directives and
components for your decks and pages, blocks for your films, themes, `recit export --to` targets,
data sources, commands and AI jobs.

**The plugin API is experimental**, except for the
[v1 data source contract](/documentation/plugins/data-sources). Outside that contract, its names may
change in a minor release, with a note in the changelog. Only the `plugins` key of the config and
the data source contract are in the v1 stability promise. See
[Stability](/documentation/reference/stability#experimental).

## Add one

```bash
npm install recit-plugin-lorem
```

```ts
// recit.config.ts
import { defineConfig } from "recit/config";
import lorem from "recit-plugin-lorem";

export default defineConfig({
  plugins: [lorem({ accent: "violet" })],
});
```

A plugin is a function: you call it with its options, and Récit checks them when it loads the
plugin, before anything else. A plugin written for another version of the plugin API is refused,
with what to update.

`recit plugins` lists the plugins of the project, their version, what each one brings, and the
weight each added to the page at the last build. `recit build` warns past 30 kB brotli per plugin
(`--plugin-budget <kB>`); the build of a site weighs its plugins the same way and takes its budget
from `publish.pluginBudget` (kB) in its `recit.config`.

## What a plugin brings

| Capability  | How you use it                                                       |
| ----------- | -------------------------------------------------------------------- |
| Directives  | `:::lorem-quote{author="…"}` in a story, in Markdown and in its twin |
| Components  | mounted in the browser, loaded only by a slide that uses them        |
| Film blocks | a scene `type=lorem-counter` in a film                               |
| Themes      | `theme: { name: lorem-dusk }`, for a deck or a film                  |
| Exporters   | `recit export slides.md --to lorem-txt`                              |
| Sources     | `data: { sales: { source: lorem-rows } }`, read at build time        |
| Commands    | `recit lorem hello`, always prefixed with the plugin's name          |
| AI jobs     | `recit ai deck.fromText` ([AI jobs](/documentation/plugins/ai))      |

A directive of a plugin has a schema: `recit validate` checks it in a `.md` and in a `.json` alike,
and `recit schema` adds it to the JSON Schema of the project. A story that uses a plugin's directive
or block needs that plugin to build: without it, `recit validate` says `plugin-missing`.

## Secrets

A key, a token or a license is never written in the config. A plugin takes the name of an
environment variable, `{ env: "LOREM_TOKEN" }`, and reads it at build time or when a job runs; a key
written in clear is refused. Nothing a plugin reads at build time reaches the built site: a data
source is read in Node, and only its rows go to the page.

## Security

- A plugin is code you install. It runs with your rights, like a Vite plugin. Récit does not isolate
  it and does not check it.
- Install a plugin as a dependency: read who publishes it, pin its version.
- An AI plugin sends your content to a service: the provider (your key) or the plugin's author (its
  license). The plugin must say so in its README.
- Your keys go in environment variables (`{ env }`), never in the config nor in the published site.

## In a site

A site evaluates its `recit.config.ts` in the browser too. Every plugin package it imports must
serve a `browser` entry, which holds its components and nothing else, so that its schemas and its
Node code never reach a visitor. [Writing a plugin](/documentation/plugins/writing) shows the two
entries. `recit validate`, `recit plugins` and the site build report a package without one
(`plugin-no-browser-entry`), and the build stops.

The stories of a site (`publish.dir`) render the plugins' directives and mount their components, as
a deck built by `recit build` does: list the plugin in the one `plugins` array of the site's
`recit.config.ts`, beside the site's own plugins.

The Markdown and MDX pages of the site render them too. A static directive (`::lorem-quote`) becomes
the plugin's element; an interactive one (`:::lorem-counter`) mounts the plugin's component, with
its attributes as props and its content as children, and the component loads with the page. A
directive the plugin refuses stops the build with the page and its line, as in a story. In an MDX
page, the plugin's component is also there by its name: `<LoremCounter start={3} />`. A component of
the site with the same name wins.

## Limits (experimental)

- **An `.mdx` deck does not render a plugin's directives.** Write the deck in `.md` to use them.
- The plugin API is outside the v1 stability promise, except for the
  [v1 data source contract](/documentation/plugins/data-sources)
  ([Stability](/documentation/reference/stability)): the rest may change in a minor release.
