Writing a Nootion plugin
A plugin adds editor blocks (/ command), database column types, API routes, scheduled tasks and
formula functions to Nootion. The administrator turns it on and off and fills in its settings in Administration › Plugins.
Plugins are installed on trust, as on any personal server: their server code runs with the same rights as Nootion. Only install plugins whose source you know. See Trust.
The Embeds plugin that ships with Nootion is a complete example: apps/server/plugins/embed.
#Installing a plugin
- Copy the plugin's folder to
/data/plugins/<id>/(Nootion's data volume). - Restart the container.
- Turn it on in Administration › Plugins. Turning it on or off, or changing a setting, needs no restart.
#Structure
my_plugin/
├── manifest.json required
├── server.js server code (ES module), optional
├── client.js browser code (ES module), optional
└── assets/ files served as they are (libraries, styles, images), optional#manifest.json
{
"id": "my_plugin",
"name": "My plugin",
"version": "1.0.0",
"description": "What the plugin does.",
"author": "Me",
"homepage": "https://example.org",
"server": "server.js",
"client": "client.js",
"blocks": [
{ "id": "map", "title": "My map", "description": "Text of the / menu", "icon": "🗺️", "keywords": "map plan", "defaultData": {} }
],
"propertyTypes": [
{ "id": "stars", "name": "Rating", "description": "One to five stars", "icon": "⭐" }
],
"settings": [
{ "key": "apiKey", "label": "API key", "type": "password", "help": "Encrypted on the server." },
{ "key": "mode", "label": "Mode", "type": "select", "default": "simple", "options": [{ "value": "simple", "label": "Simple" }] }
]
}id: lowercase letters, digits and_(2 to 40 characters). It appears in URLs and in page content.- Setting types:
text,password,number,boolean,select. Passwords are encrypted (withAPP_SECRET) and never sent back to the browser. - Blocks declared here stay known to the editor even when the plugin is off: pages keep them and show a
"plugin turned off" placeholder. So don't rename a block's
idfrom one version to the next.
#Translations
Every text of the manifest (name, description, block title, description and keywords, setting label,
help and option label) can be a plain string or one string per language:
{ "name": { "en": "My plugin", "fr": "Mon plugin" } }Each user sees the texts in their language, or in English when theirs is missing. Block keywords of every language
are searched in the / menu. In code, nootion.locale (browser) and nootion.locale() (server, during a request)
give the user's language ("en", "fr"…) to pick the plugin's own texts.
#Server side (server.js)
export function activate(nootion) {
// GET /api/plugins/my_plugin/hello/Ada: for signed-in users only
nootion.route("GET", "/hello/:name", ({ params, user, query, body }) => ({ message: `Hello ${params.name}, from ${user.name}` }));
// Reachable without signing in (public pages): { public: true }
nootion.route("GET", "/status", () => ({ ok: true }), { public: true });
// Error with an HTTP status, in the user's language
nootion.route("POST", "/refused", () => {
throw new nootion.HttpError(403, "refused", nootion.locale() === "fr" ? "Pas autorisé." : "Not allowed.");
});
// Scheduled task (at least 1 second), stopped when the plugin is turned off
nootion.every(15 * 60 * 1000, async () => nootion.log.info("syncing…"));
// Formula function, usable in Formula columns: withTax(prop("Price"))
nootion.formulaFunction("withTax", { min: 1, max: 1, help: "withTax(amount): amount × 1.2", run: ([x]) => x * 1.2 });
// Before showing a page to a user, check their access:
// nootion.pages.role(user.id, pageId) returns null, "viewer", "commenter", "editor" or "full".
// Return a function: it is called when the plugin stops.
return () => {};
}What activate receives:
| Field | Purpose |
|---|---|
id |
the plugin's id |
settings |
setting values (passwords decrypted). The plugin restarts when they change. |
dataDir |
the plugin's own folder: /data/plugin-data/<id> |
log.info / warn / error |
server log |
route(method, path, handler, { public }) |
route served under /api/plugins/<id>/… |
every(ms, task) |
repeated task |
formulaFunction(name, { min, max, help, run }) |
formula function computed on the server. run receives the already evaluated arguments (numbers, texts, booleans, lists, dates). |
pages.role(userId, pageId) |
a user's role on a page |
HttpError |
error with an HTTP status |
locale() |
language of the user being served ("en", "fr"…) |
userSecrets.get / set / delete(userId, value) |
data kept for one user (an account on another service, a token…), any JSON, stored encrypted with APP_SECRET and deleted with the user |
attachments.create({ userId, pageId, filename, mimeType, data }) |
attaches a file to a page as if the user had uploaded it: they must be able to edit the page, and MAX_UPLOAD_MB applies. data is a Buffer or a stream (e.g. a fetch response's body). Returns { id, filename, mimeType, size, url }. |
request(userId, method, path, body) |
calls Nootion's API (/databases/…, /pages/…, see API.md) as a user, with their rights, as if they had made the request. Resolves with the JSON answer, throws an HttpError on an error. |
connectedAccount({ describe, disconnect }) |
lists the user's account on another service in My account › Connected services: describe(userId) returns what to show ("ada@gmail.com") or null when not connected, disconnect(userId) forgets and revokes it. |
redirect(url) |
return it from a route to send the browser elsewhere (a sign-in page of another service…) |
appUrl |
Nootion's address (APP_URL), e.g. to build an OAuth redirect address |
net.request(url, options) |
an http(s) request to an address a user typed (a link to preview, a webhook): it refuses the server's own network and throws, unless allowPrivate: true. Options: method (GET, POST), headers, body, timeoutMs, maxRedirects (0 by default, each hop is checked), maxBytes (how much of the answer is read, 0 by default), stream (hands the answer over as it arrives, in stream, instead of reading it; timeoutMs then counts until the headers, and idleMs of silence ends it, 60 s by default). Resolves with { status, ok, url, headers, body, stream } (body is a Buffer). Use it instead of fetch for such addresses. |
net.isPrivateAddress(ip) |
whether an IP address is local (loopback, private ranges, link-local, IPv4 inside IPv6…) |
binary(data, contentType) |
return it from a route to send a file instead of JSON. PNG, JPEG, GIF, WebP and AVIF images are shown in the browser; anything else is downloaded. |
stream(data, contentType) |
return it from a route to send text as it is written (a model's answer): data is a stream or an async generator of strings, contentType is text/plain; charset=utf-8 by default. Each chunk reaches the browser at once; when the browser goes away, the stream is destroyed (a generator's finally runs). Read it in the browser with nootion.fetch. |
markdown(text) |
page blocks, as the editor keeps them, from Markdown (headings, lists, to-dos, quotes, code, tables, links): to put text written elsewhere in a page. |
A plugin must never send a user data they couldn't see themselves.
#Trust
What Nootion does and doesn't do for you:
- Server code is not sandboxed.
server.jsis ordinary Node.js code in Nootion's process: it can read the whole database and every file of/data, and reach the network. Installing a plugin is giving it the keys. - Browser code runs as the person looking at the page, in Nootion's own origin: it can do whatever they can do.
The Content-Security-Policy only lets scripts served by Nootion run, so ship the libraries you need in
assets/rather than loading them from another site (or the administrator has to setCONTENT_SECURITY_POLICY=off). - Routes are for signed-in users unless you pass
{ public: true }. They are not open to API tokens. Nootion doesn't check what a route returns: callpages.role(user.id, pageId)before showing anything of a page, and userequest(userId, …)to read or write as the user, which applies their rights for you. - Addresses typed by users go through
net.request, notfetch: otherwise anyone who can use the plugin can make the server read the local network (the router's admin page, a cloud's metadata address…). - Settings of type password and
userSecretsare encrypted withAPP_SECRETin the database, and never sent to the browser.
#Browser side (client.js)
The browser code is loaded by every page, public pages included. It receives the application's React:
don't bundle another one. No build tool needed, React.createElement is enough.
export function activate(nootion) {
const { React } = nootion;
const h = React.createElement;
const label = nootion.locale === "fr" ? "Choisir un lieu" : "Choose a place";
function Map({ data, setData, editable }) {
if (!data.place) {
return editable ? h("button", { onClick: () => setData({ place: "Lyon" }) }, label) : null;
}
return h("div", null, `📍 ${data.place}`);
}
nootion.registerBlock("map", Map);
}data: the block's data, stored in the page (and shared live with other editors). Keep there what readers need to see, so public pages render without calling the server.setData(object): replaces the block's data.editable: false for viewers and on public pages.pageId: the page the block is in (to create or find things next to it); null on public pages.nootion.api(path, { method, body }): calls one of the plugin's routes (/api/plugins/<id><path>).nootion.locale: the interface language. The page reloads when the user changes it.
The application's Tailwind classes aren't available to plugins: use inline styles.
An element with the nootion-hover-only class only shows when the block is hovered (handy for edit buttons).
If the component crashes, only the block shows an error; the rest of the page keeps working.
#Libraries, styles and images
Files in an assets/ folder of the plugin are served at /api/plugins/<id>/assets/<path> while the plugin is on, to
everyone (public pages included): JavaScript, CSS, JSON, images (PNG, JPEG, GIF, WebP) and WOFF2 fonts. Ship a
library there instead of loading it from a CDN:
const script = document.createElement("script");
script.src = `/api/plugins/${nootion.id}/assets/leaflet/leaflet.js`;
document.head.appendChild(script);The Map plugin loads Leaflet this way: apps/server/plugins/map.
#Database column types
A column type declared in propertyTypes is offered with the others when adding a column to a database. Its cells
keep a text (up to 20,000 characters): the plugin decides what it holds (a number, a code, JSON…) and how it
looks and is edited. Nootion filters, sorts, groups, searches and exports it as a text, so pick a text that reads
well in a CSV.
nootion.registerPropertyType("stars", {
// value: the text, or null when empty. setValue(text or null) saves it. editable: false for readers.
// variant: "table" (a compact, one-line cell) or "panel" (an opened row's properties, a card).
Cell: ({ value, setValue, editable, variant }) => h(Stars, { value, setValue, editable }),
});The component handles clicks and typing itself (stop the click's propagation if it opens something). When the plugin
is off, its columns stay and their cells show and edit the plain text. The Ratings plugin that ships with Nootion
is a small example: apps/server/plugins/rating.
#Other places to pick files from
A plugin can offer files from somewhere else (a cloud drive…). They are offered as a / command and as
"From …" under "Add a file" in "File" columns:
nootion.registerFileSource({
label: "My drive",
icon: "🗂️",
keywords: "drive cloud",
// Let the user choose, attach copies to the page (with attachments.create on the server), and resolve with
// what that returned: [{ id, filename, mimeType, size, url }]. Resolve with nothing when they cancel.
pick: ({ pageId, multiple }) => nootion.ui.showDialog((close) => h(MyPicker, { pageId, multiple, close })),
});#Entries in the menu of a selection
A plugin can add a menu to the bar shown over selected text (and, on phones, a button in the bar above the keyboard), for people who may edit the page. An entry receives the passage, and can show a panel in the page right under it, while the passage stays highlighted. The panel puts its result in the page, or goes away (Escape closes it too):
nootion.registerSelectionMenu({
label: "Shout",
icon: "📣",
// A line under the entries, read each time the menu opens (to say where the text goes…).
note: () => "Nothing leaves your server.",
// Hidden while it returns false.
visible: () => true,
actions: [
{
id: "upper",
label: "In capitals",
icon: "🔠",
run: ({ text, pageId, showPanel }) =>
showPanel((panel) =>
h("div", null, text.toUpperCase(), " ",
h("button", { onClick: () => panel.replace(text.toUpperCase()) }, "Replace"),
h("button", { onClick: () => panel.close() }, "Discard"))),
},
],
});The panel's replace(content) takes the passage's place, insertAfter(content) goes right after it (on the same
line when it is one paragraph), insertBelow(content) goes below its block; each closes the panel. content is plain
text (a paragraph per blank line) or editor blocks as JSON (for instance from nootion.markdown on the server).
panel.open turns false once it is closed, by the plugin, Escape, another panel or leaving the page.
#A button at the bottom right of pages
nootion.registerPagePanel({ label, icon, visible, render }) adds a round button at the bottom right of pages (one
per plugin), which opens a panel beside the page (over it on a phone); Escape or its ✕ closes it. render(ctx)
draws its content, with ctx.pageId, ctx.title() and ctx.text() (the page as plain text, as it is now),
ctx.canEdit, ctx.insert(content) (below the cursor, or at the end of the page; plain text or editor blocks) and
ctx.close().
To read a route that returns nootion.stream(…) as it arrives, nootion.fetch(path, { method, body, signal }) gives
the Response (it throws, like nootion.api, when the route answers with an error); pass an AbortController's
signal to stop it. The AI assistant plugin uses both: apps/server/plugins/assistant.
#The app's interface
nootion.ui gives a few pieces of Nootion's interface, so that a plugin looks like the rest of the app (light and
dark themes included):
ui.showDialog((close) => element): drawselementabove the page, outside its content, untilclose(value)is called; returns a promise of that value.ui.Modal: a dialog box,{ title, onClose, wide, children }.ui.Button: a button,{ variant: "primary" | "ghost" | "danger", ...button props }.
The Nextcloud plugin uses all of these: apps/server/plugins/nextcloud.