@nanocollective/roster 0.1.0-alpha.9 → 0.1.0-beta.1
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 +5 -0
- package/dist/cli.js +4067 -1950
- package/dist/worker/assets/assets/css/base.css +312 -0
- package/dist/worker/assets/assets/css/brain.css +116 -0
- package/dist/worker/assets/assets/css/diff.css +33 -0
- package/dist/worker/assets/assets/css/graph.css +39 -0
- package/dist/worker/assets/assets/css/health.css +75 -0
- package/dist/worker/assets/assets/css/home.css +101 -0
- package/dist/worker/assets/assets/css/inbox.css +199 -0
- package/dist/worker/assets/assets/css/layout.css +175 -0
- package/dist/worker/assets/assets/css/markdown.css +96 -0
- package/dist/worker/assets/assets/css/runs.css +13 -0
- package/dist/worker/assets/assets/css/search.css +48 -0
- package/dist/worker/assets/assets/css/setup.css +194 -0
- package/dist/worker/assets/assets/js/api.js +161 -0
- package/dist/worker/assets/assets/js/app.js +465 -0
- package/dist/worker/assets/assets/js/dialog.js +196 -0
- package/dist/worker/assets/assets/js/dom.js +131 -0
- package/dist/worker/assets/assets/js/homesort.js +174 -0
- package/dist/worker/assets/assets/js/icons.js +142 -0
- package/dist/worker/assets/assets/js/inflight.js +18 -0
- package/dist/worker/assets/assets/js/lightbox.js +273 -0
- package/dist/worker/assets/assets/js/md.js +406 -0
- package/dist/worker/assets/assets/js/mdedit.js +84 -0
- package/dist/worker/assets/assets/js/mention.js +264 -0
- package/dist/worker/assets/assets/js/readiness.js +70 -0
- package/dist/worker/assets/assets/js/refresh.js +197 -0
- package/dist/worker/assets/assets/js/router.js +20 -0
- package/dist/worker/assets/assets/js/search.js +366 -0
- package/dist/worker/assets/assets/js/state.js +213 -0
- package/dist/worker/assets/assets/js/textdiff.js +96 -0
- package/dist/worker/assets/assets/js/views/app.js +145 -0
- package/dist/worker/assets/assets/js/views/brain.js +260 -0
- package/dist/worker/assets/assets/js/views/changed.js +157 -0
- package/dist/worker/assets/assets/js/views/checklist.js +115 -0
- package/dist/worker/assets/assets/js/views/credential.js +84 -0
- package/dist/worker/assets/assets/js/views/docs.js +174 -0
- package/dist/worker/assets/assets/js/views/files.js +139 -0
- package/dist/worker/assets/assets/js/views/graph.js +436 -0
- package/dist/worker/assets/assets/js/views/health.js +304 -0
- package/dist/worker/assets/assets/js/views/hire.js +593 -0
- package/dist/worker/assets/assets/js/views/home.js +585 -0
- package/dist/worker/assets/assets/js/views/inbox.js +1558 -0
- package/dist/worker/assets/assets/js/views/memory.js +150 -0
- package/dist/worker/assets/assets/js/views/org.js +195 -0
- package/dist/worker/assets/assets/js/views/orgedit.js +240 -0
- package/dist/worker/assets/assets/js/views/paste.js +216 -0
- package/dist/worker/assets/assets/js/views/prompt.js +457 -0
- package/dist/worker/assets/assets/js/views/repos.js +97 -0
- package/dist/worker/assets/assets/js/views/runonce.js +94 -0
- package/dist/worker/assets/assets/js/views/runs.js +182 -0
- package/dist/worker/assets/assets/js/views/setup.js +557 -0
- package/dist/worker/assets/assets/js/views/staff.js +291 -0
- package/dist/worker/assets/assets/js/views/todo.js +62 -0
- package/dist/worker/assets/assets/js/yaml.js +134 -0
- package/dist/worker/assets/docs-images/brain.jpg +0 -0
- package/dist/worker/assets/docs-images/home.jpg +0 -0
- package/dist/worker/assets/docs-images/org.jpg +0 -0
- package/dist/worker/assets/docs-images/prompt.jpg +0 -0
- package/dist/worker/assets/docs-images/setup-org.jpg +0 -0
- package/dist/worker/assets/docs-images/setup-plan.jpg +0 -0
- package/dist/worker/assets/docs-images/staff.jpg +0 -0
- package/dist/worker/assets/index.html +82 -0
- package/dist/worker/index.js +4430 -0
- package/docs/README.md +2 -2
- package/docs/architecture.md +1 -1
- package/docs/charters/analyst.md +65 -0
- package/docs/charters/community.md +63 -0
- package/docs/charters/designer.md +65 -0
- package/docs/charters/devops.md +65 -0
- package/docs/charters/pm.md +70 -0
- package/docs/charters/qa.md +65 -0
- package/docs/charters/writer.md +63 -0
- package/docs/commands.md +9 -3
- package/docs/concepts.md +33 -20
- package/docs/cost.md +3 -3
- package/docs/developing.md +27 -9
- package/docs/doctor-codes.md +3 -1
- package/docs/export.md +2 -0
- package/docs/getting-started.md +44 -27
- package/docs/hosting.md +89 -43
- package/docs/images/brain.jpg +0 -0
- package/docs/images/home.jpg +0 -0
- package/docs/images/org.jpg +0 -0
- package/docs/images/prompt.jpg +0 -0
- package/docs/images/staff.jpg +0 -0
- package/docs/manual-steps.md +3 -2
- package/docs/org-yaml.md +3 -0
- package/docs/portal.md +143 -45
- package/docs/prompts.md +6 -0
- package/docs/security.md +57 -11
- package/docs/session-workflow.md +14 -0
- package/docs/staff-yaml.md +17 -1
- package/docs/upgrading.md +3 -3
- package/docs/writing-a-charter.md +19 -6
- package/package.json +4 -3
- package/templates/brain/.github/workflows/%%STAFF%%-daily.yaml +20 -1
- package/templates/brain/.github/workflows/%%STAFF%%-mention.yaml +53 -13
- package/templates/brain/staff.yaml +4 -1
- package/templates/briefs/amend.md +4 -3
- package/templates/briefs/priorities.md +46 -0
- package/templates/ops/.github/workflows/session.yaml +137 -3
- package/templates/ops/compose.mjs +35 -8
- package/templates/ops/org/operating.md +29 -10
- package/templates/ops/prompts/daily.md +23 -4
- package/templates/ops/prompts/mention.md +19 -0
- package/templates/ops/run-record.mjs +5 -3
- package/templates/portal/css/base.css +34 -0
- package/templates/portal/css/health.css +14 -1
- package/templates/portal/css/home.css +101 -0
- package/templates/portal/css/inbox.css +21 -4
- package/templates/portal/css/layout.css +29 -3
- package/templates/portal/css/markdown.css +19 -0
- package/templates/portal/css/search.css +48 -0
- package/templates/portal/css/setup.css +79 -4
- package/templates/portal/index.html +15 -9
- package/templates/portal/js/api.js +30 -4
- package/templates/portal/js/app.js +90 -7
- package/templates/portal/js/dialog.js +36 -0
- package/templates/portal/js/homesort.js +174 -0
- package/templates/portal/js/icons.js +41 -0
- package/templates/portal/js/inflight.js +18 -0
- package/templates/portal/js/md.js +5 -2
- package/templates/portal/js/mdedit.js +84 -0
- package/templates/portal/js/readiness.js +70 -0
- package/templates/portal/js/refresh.js +10 -2
- package/templates/portal/js/search.js +366 -0
- package/templates/portal/js/state.js +11 -5
- package/templates/portal/js/views/app.js +1 -2
- package/templates/portal/js/views/brain.js +1 -1
- package/templates/portal/js/views/checklist.js +19 -4
- package/templates/portal/js/views/credential.js +42 -51
- package/templates/portal/js/views/health.js +11 -6
- package/templates/portal/js/views/hire.js +593 -0
- package/templates/portal/js/views/home.js +585 -0
- package/templates/portal/js/views/inbox.js +237 -68
- package/templates/portal/js/views/org.js +31 -111
- package/templates/portal/js/views/orgedit.js +240 -0
- package/templates/portal/js/views/paste.js +111 -66
- package/templates/portal/js/views/prompt.js +94 -40
- package/templates/portal/js/views/repos.js +10 -10
- package/templates/portal/js/views/runs.js +21 -4
- package/templates/portal/js/views/setup.js +130 -163
- package/templates/portal/js/views/staff.js +181 -301
- package/templates/portal/js/views/todo.js +62 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/* Looking properly at a picture.
|
|
2
|
+
*
|
|
3
|
+
* An image inside a document is laid out for the page it is in, which for a screenshot of a
|
|
4
|
+
* user interface means unreadable: the thing you wanted to see is the small print. Every other
|
|
5
|
+
* surface here has an answer for "show me that bigger" and this one did not.
|
|
6
|
+
*
|
|
7
|
+
* So any image the portal renders as *content* opens over the page, fitted to the window, and
|
|
8
|
+
* zooms. Gallery tiles are deliberately not included: they already have a click, which opens
|
|
9
|
+
* the file, and the file's own view is one of the images this does catch.
|
|
10
|
+
*
|
|
11
|
+
* Deliberately not a `<dialog>`. The rest of the portal uses one, and it is right for a form,
|
|
12
|
+
* but this needs its own key handling, wheel handling and pointer capture, and `showModal`
|
|
13
|
+
* brings a focus trap and a backdrop that then have to be worked around rather than used.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { el } from "./dom.js";
|
|
17
|
+
import { icon } from "./icons.js";
|
|
18
|
+
|
|
19
|
+
/** Images that open. `.md img` is every rendered document; `zoomable` is opt-in elsewhere. */
|
|
20
|
+
const SELECTOR = ".md img, img.zoomable";
|
|
21
|
+
|
|
22
|
+
const MIN_SCALE = 0.1;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* How far in it will go, and why it is not just a number.
|
|
26
|
+
*
|
|
27
|
+
* A scaled `<img>` is a composited layer, and the compositor allocates it at the *rendered*
|
|
28
|
+
* size. A flat ceiling of 16 turned a 1544x784 screenshot into a 310-megapixel layer, about
|
|
29
|
+
* 1.2GB of texture, and froze the tab for the best part of a minute. That is not a zoom level
|
|
30
|
+
* anybody asked for; it is a number that was never checked against a real picture.
|
|
31
|
+
*
|
|
32
|
+
* So the ceiling comes off the image. The budget is what may be rendered at once; the floor of
|
|
33
|
+
* 1 keeps actual size reachable however large the source is, and the cap of 8 stops a 100px
|
|
34
|
+
* icon being allowed to fill a wall.
|
|
35
|
+
*/
|
|
36
|
+
const MAX_RENDERED_PIXELS = 16e6;
|
|
37
|
+
const ABSOLUTE_MAX = 8;
|
|
38
|
+
|
|
39
|
+
export function maxScaleFor(natural) {
|
|
40
|
+
const px = (natural.width || 1) * (natural.height || 1);
|
|
41
|
+
return Math.min(ABSOLUTE_MAX, Math.max(1, Math.sqrt(MAX_RENDERED_PIXELS / px)));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let open = null;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Zoom about a point, as arithmetic.
|
|
48
|
+
*
|
|
49
|
+
* The pixel under the cursor has to stay under the cursor. Without that the image drifts away
|
|
50
|
+
* from whatever you were trying to look at, which is the difference between a zoom and a
|
|
51
|
+
* nuisance, and it is the one part of this that is wrong in a way no screenshot shows.
|
|
52
|
+
*
|
|
53
|
+
* Pure, and separate from the element, so it can be checked rather than eyeballed.
|
|
54
|
+
*
|
|
55
|
+
* @param at {scale, tx, ty}, the current transform
|
|
56
|
+
* @param px,py the fixed point, in stage coordinates
|
|
57
|
+
* @param max the ceiling, from `maxScaleFor`
|
|
58
|
+
* @returns the new transform, or `at` unchanged when it is already against a stop
|
|
59
|
+
*/
|
|
60
|
+
export function zoomAbout(at, factor, px, py, max = ABSOLUTE_MAX) {
|
|
61
|
+
const scale = Math.min(max, Math.max(MIN_SCALE, at.scale * factor));
|
|
62
|
+
if (scale === at.scale) return at;
|
|
63
|
+
const k = scale / at.scale;
|
|
64
|
+
return { scale, tx: px - (px - at.tx) * k, ty: py - (py - at.ty) * k };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The fitted transform: the whole thing visible, centred, and never enlarged to get there.
|
|
69
|
+
*
|
|
70
|
+
* Blowing a 200px image up to fill a 1400px window is not "fitting" it, it is making it worse,
|
|
71
|
+
* so the scale is capped at 1 and a small picture sits small in the middle.
|
|
72
|
+
*/
|
|
73
|
+
export function fitInside(natural, stage) {
|
|
74
|
+
const [w, h] = [natural.width || 1, natural.height || 1];
|
|
75
|
+
const [bw, bh] = [stage.width || 1, stage.height || 1];
|
|
76
|
+
const scale = Math.min(bw / w, bh / h, 1);
|
|
77
|
+
return { scale, tx: (bw - w * scale) / 2, ty: (bh - h * scale) / 2 };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* One delegated listener, installed at boot.
|
|
82
|
+
*
|
|
83
|
+
* Per-image handlers would have to be attached by every render site, and there are four of
|
|
84
|
+
* them: markdown in a doc page, markdown in a brain document, an image surface, and the file
|
|
85
|
+
* viewer. A render that forgot would be an image that silently does not open, which is exactly
|
|
86
|
+
* the sort of thing nobody notices for a month.
|
|
87
|
+
*/
|
|
88
|
+
export function installLightbox() {
|
|
89
|
+
document.addEventListener?.("click", (e) => {
|
|
90
|
+
const img = e.target?.closest?.(SELECTOR);
|
|
91
|
+
if (!img || !img.currentSrc && !img.getAttribute?.("src")) return;
|
|
92
|
+
// A picture inside a link is the link's to handle. Following it is what was asked for.
|
|
93
|
+
if (img.closest("a")) return;
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
show(img.currentSrc || img.getAttribute("src"), img.getAttribute("alt") || "");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/* Navigating away closes it. The overlay is appended to the body rather than into the view,
|
|
99
|
+
so without this it outlives the page it was opened from: a doc image left open over the
|
|
100
|
+
Inbox, with no obvious relationship to anything on screen. */
|
|
101
|
+
addEventListener?.("hashchange", close);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Open one. Exported so a caller with a src and no element can use it too. */
|
|
105
|
+
export function show(src, alt = "") {
|
|
106
|
+
close();
|
|
107
|
+
|
|
108
|
+
const img = el("img", { src, alt, draggable: false });
|
|
109
|
+
const stage = el("div", { className: "lbstage" }, [img]);
|
|
110
|
+
|
|
111
|
+
const caption = alt ? el("figcaption", { className: "lbcap", textContent: alt }) : null;
|
|
112
|
+
const shut = el("button", {
|
|
113
|
+
className: "lbx",
|
|
114
|
+
title: "Close (Esc)",
|
|
115
|
+
"aria-label": "Close",
|
|
116
|
+
}, [icon("close", "ic")]);
|
|
117
|
+
const hint = el("span", {
|
|
118
|
+
className: "lbhint",
|
|
119
|
+
textContent: "scroll to zoom · drag to move · click to fit",
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
/* Zoom has always worked from the keyboard and the wheel. Neither is discoverable, and on a
|
|
123
|
+
mouse without a usable wheel the keyboard was the only way in at all.
|
|
124
|
+
The percentage is a button rather than a readout: "how big is this" and "put it back" are
|
|
125
|
+
the same question, and it is the one control here that says what the state is. */
|
|
126
|
+
const out = el("button", {
|
|
127
|
+
className: "lbctl",
|
|
128
|
+
title: "Zoom out (-)",
|
|
129
|
+
"aria-label": "Zoom out",
|
|
130
|
+
}, [icon("dash", "ic")]);
|
|
131
|
+
const level = el("button", { className: "lbctl lblevel", title: "Fit to the window (0)" });
|
|
132
|
+
const into = el("button", {
|
|
133
|
+
className: "lbctl",
|
|
134
|
+
title: "Zoom in (+)",
|
|
135
|
+
"aria-label": "Zoom in",
|
|
136
|
+
}, [icon("plus", "ic")]);
|
|
137
|
+
const zoombar = el("div", { className: "lbzoom" }, [out, level, into]);
|
|
138
|
+
|
|
139
|
+
const box = el("div", {
|
|
140
|
+
className: "lightbox",
|
|
141
|
+
role: "dialog",
|
|
142
|
+
"aria-modal": "true",
|
|
143
|
+
"aria-label": alt || "image",
|
|
144
|
+
}, [stage, shut, hint, zoombar, ...(caption ? [caption] : [])]);
|
|
145
|
+
|
|
146
|
+
/* Scale and offset are kept here rather than read back off the transform, because reading a
|
|
147
|
+
matrix back and re-deriving them accumulates error over a few dozen wheel events. */
|
|
148
|
+
let scale = 1;
|
|
149
|
+
let fit = 1;
|
|
150
|
+
let ceiling = ABSOLUTE_MAX;
|
|
151
|
+
let tx = 0;
|
|
152
|
+
let ty = 0;
|
|
153
|
+
|
|
154
|
+
const draw = () => {
|
|
155
|
+
img.style.transform = `translate(${tx}px, ${ty}px) scale(${scale})`;
|
|
156
|
+
box.dataset.zoomed = String(scale > fit * 1.001);
|
|
157
|
+
/* Percentage of actual size, not of fitted: 100% has to mean one image pixel per screen
|
|
158
|
+
pixel, or the number is about the window rather than about the picture. */
|
|
159
|
+
level.textContent = `${Math.round(scale * 100)}%`;
|
|
160
|
+
// Disabled at the stops, because a button that does nothing reads as a broken one.
|
|
161
|
+
out.disabled = scale <= MIN_SCALE + 1e-9;
|
|
162
|
+
into.disabled = scale >= ceiling - 1e-9;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const reset = () => {
|
|
166
|
+
const natural = {
|
|
167
|
+
width: img.naturalWidth || img.width,
|
|
168
|
+
height: img.naturalHeight || img.height,
|
|
169
|
+
};
|
|
170
|
+
const at = fitInside(natural, stage.getBoundingClientRect?.() ?? { width: 0, height: 0 });
|
|
171
|
+
fit = at.scale;
|
|
172
|
+
ceiling = maxScaleFor(natural);
|
|
173
|
+
({ scale, tx, ty } = at);
|
|
174
|
+
draw();
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const zoomAt = (factor, px, py) => {
|
|
178
|
+
({ scale, tx, ty } = zoomAbout({ scale, tx, ty }, factor, px, py, ceiling));
|
|
179
|
+
draw();
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const centre = () => {
|
|
183
|
+
const r = stage.getBoundingClientRect?.() ?? { width: 0, height: 0 };
|
|
184
|
+
return [r.width / 2, r.height / 2];
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
img.addEventListener("load", reset);
|
|
188
|
+
if (img.complete) reset();
|
|
189
|
+
|
|
190
|
+
box.addEventListener(
|
|
191
|
+
"wheel",
|
|
192
|
+
(e) => {
|
|
193
|
+
e.preventDefault();
|
|
194
|
+
const r = stage.getBoundingClientRect();
|
|
195
|
+
// ctrl+wheel is what a trackpad pinch arrives as, and it comes in much larger steps.
|
|
196
|
+
const step = e.ctrlKey ? 0.01 : 0.0015;
|
|
197
|
+
zoomAt(Math.exp(-e.deltaY * step), e.clientX - r.left, e.clientY - r.top);
|
|
198
|
+
},
|
|
199
|
+
{ passive: false },
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
/* Drag to pan, and the same gesture decides what a release means: a press that moved is a
|
|
203
|
+
pan, a press that did not is a click. Without the distance test, panning an image always
|
|
204
|
+
ended by toggling the zoom you had just set. */
|
|
205
|
+
let from = null;
|
|
206
|
+
let moved = false;
|
|
207
|
+
img.addEventListener("pointerdown", (e) => {
|
|
208
|
+
from = { x: e.clientX, y: e.clientY, tx, ty };
|
|
209
|
+
moved = false;
|
|
210
|
+
img.setPointerCapture?.(e.pointerId);
|
|
211
|
+
});
|
|
212
|
+
img.addEventListener("pointermove", (e) => {
|
|
213
|
+
if (!from) return;
|
|
214
|
+
const dx = e.clientX - from.x;
|
|
215
|
+
const dy = e.clientY - from.y;
|
|
216
|
+
if (Math.abs(dx) + Math.abs(dy) > 3) moved = true;
|
|
217
|
+
tx = from.tx + dx;
|
|
218
|
+
ty = from.ty + dy;
|
|
219
|
+
draw();
|
|
220
|
+
});
|
|
221
|
+
img.addEventListener("pointerup", (e) => {
|
|
222
|
+
const wasDrag = moved;
|
|
223
|
+
from = null;
|
|
224
|
+
img.releasePointerCapture?.(e.pointerId);
|
|
225
|
+
if (wasDrag) return;
|
|
226
|
+
// Fitted: go to actual size under the cursor. Zoomed at all: back to fitted.
|
|
227
|
+
const r = stage.getBoundingClientRect();
|
|
228
|
+
if (scale > fit * 1.001) reset();
|
|
229
|
+
else zoomAt(1 / fit, e.clientX - r.left, e.clientY - r.top);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// The buttons zoom about the middle of the stage, which is the only fixed point they have.
|
|
233
|
+
out.onclick = () => zoomAt(0.8, ...centre());
|
|
234
|
+
into.onclick = () => zoomAt(1.25, ...centre());
|
|
235
|
+
level.onclick = reset;
|
|
236
|
+
|
|
237
|
+
// The backdrop is the way out, and the image is not the backdrop.
|
|
238
|
+
box.addEventListener("pointerdown", (e) => {
|
|
239
|
+
if (e.target === box || e.target === stage) close();
|
|
240
|
+
});
|
|
241
|
+
shut.onclick = close;
|
|
242
|
+
|
|
243
|
+
const keys = (e) => {
|
|
244
|
+
if (e.key === "Escape") return close();
|
|
245
|
+
if (e.key === "0") return reset();
|
|
246
|
+
if (e.key === "+" || e.key === "=") return zoomAt(1.25, ...centre());
|
|
247
|
+
if (e.key === "-" || e.key === "_") return zoomAt(0.8, ...centre());
|
|
248
|
+
};
|
|
249
|
+
addEventListener("keydown", keys);
|
|
250
|
+
|
|
251
|
+
const onResize = () => reset();
|
|
252
|
+
addEventListener("resize", onResize);
|
|
253
|
+
|
|
254
|
+
const returnTo = document.activeElement;
|
|
255
|
+
open = { box, keys, onResize, returnTo };
|
|
256
|
+
|
|
257
|
+
document.body.append(box);
|
|
258
|
+
// The page behind must not scroll under an overlay: the scrollbar is still live otherwise,
|
|
259
|
+
// and a wheel event that misses the image scrolls the document instead of zooming.
|
|
260
|
+
document.body.dataset.lightbox = "1";
|
|
261
|
+
shut.focus?.();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function close() {
|
|
265
|
+
if (!open) return;
|
|
266
|
+
const { box, keys, onResize, returnTo } = open;
|
|
267
|
+
open = null;
|
|
268
|
+
removeEventListener("keydown", keys);
|
|
269
|
+
removeEventListener("resize", onResize);
|
|
270
|
+
box.remove?.();
|
|
271
|
+
delete document.body.dataset.lightbox;
|
|
272
|
+
returnTo?.focus?.();
|
|
273
|
+
}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/* Enough markdown for an issue thread and for a file in a brain, escaped first. Not a
|
|
2
|
+
* general parser: tables, headings, nested lists, code, quotes, rules, images and links,
|
|
3
|
+
* which is what these actually contain. Tables matter most — the status issues are mostly
|
|
4
|
+
* tables, and without them the body is a wall of pipes.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { esc } from "./dom.js";
|
|
8
|
+
import { iconHTML } from "./icons.js";
|
|
9
|
+
import { yamlHTMLFromEscaped } from "./yaml.js";
|
|
10
|
+
|
|
11
|
+
/* Who this org is, so `@cto` can be told from `@some-stranger`. Registered once at boot
|
|
12
|
+
rather than threaded through every call site, because every call site would pass the
|
|
13
|
+
same value. */
|
|
14
|
+
let PEOPLE = new Map();
|
|
15
|
+
|
|
16
|
+
/** @param people Map of lowercased handle → { href, known } */
|
|
17
|
+
export function setPeople(people) {
|
|
18
|
+
PEOPLE = people;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/* Renders the small subset of markdown that appears in a fact line. Deliberately not a
|
|
22
|
+
markdown parser: escaping first, then a handful of inline forms. */
|
|
23
|
+
export function inline(s) {
|
|
24
|
+
return esc(s)
|
|
25
|
+
.replace(/`([^`]+)`/g, '<code style="font:12px var(--mono);color:var(--accent)">$1</code>')
|
|
26
|
+
.replace(/\[\[([a-z0-9-]+)\]\]/g, '<span class="slug" data-goto="$1">[[$1]]</span>')
|
|
27
|
+
.replace(/\*\*([^*]+)\*\*/g, "<b>$1</b>")
|
|
28
|
+
.replace(
|
|
29
|
+
/\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g,
|
|
30
|
+
'<a href="$2" target="_blank" rel="noopener">$1</a>',
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const LIST_RE = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/;
|
|
35
|
+
|
|
36
|
+
/* ---------------------------- HTML tables ----------------------------------
|
|
37
|
+
* Some comments are raw HTML rather than markdown. The Cloudflare Pages bot posts its deploy
|
|
38
|
+
* status as a `<table>`, and GitHub renders it; this escaped it and showed you the tags.
|
|
39
|
+
*
|
|
40
|
+
* Nothing here relaxes the escaping, which is the one thing standing between a comment on a
|
|
41
|
+
* public repo and this page. The table is taken apart, each cell is reduced to the handful of
|
|
42
|
+
* markdown forms a cell actually uses, and the result goes back through the same escape-first
|
|
43
|
+
* pipeline as everything else. No attacker-controlled markup reaches innerHTML.
|
|
44
|
+
*/
|
|
45
|
+
const TABLE_RE = /<table[^>]*>[\s\S]*?<\/table>/gi;
|
|
46
|
+
const ROW_RE = /<tr[^>]*>([\s\S]*?)<\/tr>/gi;
|
|
47
|
+
const CELL_RE = /<(t[hd])[^>]*>([\s\S]*?)<\/\1>/gi;
|
|
48
|
+
|
|
49
|
+
const ENTITIES = {
|
|
50
|
+
" ": " ", "&": "&", "<": "<", ">": ">", """: '"',
|
|
51
|
+
"'": "'", "'": "'", "—": "—", "–": "–",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The handful of inline HTML forms a comment actually uses, as the markdown they stand in for.
|
|
56
|
+
* Everything else is dropped to its text, which is what GitHub does with it too.
|
|
57
|
+
*
|
|
58
|
+
* `<img>` is resolved before `<a>` on purpose: a linked icon is `<a><img alt="x"></a>`, and
|
|
59
|
+
* taking the anchor first leaves an empty label and a bare URL on the page.
|
|
60
|
+
*/
|
|
61
|
+
function htmlToMarkdown(html, breakAs, stripUnknown) {
|
|
62
|
+
return html
|
|
63
|
+
.replace(/<br\s*\/?>/gi, breakAs)
|
|
64
|
+
.replace(/<(strong|b)[^>]*>([\s\S]*?)<\/\1>/gi, (_m, _t, x) => "**" + x.trim() + "**")
|
|
65
|
+
.replace(/<(em|i)[^>]*>([\s\S]*?)<\/\1>/gi, (_m, _t, x) => "*" + x.trim() + "*")
|
|
66
|
+
.replace(/<code[^>]*>([\s\S]*?)<\/code>/gi, (_m, x) => "`" + x.trim() + "`")
|
|
67
|
+
.replace(/<img[^>]*alt=["']([^"']*)["'][^>]*>/gi, "$1")
|
|
68
|
+
.replace(/<img[^>]*>/gi, "")
|
|
69
|
+
// Only http(s). A `javascript:` href would be escaped downstream anyway, but a link that
|
|
70
|
+
// cannot go anywhere useful is better dropped than rendered.
|
|
71
|
+
.replace(
|
|
72
|
+
/<a[^>]*href=["'](https?:\/\/[^"']+)["'][^>]*>([\s\S]*?)<\/a>/gi,
|
|
73
|
+
(_m, href, text) => {
|
|
74
|
+
const label = text.replace(/<[^>]+>/g, "").trim();
|
|
75
|
+
return label ? "[" + label + "](" + href + ")" : href;
|
|
76
|
+
},
|
|
77
|
+
)
|
|
78
|
+
/* Only inside a cell. Out in the prose, a tag nobody asked about stays escaped and
|
|
79
|
+
visible: a brain document that mentions `<script>` or <div> without backticks should
|
|
80
|
+
still say so, and silently deleting a word is worse than printing an angle bracket. */
|
|
81
|
+
.replace(stripUnknown ? /<[^>]+>/g : /(?!)/g, "")
|
|
82
|
+
.replace(/&[a-z#0-9]+;/gi, (e) => ENTITIES[e.toLowerCase()] ?? e);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** One `<td>`, on one line. */
|
|
86
|
+
function cellToMarkdown(html) {
|
|
87
|
+
return htmlToMarkdown(html, " ", true).replace(/\s+/g, " ").trim();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Pull every `<table>` out of the source, leaving a marker line where each one was.
|
|
92
|
+
*
|
|
93
|
+
* The markers are substituted back inside the main loop, so the table renders in place and
|
|
94
|
+
* everything around it is still ordinary markdown. A literal `%%HTMLTABLE0%%` typed by a
|
|
95
|
+
* person only collides if the same comment also contains a real HTML table.
|
|
96
|
+
*/
|
|
97
|
+
/* A fence or a code span is content, not markup. Splitting on them first is what keeps a
|
|
98
|
+
comment discussing `<meta name="robots">` from having its example quietly deleted. */
|
|
99
|
+
const CODE_RE = /(```[\s\S]*?```|`[^`\n]*`)/;
|
|
100
|
+
|
|
101
|
+
function liftTables(src) {
|
|
102
|
+
const tables = [];
|
|
103
|
+
const text = String(src ?? "")
|
|
104
|
+
.split(CODE_RE)
|
|
105
|
+
.map((part, i) => (i % 2 ? part : htmlToMarkdown(liftTablesIn(part, tables), "\n", false)))
|
|
106
|
+
.join("");
|
|
107
|
+
return { text, tables };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function liftTablesIn(src, tables) {
|
|
111
|
+
return src.replace(TABLE_RE, (block) => {
|
|
112
|
+
const rows = [];
|
|
113
|
+
let head = null;
|
|
114
|
+
for (const [, inner] of block.matchAll(ROW_RE)) {
|
|
115
|
+
const cells = [];
|
|
116
|
+
let isHead = true;
|
|
117
|
+
for (const [, tag, body] of inner.matchAll(CELL_RE)) {
|
|
118
|
+
if (tag.toLowerCase() !== "th") isHead = false;
|
|
119
|
+
cells.push(cellToMarkdown(body));
|
|
120
|
+
}
|
|
121
|
+
if (!cells.length) continue;
|
|
122
|
+
if (isHead && !rows.length && !head) head = cells;
|
|
123
|
+
else rows.push(cells);
|
|
124
|
+
}
|
|
125
|
+
if (!head && !rows.length) return block;
|
|
126
|
+
tables.push({ head, rows });
|
|
127
|
+
return "\n\n%%HTMLTABLE" + (tables.length - 1) + "%%\n\n";
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const MARKER_RE = /^%%HTMLTABLE(\d+)%%$/;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* opts.repo resolves a bare #123 to that repository
|
|
135
|
+
* opts.file { dir, staffDir } resolves relative images and links inside a brain
|
|
136
|
+
* opts.docs links between doc pages stay inside the portal
|
|
137
|
+
*/
|
|
138
|
+
export function mdlite(src, opts = {}) {
|
|
139
|
+
const lifted = liftTables(String(src ?? ""));
|
|
140
|
+
const lines = esc(lifted.text).replace(/\r/g, "").split("\n");
|
|
141
|
+
const out = [];
|
|
142
|
+
let i = 0;
|
|
143
|
+
|
|
144
|
+
const inlineFmt = (s) =>
|
|
145
|
+
chips(
|
|
146
|
+
s
|
|
147
|
+
.replace(/`([^`]+)`/g, "<code>$1</code>")
|
|
148
|
+
.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (_m, alt, src2) =>
|
|
149
|
+
// `` is how a recording gets written into a note, and GitHub plays it.
|
|
150
|
+
// An <img> pointing at a video is a broken-image icon, so it becomes a player.
|
|
151
|
+
/\.(mp4|m4v|webm|mov|ogv)(\?|#|$)/i.test(src2)
|
|
152
|
+
? '<video src="' + mdAsset(src2, opts) + '" controls preload="metadata" class="player">' +
|
|
153
|
+
"</video>"
|
|
154
|
+
: '<img src="' + mdAsset(src2, opts) + '" alt="' + alt + '" loading="lazy">',
|
|
155
|
+
)
|
|
156
|
+
// Non-greedy and allowing an inner asterisk, because "**bold with *this* inside**" is
|
|
157
|
+
// ordinary in these files and the old pattern silently left the stars on the page.
|
|
158
|
+
// Requiring a non-space first character keeps a literal "/** wildcard" from opening one.
|
|
159
|
+
.replace(/\*\*(\S[\s\S]*?)\*\*/g, "<b>$1</b>")
|
|
160
|
+
.replace(/(^|[\s(])\*([^*\s][^*]*)\*/g, "$1<i>$2</i>")
|
|
161
|
+
.replace(/~~([^~]+)~~/g, "<s>$1</s>")
|
|
162
|
+
.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_m, text, href) => mdLink(href, text, opts))
|
|
163
|
+
.replace(/(^|[\s(])(https?:\/\/[^\s<)]+)/g, (_m, pre, url) => pre + mdLink(url, url, opts)),
|
|
164
|
+
opts,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const cells = (row) => {
|
|
168
|
+
let s = row.trim();
|
|
169
|
+
if (s.startsWith("|")) s = s.slice(1);
|
|
170
|
+
if (s.endsWith("|")) s = s.slice(0, -1);
|
|
171
|
+
return s.split("|").map((c) => inlineFmt(c.trim()));
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
while (i < lines.length) {
|
|
175
|
+
const line = lines[i];
|
|
176
|
+
|
|
177
|
+
const lifted_at = MARKER_RE.exec(line.trim());
|
|
178
|
+
if (lifted_at && lifted.tables[Number(lifted_at[1])]) {
|
|
179
|
+
const t = lifted.tables[Number(lifted_at[1])];
|
|
180
|
+
const th = (t.head ?? []).map((h) => "<th>" + inlineFmt(esc(h)) + "</th>").join("");
|
|
181
|
+
const tr = t.rows
|
|
182
|
+
.map((r) => "<tr>" + r.map((c) => "<td>" + inlineFmt(esc(c)) + "</td>").join("") + "</tr>")
|
|
183
|
+
.join("");
|
|
184
|
+
out.push(
|
|
185
|
+
'<div class="ctable"><table>' +
|
|
186
|
+
(t.head ? "<thead><tr>" + th + "</tr></thead>" : "") +
|
|
187
|
+
"<tbody>" + tr + "</tbody></table></div>",
|
|
188
|
+
);
|
|
189
|
+
i++;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (line.trim().startsWith("```")) {
|
|
194
|
+
// The fence's language was thrown away, which is most of why a YAML example in the docs
|
|
195
|
+
// read as a grey block while the same file in the Org screen is coloured.
|
|
196
|
+
const lang = line.trim().slice(3).trim().toLowerCase();
|
|
197
|
+
const buf = [];
|
|
198
|
+
i++;
|
|
199
|
+
while (i < lines.length && !lines[i].trim().startsWith("```")) buf.push(lines[i++]);
|
|
200
|
+
i++;
|
|
201
|
+
const body = buf.join("\n");
|
|
202
|
+
out.push(
|
|
203
|
+
"<pre" + (lang ? ' class="lang-' + esc(lang) + '"' : "") + "><code>" +
|
|
204
|
+
(lang === "yaml" || lang === "yml" ? yamlHTMLFromEscaped(body) : body) +
|
|
205
|
+
"</code></pre>",
|
|
206
|
+
);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// A table is a pipe row followed by a --- separator row.
|
|
211
|
+
if (
|
|
212
|
+
line.trim().startsWith("|") &&
|
|
213
|
+
(lines[i + 1] ?? "").replace(/[^|:\-\s]/g, "") === (lines[i + 1] ?? "").trim() &&
|
|
214
|
+
/^\|?[\s:|-]+\|[\s:|-]*$/.test((lines[i + 1] ?? "").trim())
|
|
215
|
+
) {
|
|
216
|
+
const head = cells(line);
|
|
217
|
+
i += 2;
|
|
218
|
+
const body = [];
|
|
219
|
+
while (i < lines.length && lines[i].trim().startsWith("|")) body.push(cells(lines[i++]));
|
|
220
|
+
const th = head.map((h) => "<th>" + h + "</th>").join("");
|
|
221
|
+
const tr = body.map((r) => "<tr>" + r.map((c) => "<td>" + c + "</td>").join("") + "</tr>").join("");
|
|
222
|
+
// A leading empty header cell is how these issues start a two-column "thing | ask"
|
|
223
|
+
// table; keeping the row would just print a blank strip.
|
|
224
|
+
const showHead = head.some((h) => h.trim());
|
|
225
|
+
out.push(
|
|
226
|
+
'<div class="ctable"><table>' +
|
|
227
|
+
(showHead ? "<thead><tr>" + th + "</tr></thead>" : "") +
|
|
228
|
+
"<tbody>" + tr + "</tbody></table></div>",
|
|
229
|
+
);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (!line.trim()) { i++; continue; }
|
|
234
|
+
|
|
235
|
+
const h = line.match(/^(#{1,6})\s+(.*)$/);
|
|
236
|
+
if (h) { out.push("<h" + h[1].length + ">" + inlineFmt(h[2]) + "</h" + h[1].length + ">"); i++; continue; }
|
|
237
|
+
|
|
238
|
+
if (/^\s*([-*_])\s*\1\s*\1[\s\-*_]*$/.test(line)) { out.push("<hr>"); i++; continue; }
|
|
239
|
+
|
|
240
|
+
if (line.trim().startsWith(">")) {
|
|
241
|
+
const buf = [];
|
|
242
|
+
while (i < lines.length && lines[i].trim().startsWith(">")) {
|
|
243
|
+
buf.push(lines[i++].trim().replace(/^>\s?/, ""));
|
|
244
|
+
}
|
|
245
|
+
out.push("<blockquote>" + inlineFmt(buf.join(" ")) + "</blockquote>");
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Lists are gathered whole and nested by indent. Flat divs with a left margin looked
|
|
250
|
+
// right until a sub-point wrapped, and then the wrap did not line up with the bullet.
|
|
251
|
+
if (LIST_RE.test(line)) {
|
|
252
|
+
const items = [];
|
|
253
|
+
while (i < lines.length && LIST_RE.test(lines[i])) {
|
|
254
|
+
const m = LIST_RE.exec(lines[i++]);
|
|
255
|
+
items.push({
|
|
256
|
+
depth: Math.floor(m[1].replace(/\t/g, " ").length / 2),
|
|
257
|
+
ordered: /\d/.test(m[2]),
|
|
258
|
+
text: m[3],
|
|
259
|
+
});
|
|
260
|
+
// A continuation line indented under the bullet is part of that bullet.
|
|
261
|
+
while (i < lines.length && lines[i].trim() && /^\s{2,}\S/.test(lines[i]) && !LIST_RE.test(lines[i])) {
|
|
262
|
+
items[items.length - 1].text += " " + lines[i++].trim();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
out.push(mdList(items, 0, items[0].depth, inlineFmt).html);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/* Consecutive plain lines are one paragraph; markdown hard-wraps and we should not.
|
|
270
|
+
The join happens before the formatting, not after: these files wrap at 95 columns, so
|
|
271
|
+
a **bold run** or a [link](…) routinely straddles two source lines, and formatting
|
|
272
|
+
line by line left the asterisks on the page. */
|
|
273
|
+
const para = [];
|
|
274
|
+
while (
|
|
275
|
+
i < lines.length && lines[i].trim() &&
|
|
276
|
+
!LIST_RE.test(lines[i]) && !/^\s*(#{1,6}\s|\||>|```)/.test(lines[i])
|
|
277
|
+
) {
|
|
278
|
+
para.push(lines[i++].trim());
|
|
279
|
+
}
|
|
280
|
+
out.push("<p>" + inlineFmt(para.length ? para.join(" ") : lines[i++].trim()) + "</p>");
|
|
281
|
+
}
|
|
282
|
+
return out.join("\n");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** One level of a list, recursing wherever the indent goes deeper. */
|
|
286
|
+
function mdList(items, from, depth, fmt) {
|
|
287
|
+
let html = "";
|
|
288
|
+
let tasks = 0;
|
|
289
|
+
let i = from;
|
|
290
|
+
const ordered = items[from].ordered;
|
|
291
|
+
while (i < items.length && items[i].depth >= depth) {
|
|
292
|
+
if (items[i].depth > depth) {
|
|
293
|
+
const sub = mdList(items, i, items[i].depth, fmt);
|
|
294
|
+
// Nest inside the bullet we just closed, so the sub-list belongs to it.
|
|
295
|
+
html = html.replace(/<\/li>$/, sub.html + "</li>");
|
|
296
|
+
i = sub.next;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const task = /^\[( |x|X)\]\s+(.*)$/.exec(items[i].text);
|
|
300
|
+
if (task) {
|
|
301
|
+
tasks++;
|
|
302
|
+
html += '<li><span class="box">' + iconHTML(task[1] === " " ? "square" : "task-done") +
|
|
303
|
+
"</span>" + fmt(task[2]) + "</li>";
|
|
304
|
+
} else {
|
|
305
|
+
html += "<li>" + fmt(items[i].text) + "</li>";
|
|
306
|
+
}
|
|
307
|
+
i++;
|
|
308
|
+
}
|
|
309
|
+
const tag = ordered ? "ol" : "ul";
|
|
310
|
+
return { html: "<" + tag + (tasks ? ' class="tasks"' : "") + ">" + html + "</" + tag + ">", next: i };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const GH_REF = /^https?:\/\/github\.com\/([\w.-]+)\/([\w.-]+)\/(issues|pull)\/(\d+)/;
|
|
314
|
+
|
|
315
|
+
/** A link, or a chip if it points at an issue or a PR and the text adds nothing. */
|
|
316
|
+
function mdLink(href, text, opts) {
|
|
317
|
+
const gh = GH_REF.exec(href);
|
|
318
|
+
if (gh && (text === href || text === gh[1] + "/" + gh[2] + "#" + gh[4])) {
|
|
319
|
+
const repo = gh[1] + "/" + gh[2];
|
|
320
|
+
const label = (repo === opts.repo ? "" : repo) + "#" + gh[4];
|
|
321
|
+
return '<a class="ref' + (gh[3] === "pull" ? " pr" : "") + '" href="' + href +
|
|
322
|
+
'" target="_blank" rel="noopener" title="' + repo + " #" + gh[4] + '">' + label + "</a>";
|
|
323
|
+
}
|
|
324
|
+
if (/^https?:\/\//.test(href)) {
|
|
325
|
+
return '<a href="' + href + '" target="_blank" rel="noopener">' + text + "</a>";
|
|
326
|
+
}
|
|
327
|
+
if (opts.docs) {
|
|
328
|
+
// A link between doc pages stays inside the portal.
|
|
329
|
+
if (/^[\w.-]*\.md(#.*)?$/.test(href) || href.startsWith("#")) {
|
|
330
|
+
return '<a href="#" data-doc="' + href + '">' + text + "</a>";
|
|
331
|
+
}
|
|
332
|
+
return text;
|
|
333
|
+
}
|
|
334
|
+
if (href.startsWith("#")) return text;
|
|
335
|
+
// A relative link inside a brain opens the file here rather than going nowhere.
|
|
336
|
+
const path = mdPath(href, opts);
|
|
337
|
+
if (!path) return text;
|
|
338
|
+
return '<a href="#" data-file="' + path + '">' + text + "</a>";
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Resolve a relative path against the file being read, so ../assets/x.png works. */
|
|
342
|
+
function mdPath(href, opts) {
|
|
343
|
+
if (!opts.file || /^(https?:|data:|mailto:)/.test(href)) return null;
|
|
344
|
+
const parts = (opts.file.dir + href.split("#")[0].split("?")[0]).split("/");
|
|
345
|
+
const stack = [];
|
|
346
|
+
for (const p of parts) {
|
|
347
|
+
if (!p || p === ".") continue;
|
|
348
|
+
if (p === "..") stack.pop();
|
|
349
|
+
else stack.push(p);
|
|
350
|
+
}
|
|
351
|
+
return stack.join("/");
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function mdAsset(src, opts) {
|
|
355
|
+
if (/^(https?:|data:)/.test(src)) return src;
|
|
356
|
+
/* A screenshot in the docs is not in anybody's brain repo, so it cannot go through
|
|
357
|
+
/api/file, which resolves inside the workspace. It comes off the framework's own docs
|
|
358
|
+
directory instead, through a route that serves nothing else. */
|
|
359
|
+
if (opts.docs) return "/api/docasset?path=" + encodeURIComponent(src.replace(/^\.?\//, ""));
|
|
360
|
+
const path = mdPath(src, opts);
|
|
361
|
+
return path ? "/api/file?path=" + encodeURIComponent(opts.file.staffDir + "/" + path) : src;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/* #123, repo#123 and owner/repo#123. The short repo form is how the staff write about the other
|
|
365
|
+
repos in their own org (pip-web#172), and it was left as plain text. */
|
|
366
|
+
const REF_RE = /(^|[\s(>])((?:[\w.-]+\/)?[\w.-]+)?#(\d+)\b/g;
|
|
367
|
+
// GitHub's own username shape, plus the short aliases the agents answer to.
|
|
368
|
+
const AT_RE = /(^|[\s(>])@([a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38})\b/gi;
|
|
369
|
+
|
|
370
|
+
/* #123, owner/repo#123 and @handle become chips.
|
|
371
|
+
Split on tags and skip anything already inside an anchor, because a markdown link whose
|
|
372
|
+
text is "#113" would otherwise end up as an anchor nested in an anchor. */
|
|
373
|
+
export function chips(html, opts = {}) {
|
|
374
|
+
const parts = html.split(/(<[^>]*>)/);
|
|
375
|
+
let inA = 0;
|
|
376
|
+
return parts
|
|
377
|
+
.map((p) => {
|
|
378
|
+
if (p.startsWith("<")) {
|
|
379
|
+
if (/^<a\b/i.test(p)) inA++;
|
|
380
|
+
else if (/^<\/a>/i.test(p)) inA = Math.max(0, inA - 1);
|
|
381
|
+
return p;
|
|
382
|
+
}
|
|
383
|
+
if (inA) return p;
|
|
384
|
+
return p
|
|
385
|
+
.replace(REF_RE, (m, pre, repo, n) => {
|
|
386
|
+
const owner = String(opts.repo ?? "").split("/")[0];
|
|
387
|
+
const target = !repo ? opts.repo : repo.includes("/") ? repo : owner ? owner + "/" + repo : "";
|
|
388
|
+
if (!target) return m;
|
|
389
|
+
// /issues/N redirects to /pull/N when it is a PR, so one form is always right.
|
|
390
|
+
return pre + '<a class="ref" href="https://github.com/' + target + "/issues/" + n +
|
|
391
|
+
'" target="_blank" rel="noopener" title="' + target + " #" + n + '">' +
|
|
392
|
+
(repo ? repo : "") + "#" + n + "</a>";
|
|
393
|
+
})
|
|
394
|
+
.replace(AT_RE, (m, pre, name) => {
|
|
395
|
+
const who = PEOPLE.get(name.toLowerCase());
|
|
396
|
+
if (!who) {
|
|
397
|
+
return pre + '<a class="at" href="https://github.com/' + name +
|
|
398
|
+
'" target="_blank" rel="noopener">@' + name + "</a>";
|
|
399
|
+
}
|
|
400
|
+
// A mention of someone on this roster is marked, and goes to their repo.
|
|
401
|
+
return pre + '<a class="at you" href="' + who.href + '" target="_blank" rel="noopener"' +
|
|
402
|
+
' title="' + esc(who.title) + '">@' + name + "</a>";
|
|
403
|
+
});
|
|
404
|
+
})
|
|
405
|
+
.join("");
|
|
406
|
+
}
|