Documents in a git repository
Premium add-on. This page covers a premium add-on, included in Scriviamo Pro and Pro + Collaboration. See pricing
The premium git add-on keeps documents in a GitHub repository. People open a document from the
repository, and each version they save becomes a commit on the document's own branch, in a pull
request: the first version opens it, later versions update it, and merging it publishes the
document to the repository's default branch.
import {Git} from "./dist/addons/scriviamo.git.js"
import "./dist/addons/scriviamo.versions.js" // for saving versions and the History tab
Git.endpoint = "/scriviamo-git" // your git server; the reference server's address by default
The Repository tool opens a document (Open from Repository…), starts one at a path (New Document in Repository…), shows the open document's pull request, and signs out. Saving a version, with the versions add-on's Save Version, adds it to the pull request.
How the repository is laid out
Many documents share one repository. Each is a file at a path, which is its document ID:
docs/
contracts/
acme.scriviamo.json the document, as JSON
acme.md a readable copy, for reading and reviewing on GitHub
handbook/
welcome.scriviamo.json
welcome.md
The picker lists the .scriviamo.json files by folder, with those in review marked. A document
with an open pull request opens from its branch, scriviamo/<path>, so people carry on where the
last version left off; others open from the default branch.
The readable copy is the document as Markdown, saved beside it in the same commit, so a pull
request's diff can be read. Turn it off with Git.readableCopies = false, or on the server.
Versions
While a document from the repository is open, the versions add-on keeps its versions there. The History tab lists the commits that changed the document, newest first, with a link to each, and a line linking the open pull request. Versions can't be deleted, as they're the repository's history. Compare any version with the current document, or with another version.
Each commit's message is the version's name, with trailers recording who saved it and whether it was automatic:
Payment terms
Scriviamo-Version: named
Scriviamo-Author: Ada Lovelace
Signing in
People sign in with GitHub, through a GitHub App, so they reach only the repositories the app is installed on and that they can reach themselves, and commits and pull requests are theirs. Their GitHub tokens stay on the git server; the browser holds only a session cookie, so a script on the page can't take a token that writes to the repository. A token that runs out is refreshed by the server.
The git server
The add-on talks to a git server, over HTTP, with the browser's cookies:
| Request | Answer |
|---|---|
GET /session | {signedIn, user, signInURL} |
GET /sign-in?return=<path> | To GitHub to sign in, then back to the path |
POST /sign-out | {signedIn: false} |
GET /documents | {documents: [{path, name, folder, pullRequest}]} |
GET /document?path= | {path, ref, content, pullRequest} |
GET /versions?path= | {versions: [{id, name, createdAt, author, automatic, url}], pullRequest} |
GET /version?path=&id= | {content} |
POST /versions {path, name, automatic, content, readable} | {version, pullRequest} |
A pull request is {number, url, state, title}. Errors are {error, message} with an HTTP
status; signed-out means signing in again.
On Scriviamo Cloud
Scriviamo Cloud keeps an account's documents in its repository itself, so there's no git server to
run. While an editor is connected to Cloud with the collaboration add-on, and the account has a
repository set, the add-on works over that connection, whatever Git.endpoint says:
- Documents open as shared documents, whose ID is their path, so everyone editing one sees the others' changes.
- Versions are Cloud's, shared with everyone in the document, and Cloud copies each one to the pull request: saved ones as the person who saved them, and its automatic ones as someone who changed the document. The History tab links each version's commit.
- Sign In with GitHub opens GitHub in a window, and the picker carries on once it closes.
Set Git.overCollaboration = false to use Git.endpoint even then.
The reference server
src/addons/git/server/git-server.mjs is a dependency-free Node.js server that speaks this
protocol. Mount it in your own server:
import http from "node:http"
import {createGitHandler} from "./src/addons/git/server/git-server.mjs"
let git = createGitHandler({
repository: "acme/docs", // owner/name
folder: "docs", // documents live under this folder; "" for anywhere
readableCopies: true,
clientID: process.env.GITHUB_APP_CLIENT_ID,
clientSecret: process.env.GITHUB_APP_CLIENT_SECRET,
secret: process.env.SCRIVIAMO_GIT_SECRET, // 32 or more random characters, for cookies
sessions: myStore // {get, set, delete}; in memory by default
})
http.createServer(async (request, response) => {
if (await git(request, response)) return
// … the rest of your site
}).listen(8080)
or run it alone with node src/addons/git/server/git-server.mjs --port 8771 --repository owner/name.
To set up the GitHub App:
- Create a GitHub App, with Contents and Pull requests set to read and write, and its
callback URL set to your server's
/scriviamo-git/callback. Turn on expiring user tokens. - Install it on the repository.
- Give the server its client ID and a client secret.
The server keeps sessions in memory by default, so a restart signs everyone out; give it a store
of your own in production. Requests that change the repository from another site are refused
unless listed in allowedOrigins.
For development, node tools/git-demo-server.mjs serves the demo with the reference server and a
stand-in for GitHub holding a few documents, all in memory.
| Member | Meaning |
|---|---|
Git.endpoint | The git server's address |
Git.readableCopies | Whether a Markdown copy is saved beside each document |
Git.open(editor, path), Git.create(editor, path) | Open or start a document |
Git.choose(editor) | Shows the picker |
Git.pathFor(editor), Git.pullRequestFor(editor) | The open document's path and pull request |
Git.setConnection(editor, connection) | A GitConnection for one editor, such as to another server |
Git.overCollaboration | Whether git goes over a collaboration connection to a server that keeps a repository; on by default |
An editor raises repositoryChanged, with {path, pullRequest}, when its pull request changes.