Notifications
The plugin sends no notifications by itself: a reply is a commit on the notes branch, and GitHub does not notify anyone about pushes. Give the handler an onChange listener and it is called after every saved change.
import { createAnnotationsHandler, github, slackWebhook } from 'starlight-annotations/server';
const annotations = createAnnotationsHandler({ ...github(options), onChange: slackWebhook(env.SLACK_WEBHOOK_URL),});A failing listener is logged and never fails the save. onChange also takes a list, to post to several places.
Do not make the reader wait
Section titled āDo not make the reader waitāOn Cloudflare Workers, Vercel and Netlify, pass the platform context to the handler. The note is saved, the response goes back at once, and the notification is sent after it:
export default { async fetch(request, env, ctx) { const response = await annotations(request, ctx); return response ?? env.ASSETS.fetch(request); },};Without it, the handler waits for the listeners before answering. That is right on Node, where the process keeps running anyway.
- At api.slack.com/apps, Create New App ā From scratch, in your workspace.
- Incoming Webhooks: turn them on, then Add New Webhook to Workspace and pick the channel.
- Copy the webhook URL and store it as a secret, for example
npx wrangler secret put SLACK_WEBHOOK_URL.
onChange: slackWebhook(env.SLACK_WEBHOOK_URL),Each change becomes one message: a linked title (āGrace replied on rfc/0002ā), the quoted passage and the message.
Microsoft Teams
Section titled āMicrosoft Teamsā- In the channel, open Workflows and create a flow from the template that posts to a channel when a webhook request is received.
- Pick the team and channel, and copy the URL the flow gives you. Store it as a secret.
onChange: teamsWebhook(env.TEAMS_WEBHOOK_URL),Each change becomes an Adaptive Card with an Open the note button.
Only some changes
Section titled āOnly some changesāBoth senders take the actions to post. By default they post everything except edit and anchor. To hear about new notes and replies but not resolve, reopen and delete:
onChange: slackWebhook(env.SLACK_WEBHOOK_URL, { actions: ['create', 'reply'] }),Email, or anything else
Section titled āEmail, or anything elseāonChange is any function. describeChange(event) turns an event into plain text (title, quote, body, and all of them with the link as text). For example with Resend:
import { describeChange } from 'starlight-annotations/server';
async function email(event) { const { title, text } = describeChange(event); await fetch('https://api.resend.com/emails', { method: 'POST', headers: { Authorization: `Bearer ${env.RESEND_API_KEY}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ from: 'docs@example.com', to: ['reviewers@example.com'], subject: title, text }), });}Email the author of a note
Section titled āEmail the author of a noteāTo tell the person who opened a note that someone answered it or resolved it, turn on emails in the auth:
github({ ...options, emails: true,});- At sign-in the reader is asked for their email address too. With a GitHub OAuth app the plugin asks for the
user:emailscope by itself. With a GitHub App, give it the account permission Email addresses: Read-only (Permissions & events ā Account permissions). GitLab, Gitea and Forgejo already return it. - Every new note and reply stores its writerās email in the notes file, encrypted (AES-GCM, with a key derived from the session secret). The repository and its history only ever see an unreadable
contactfield, and the API never sends it to the browser. - When someone else answers or resolves a note, the event has
threadAuthor: { login, name, email }, the person who opened it. It isnullwhen they act on their own note.
import { describeChange } from 'starlight-annotations/server';
async function emailTheAuthor(event) { if (!['reply', 'resolve'].includes(event.action) || !event.threadAuthor?.email) return; const { title, text } = describeChange(event); await fetch('https://api.resend.com/emails', { method: 'POST', headers: { Authorization: `Bearer ${env.RESEND_API_KEY}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ from: 'docs@example.com', to: [event.threadAuthor.email], subject: title, text }), });}Good to know:
- Changing the session secret makes the stored emails unreadable: those notes simply stop sending emails (
email: null); new notes work again. - Notes written before
emailswas turned on carry no email. - Tell your readers. Their email is stored, encrypted, so that they can be notified. Say so where they sign in, for example in your docsā privacy note.
Reaching people
Section titled āReaching peopleāThe plugin knows readers by their login and name, not their email. The event lists the people concerned by login, so a map you keep yourself can reach them:
const people = { grace: 'grace@example.com', 'alan-t': 'alan@example.com' };
async function notifyPeople(event) { const to = [...event.participants, ...event.mentions].map((login) => people[login.toLowerCase()]).filter(Boolean); if (to.length) await sendEmail(to, describeChange(event));}participants: everyone else who wrote in the thread.mentions: logins written as@loginin a new note or reply.
The person who made the change is never in either list.
The event
Section titled āThe eventā| Field | |
|---|---|
action |
create, reply, resolve, reopen, edit (the author changed a message), anchor (a thread moved to another passage), issue (a thread became a GitHub issue, in annotation.issue) or delete |
page |
The page key, e.g. rfc/0002-user-data-model |
url |
Opens the note on its page. After a whole thread is deleted, the page alone |
user |
{ login, name } of who made the change |
annotation |
The note created, answered, resolved, reopened, edited, moved or deleted |
thread |
The first note of the thread, with the quoted passage in thread.target |
inReplyTo |
For a reply, the message it answers |
threadAuthor |
{ login, name, email } of who opened the thread, when it is someone else; email needs emails: true |
participants, mentions |
Logins, see above |
url is built from the page the reader saved from. To build it yourself, pass pageUrl: (page) => `https://docs.example.com/${page}/` to the handler.