Files
RuneStone/packages/plugin-sdk/README.md
T
Knotty Wyvern bb0b8f88b6 Add the versioned plugin SDK package and its release script
The new package packages/plugin-sdk (@runestone/plugin-sdk) is the one source of the public plugin types.
It contains PluginApi and its members, the manifest type, API_VERSION, and feature checks
(hasFeature, requireFeature, requireFeatures, missingFeatures). It also contains the action icons and
the tooltip helper as DOM functions, because RuneStone has no API for them. Menus stay host-drawn through
api.ui.showActionMenu, so the SDK contains only the menu types. The SDK uses the Apache License 2.0.

The host imports the SDK with relative paths in src/lib/plugins/sdk.ts, src/lib/ui/actionIcons.ts, and
src/lib/ui/tooltip.ts. The completion, Git, background, and contribution contract files export the SDK types.
The menu files keep their types, because the Terminal plugin keeps copies of them. A unit test makes sure
that those types and the SDK types are the same. The menu inventory now scans the SDK source.

pnpm sdk:build compiles the SDK to dist/. pnpm release:sdk --version <v> builds it, then commits and tags
v<v> in a local clone of the public repository. It never pushes, and it prints the push commands.
It stops before a change in these conditions:
- The clone is missing, has changes, or is not on a branch.
- The tag exists, or the version is not greater than the current version.
- The package has no LICENSE file, or the SDK source has changes that are not committed.
A failed attempt restores the version and the clone.

pnpm check type checks the SDK and the example plugin. The example plugin now has JSDoc types from the SDK.
pnpm test covers the feature checks, the host and SDK parity, and the release script with temporary
Git repositories. The plugin development, release, development, and completion guides describe the SDK.
2026-10-28 02:33:16 -07:00

143 lines
6.5 KiB
Markdown

# RuneStone Plugin SDK
`@runestone/plugin-sdk` gives TypeScript types and small helpers for [RuneStone](https://git.knottywyvern.com/kwyvern/RuneStone) plugins.
RuneStone is a desktop workspace for Markdown notes, code, and project work. Plugins add views, commands, and document editors to it.
The SDK contains:
- The types of the plugin API: `PluginApi` and its members, the `plugin.json` manifest, commands, views, document types, menus, and contributions
- `API_VERSION`, the plugin API version that the SDK describes
- Feature checks for API members that older RuneStone versions do not have
- The RuneStone action icons and the tooltip helper, as DOM functions
The SDK does not run plugins and does not call RuneStone. RuneStone gives the `api` object to your plugin at activation.
## Install
Install the SDK from a release tag of its Git repository. The SDK is not on the npm registry.
```sh
pnpm add -D "git+https://git.knottywyvern.com/kwyvern/RuneStone_Plugin_SDK.git#v0.1.0"
```
The result in `package.json`:
```json
{
"devDependencies": {
"@runestone/plugin-sdk": "git+https://git.knottywyvern.com/kwyvern/RuneStone_Plugin_SDK.git#v0.1.0"
}
}
```
The repository is public, so you need no access token and no `.npmrc` file. npm and Yarn accept the same URL.
Always give a tag. Without a tag, the package manager uses the latest commit, and your build can change without notice.
The package contains compiled JavaScript and declaration files, so the installation runs no build step.
Use TypeScript 5.0 or later, with `moduleResolution` set to `bundler`, `node16`, or `nodenext`. Include the `DOM` library.
## Write a plugin
A plugin is one JavaScript module that exports `activate(api)`. This example adds a view and a command that opens it.
```ts
import type { PluginApi } from "@runestone/plugin-sdk";
export function activate(api: PluginApi) {
api.registerView({
id: "hello",
title: "Hello",
mount(container) {
const text = document.createElement("p");
text.textContent = "Hello from a RuneStone plugin.";
container.append(text);
return () => text.remove();
},
});
api.registerCommand({ id: "show-hello", title: "Show hello", icon: "puzzle", run: () => api.openView("hello") });
}
```
Compile the module with a bundler, so the entry file contains the SDK helpers that it uses. RuneStone loads the entry from a Blob URL, so imports from `node_modules` do not resolve at run time.
Then make the plugin ZIP. See [Build and package a plugin](https://git.knottywyvern.com/kwyvern/RuneStone/src/branch/master/docs/plugin-development.md#build-and-package-a-plugin).
A type-only import, such as `import type { PluginApi }`, adds no code to the bundle.
## Check for features
Members marked optional in `PluginApi` can be absent on older RuneStone versions. Examples are `api.pty`, `api.files`, and `api.workspace.readBytes`.
Make sure that a member exists before you use it.
```ts
import { hasFeature, requireFeature, requireFeatures, type PluginApi } from "@runestone/plugin-sdk";
export function activate(api: PluginApi) {
// Stop the activation with one message that names each missing feature.
requireFeatures(api, ["pty", "workspace.readOptional"]);
const pty = requireFeature(api, "pty");
// Hide a control when the feature is absent.
if (hasFeature(api, "ui.showActionMenu")) showMenuButton();
}
```
| Function | Result |
| --- | --- |
| `hasFeature(api, feature)` | `true` when RuneStone gives the member |
| `missingFeatures(api, features)` | The features of the list that RuneStone does not give |
| `requireFeature(api, feature)` | The member, with its type. A function stays bound to its object. |
| `requireFeatures(api, features)` | Nothing. Throws one error for all missing features. |
When a feature is missing, `requireFeature` and `requireFeatures` throw a `MissingFeatureError`. Its `features` property lists the feature names.
Its message names each feature and its API path, and tells the user to update RuneStone. For example:
`This plugin needs the terminal API (api.pty). Update RuneStone to a version that has this feature.`
`pluginFeatures` lists all the feature names. The checks examine only whether the member exists.
## Icons and tooltips
RuneStone has no API for icons or tooltips. The SDK gives the host code as DOM functions, so your controls look and behave like the RuneStone controls.
```ts
import { actionIcon } from "@runestone/plugin-sdk/icons";
import { attachTooltip } from "@runestone/plugin-sdk/tooltip";
const button = document.createElement("button");
button.setAttribute("aria-label", "Refresh");
button.append(actionIcon("refresh"));
const removeTooltip = attachTooltip(button, () => "Refresh");
```
| Import | Contents |
| --- | --- |
| `@runestone/plugin-sdk/icons` | `actionIcons`, the SVG markup of each icon. `actionIcon(name)` makes an SVG element with the `action-icon` class. |
| `@runestone/plugin-sdk/tooltip` | `attachTooltip(element, label)` shows a tooltip on hover and keyboard focus. It returns a cleanup function. |
RuneStone styles the `action-icon` and `rs-tooltip` classes. Call the cleanup function of each tooltip when your view closes.
For menus, call `api.ui.showActionMenu`. RuneStone draws the menu, so the SDK contains only its types: `ActionMenuItem` and `ActionMenuOptions`.
## Versions
The SDK has its own version number in the form `zz.yy.xx`, for example `0.1.0`. Each release has the tag `v<version>`.
A hotfix adds a lowercase letter to the version, for example `0.1.0a`.
`API_VERSION` is the `apiVersion` value of `plugin.json`. The SDK version and the RuneStone version are different numbers.
A new SDK release can add types for new RuneStone features. Use the feature checks for features that older RuneStone versions do not have.
## Documentation and help
- [Plugin development reference](https://git.knottywyvern.com/kwyvern/RuneStone/src/branch/master/docs/plugin-development.md): the package format, the API, and the build procedure
- [Completion API](https://git.knottywyvern.com/kwyvern/RuneStone/src/branch/master/docs/completion.md)
- [Install and manage plugins](https://git.knottywyvern.com/kwyvern/RuneStone/src/branch/master/docs/plugins.md)
- [Release policy](https://git.knottywyvern.com/kwyvern/RuneStone/src/branch/master/docs/releases.md#sdk-releases)
The SDK source is in the `packages/plugin-sdk` folder of the [RuneStone repository](https://git.knottywyvern.com/kwyvern/RuneStone). Report problems there.
This repository holds only the released files.
Knotty Wyvern maintains RuneStone and the SDK.
## License
Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).