starlight-annotations 0.1.0 โ 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -11
- package/index.js +4 -1
- package/package.json +9 -4
- package/server.d.ts +2 -2
- package/src/client/annotations.js +29 -4
- package/src/client/styles.css +3 -0
- package/src/server/github.js +4 -1
package/README.md
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
# starlight-annotations
|
|
1
|
+
# ๐๏ธ starlight-annotations
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/starlight-annotations)
|
|
4
|
+
[](https://github.com/alessandroros/starlight-annotations/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/alessandroros/starlight-annotations/blob/main/LICENSE)
|
|
6
|
+
[](https://starlight.astro.build/resources/plugins/)
|
|
2
7
|
|
|
3
8
|
Let readers highlight text and leave notes on your [Starlight](https://starlight.astro.build) docs. Notes are JSON files committed to your Git repository, not stored by a third-party service.
|
|
4
9
|
|
|
5
|
-
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
- **Runs anywhere.** The API is one Web-standard handler for Cloudflare Workers, Netlify, Vercel, Deno or Node. Or keep personal notes in the browser, with no server.
|
|
9
|
-
- **No DOM rewriting.** Highlights use the CSS Custom Highlight API, so Mermaid, Expressive Code and other plugins are untouched.
|
|
10
|
+
**๐ [Documentation](https://alessandroros.github.io/starlight-annotations/) ยท โจ [Live demo](https://alessandroros.github.io/starlight-annotations/guides/demo/)**
|
|
11
|
+
|
|
12
|
+
## Features
|
|
10
13
|
|
|
11
|
-
**
|
|
14
|
+
- ๐ฌ **Select, note, discuss.** Markdown notes with a formatting toolbar, replies, "in reply to" references, resolve and reopen.
|
|
15
|
+
- ๐ **Anchored to the text.** W3C Web Annotation selectors: notes find their place again after edits, and long passages are stored by their start and end.
|
|
16
|
+
- ๐๏ธ **Your repo, your data.** One file per page on a branch of its own; every note is a commit by its author.
|
|
17
|
+
- ๐ **Runs anywhere.** The API is one Web-standard handler for Cloudflare Workers, Netlify, Vercel, Deno or Node. Or keep personal notes in the browser, with no server.
|
|
18
|
+
- ๐ชถ **Light and safe.** Highlights use the CSS Custom Highlight API, so the page markup is never rewritten. Notes cannot inject HTML, and the Markdown parser loads only when it is needed.
|
|
19
|
+
- ๐ **Multilingual.** English, Italian, German, French and Spanish built in; the UI follows the page language.
|
|
12
20
|
|
|
13
|
-
## Quick start
|
|
21
|
+
## ๐ Quick start
|
|
14
22
|
|
|
15
23
|
```sh
|
|
16
24
|
npm install starlight-annotations
|
|
@@ -34,7 +42,9 @@ export default defineConfig({
|
|
|
34
42
|
|
|
35
43
|
Run `npm run dev`, select text on any page and press **Annotate**. During development notes are written to `.annotations/` in your project.
|
|
36
44
|
|
|
37
|
-
|
|
45
|
+
## โ๏ธ Publish
|
|
46
|
+
|
|
47
|
+
Mount the handler next to your pages, for example on a Cloudflare Worker:
|
|
38
48
|
|
|
39
49
|
```js
|
|
40
50
|
import { createAnnotationsHandler, github } from 'starlight-annotations/server';
|
|
@@ -59,6 +69,6 @@ See [Store notes on GitHub](https://alessandroros.github.io/starlight-annotation
|
|
|
59
69
|
|
|
60
70
|
No server? `starlightAnnotations({ backend: 'browser' })` keeps notes in each reader's browser.
|
|
61
71
|
|
|
62
|
-
## License
|
|
72
|
+
## ๐ License
|
|
63
73
|
|
|
64
|
-
MIT
|
|
74
|
+
MIT ยฉ Alessandro Rosa
|
package/index.js
CHANGED
|
@@ -14,7 +14,10 @@ export default function starlightAnnotations(options = {}) {
|
|
|
14
14
|
return {
|
|
15
15
|
name: 'starlight-annotations',
|
|
16
16
|
hooks: {
|
|
17
|
-
'config:setup'({ addIntegration }) {
|
|
17
|
+
'config:setup'({ addIntegration, config: starlightConfig, updateConfig }) {
|
|
18
|
+
// Starlight links customCss on every page, in dev and in the build.
|
|
19
|
+
// (CSS imported by an injected script is bundled but not linked.)
|
|
20
|
+
updateConfig({ customCss: [...(starlightConfig.customCss ?? []), 'starlight-annotations/styles.css'] });
|
|
18
21
|
addIntegration(annotationsIntegration(config));
|
|
19
22
|
},
|
|
20
23
|
},
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starlight-annotations",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Let readers highlight text and leave notes on Starlight docs. Notes are
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "๐๏ธ Let readers highlight text and leave notes on Starlight docs. Notes are committed to your Git repository.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alessandro Rosa",
|
|
7
7
|
"type": "module",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "git+https://github.com/alessandroros/starlight-annotations.git",
|
|
11
11
|
"directory": "packages/starlight-annotations"
|
|
12
12
|
},
|
|
13
|
-
"homepage": "https://github.
|
|
13
|
+
"homepage": "https://alessandroros.github.io/starlight-annotations/",
|
|
14
14
|
"bugs": "https://github.com/alessandroros/starlight-annotations/issues",
|
|
15
15
|
"keywords": [
|
|
16
16
|
"withastro",
|
|
@@ -20,7 +20,11 @@
|
|
|
20
20
|
"annotations",
|
|
21
21
|
"comments",
|
|
22
22
|
"documentation",
|
|
23
|
-
"web-annotation"
|
|
23
|
+
"web-annotation",
|
|
24
|
+
"highlight",
|
|
25
|
+
"notes",
|
|
26
|
+
"feedback",
|
|
27
|
+
"docs-review"
|
|
24
28
|
],
|
|
25
29
|
"exports": {
|
|
26
30
|
".": {
|
|
@@ -36,6 +40,7 @@
|
|
|
36
40
|
"default": "./src/server/node.js"
|
|
37
41
|
},
|
|
38
42
|
"./client": "./src/client/annotations.js",
|
|
43
|
+
"./styles.css": "./src/client/styles.css",
|
|
39
44
|
"./package.json": "./package.json"
|
|
40
45
|
},
|
|
41
46
|
"files": [
|
package/server.d.ts
CHANGED
|
@@ -55,8 +55,8 @@ export interface GitHubAuthOptions {
|
|
|
55
55
|
/** A long random string that signs the session cookie. */
|
|
56
56
|
sessionSecret: string;
|
|
57
57
|
/**
|
|
58
|
-
* Who may write. `"collaborators"` (default)
|
|
59
|
-
* with `token`. `"anyone"` accepts every GitHub user.
|
|
58
|
+
* Who may write. `"collaborators"` (default) accepts people with write, maintain or admin
|
|
59
|
+
* access to `repo`, checked with `token`. `"anyone"` accepts every GitHub user.
|
|
60
60
|
*/
|
|
61
61
|
writers?: 'collaborators' | 'anyone' | string[] | ((profile: { login: string; id: number }) => boolean | Promise<boolean>);
|
|
62
62
|
repo?: string;
|
|
@@ -6,12 +6,9 @@
|
|
|
6
6
|
// Notes are written in Markdown (GitHub flavour) and stored as typed.
|
|
7
7
|
// micromark escapes raw HTML and drops javascript: links by default, so a
|
|
8
8
|
// note cannot inject markup into the page.
|
|
9
|
-
import { micromark } from 'micromark';
|
|
10
|
-
import { gfm, gfmHtml } from 'micromark-extension-gfm';
|
|
11
9
|
import config from 'virtual:starlight-annotations/config';
|
|
12
10
|
import { createBackend } from './backend.js';
|
|
13
11
|
import { strings } from './i18n.js';
|
|
14
|
-
import './styles.css';
|
|
15
12
|
|
|
16
13
|
const BASE = import.meta.env.BASE_URL.replace(/\/$/, '');
|
|
17
14
|
// Text the reader does not see as content: diagrams (rendered late), heading
|
|
@@ -26,8 +23,35 @@ const mod = mac ? 'โ' : 'Ctrl+';
|
|
|
26
23
|
|
|
27
24
|
// ---- Markdown ----
|
|
28
25
|
|
|
26
|
+
// The Markdown parser is most of the client's weight, so it loads on demand:
|
|
27
|
+
// when the page has notes, or when the reader starts to write one. Until it
|
|
28
|
+
// arrives a note shows as plain text, then it is filled in.
|
|
29
|
+
let markdown = null;
|
|
30
|
+
let markdownLoading = null;
|
|
31
|
+
const waitingForMarkdown = new Map(); // element -> source
|
|
32
|
+
|
|
33
|
+
function loadMarkdown() {
|
|
34
|
+
markdownLoading ??= Promise.all([import('micromark'), import('micromark-extension-gfm')]).then(([{ micromark }, { gfm, gfmHtml }]) => {
|
|
35
|
+
markdown = (source) => micromark(source, { extensions: [gfm()], htmlExtensions: [gfmHtml()] });
|
|
36
|
+
for (const [target, source] of waitingForMarkdown) if (target.isConnected) renderMarkdown(target, source);
|
|
37
|
+
waitingForMarkdown.clear();
|
|
38
|
+
}).catch(() => {
|
|
39
|
+
// Offline or blocked: notes stay as plain text, and the next note retries.
|
|
40
|
+
markdownLoading = null;
|
|
41
|
+
});
|
|
42
|
+
return markdownLoading;
|
|
43
|
+
}
|
|
44
|
+
|
|
29
45
|
function renderMarkdown(target, source) {
|
|
30
|
-
|
|
46
|
+
if (!markdown) {
|
|
47
|
+
target.textContent = source;
|
|
48
|
+
target.classList.add('annot-md-pending');
|
|
49
|
+
waitingForMarkdown.set(target, source);
|
|
50
|
+
loadMarkdown();
|
|
51
|
+
return target;
|
|
52
|
+
}
|
|
53
|
+
target.classList.remove('annot-md-pending');
|
|
54
|
+
target.innerHTML = markdown(source);
|
|
31
55
|
for (const link of target.querySelectorAll('a[href]')) {
|
|
32
56
|
// micromark empties unsafe URLs (javascript: and the like): show plain text.
|
|
33
57
|
if (!link.getAttribute('href')) {
|
|
@@ -654,6 +678,7 @@ async function start(root) {
|
|
|
654
678
|
|
|
655
679
|
function compose() {
|
|
656
680
|
if (!selected) return;
|
|
681
|
+
loadMarkdown();
|
|
657
682
|
state.pending = selected.selector;
|
|
658
683
|
selectButton.hidden = true;
|
|
659
684
|
window.getSelection()?.removeAllRanges();
|
package/src/client/styles.css
CHANGED
package/src/server/github.js
CHANGED
|
@@ -107,8 +107,11 @@ export function githubAuth({ clientId, clientSecret, sessionSecret, repo, token,
|
|
|
107
107
|
if (writers === 'anyone') return true;
|
|
108
108
|
if (Array.isArray(writers)) return writers.map((login) => login.toLowerCase()).includes(profile.login.toLowerCase());
|
|
109
109
|
if (typeof writers === 'function') return Boolean(await writers(profile));
|
|
110
|
+
// Only people who can push to the repository. `permission` is the legacy
|
|
111
|
+
// level: maintain counts as write, triage as read. On a public repository
|
|
112
|
+
// GitHub answers "read" for anyone, so read access is not enough.
|
|
110
113
|
const permission = await api(token, `/repos/${repo}/collaborators/${encodeURIComponent(profile.login)}/permission`, { allow404: true });
|
|
111
|
-
return
|
|
114
|
+
return permission?.permission === 'admin' || permission?.permission === 'write';
|
|
112
115
|
}
|
|
113
116
|
|
|
114
117
|
async function hmac(value) {
|