cornfield 0.57.2 → 0.58.0

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/core/config.js CHANGED
@@ -46,6 +46,7 @@ import os from 'node:os';
46
46
  import path from 'node:path';
47
47
  import { pathToFileURL, fileURLToPath } from 'node:url';
48
48
  import { repoReader } from './repo.js';
49
+ import { linkProblems } from './schema.js';
49
50
  import { themeProblems } from './theme.js';
50
51
  import { brandProblems } from './brand.js';
51
52
  import { stringsProblems } from './strings.js';
@@ -415,10 +416,7 @@ export function configProblems(c) {
415
416
  } else if (!inRepo(c.proposals)) out.push('"proposals" is the proposals folder inside the repo, written relative to it (or { dir, approveLands }).');
416
417
  }
417
418
  if (c.links !== undefined) {
418
- if (!Array.isArray(c.links)) out.push('"links" is a list of { label, href }.');
419
- else c.links.forEach((l, i) => {
420
- if (!isObject(l) || !isText(l.label) || !/^(https?:\/\/|\/)/.test(String(l.href || ''))) out.push(`links[${i}] is { label, href } with an http(s) or same-origin href.`);
421
- });
419
+ for (const p of linkProblems(c.links)) out.push(p.message);
422
420
  }
423
421
  out.push(...themeProblems(c.theme));
424
422
  out.push(...brandProblems(c.brand));
package/core/proposal.js CHANGED
@@ -27,6 +27,7 @@
27
27
  * change?: [{ from, to, kind?, flow?, set: { …fields }, why }],
28
28
  * remove?: [{ from, to, kind?, flow?, why }] },
29
29
  * mockups?: [{ node, path, title }],
30
+ * links?: [{ label, href }],
30
31
  * scenarios?: [{ id, title, steps: [{ node } | { edge: { from, to, kind?, flow? } }, + text, + ms?,
31
32
  * + actor?, at?, check?, expect?], stepMs?, linkNodes?, actor?, down? }] }
32
33
  *
@@ -36,6 +37,10 @@
36
37
  * backslash, no hidden segment) and ends in one of MOCKUP_EXTENSIONS. The explorer opens a mockup from its node;
37
38
  * approving the proposal covers the files (server/mockups.js). HTML and SVG are untrusted content from a branch:
38
39
  * the server and the explorer never let them run script (docs/proposals.md).
40
+ * - Links: `links` lists pages that belong to this one proposal (a walkthrough, a design doc, a preview), at most
41
+ * MAX_LINKS. Each is a `label` (text, at most MAX_LINK_LABEL characters) and an `href`: an http: or https: address,
42
+ * or a path in the site (one slash). Nothing else (`javascript:`, `data:`) and no other key. The check is the config's
43
+ * `links` check (core/schema.js); the explorer shows them beside Before / Diff / After and asks again before it draws one.
39
44
  * - An edge is identified by `from` + `to` + `kind`. No `kind` is the ownership edge (parent → child);
40
45
  * a `flow` edge also names its `flow` (one of FLOW_KINDS), and that is part of its identity.
41
46
  * - `parent` on an added node adds the ownership edge `parent → id`; `parent` is not a node field and
@@ -70,15 +75,18 @@
70
75
  * the derived map to them; a proposal is a sketch.
71
76
  */
72
77
 
73
- import { NODE_THEMES, EDGE_KINDS, FLOW_KINDS, DEVICE_KINDS, EXTERNAL_KINDS, LIVE_ID_PREFIX, parseLiveId, isLiveNode, personaNodeId, callFieldProblems } from './schema.js';
78
+ import { NODE_THEMES, linkProblems, EDGE_KINDS, FLOW_KINDS, DEVICE_KINDS, EXTERNAL_KINDS, LIVE_ID_PREFIX, parseLiveId, isLiveNode, personaNodeId, callFieldProblems } from './schema.js';
74
79
 
75
- const TOP_KEYS = ['version', 'spec', 'title', 'summary', 'nodes', 'edges', 'mockups', 'scenarios'];
80
+ const TOP_KEYS = ['version', 'spec', 'title', 'summary', 'nodes', 'edges', 'mockups', 'links', 'scenarios'];
76
81
  const MOCKUP_KEYS = ['node', 'path', 'title'];
77
82
  /** The file types a mockup may be, by lower-case extension. `.html` and `.svg` are untrusted content and are served sandboxed */
78
83
  export const MOCKUP_EXTENSIONS = ['png', 'jpg', 'jpeg', 'webp', 'svg', 'html'];
79
84
  /** Mockups in one proposal, and the characters of a path */
80
85
  export const MAX_MOCKUPS = 40;
81
86
  export const MAX_MOCKUP_PATH = 200;
87
+ /** Links in one proposal, and the characters of a link's label */
88
+ export const MAX_LINKS = 8;
89
+ export const MAX_LINK_LABEL = 60;
82
90
  const SCENARIO_KEYS = ['id', 'title', 'steps', 'stepMs', 'linkNodes', 'actor', 'down'];
83
91
  const STEP_KEYS = ['node', 'edge', 'text', 'ms', 'actor', 'at', 'check', 'expect'];
84
92
  /** What a step's `check` may ask (spec 0029 section 3): a route and a method, or a table, an op and a row shape */
@@ -502,6 +510,11 @@ export function validateProposal(topology, proposal) {
502
510
  });
503
511
  }
504
512
 
513
+ // --- links
514
+ if (proposal.links !== undefined) {
515
+ for (const p of linkProblems(proposal.links, { max: MAX_LINKS, maxLabel: MAX_LINK_LABEL, closed: true })) err(`links${p.at}`, p.message);
516
+ }
517
+
505
518
  return errors;
506
519
  }
507
520
 
package/core/schema.js CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * What a `topology.json` node and edge may be: the node types with their colour and radius, and the
5
5
  * edge kinds, the physical world's rules (spec 0029: live types and ids, which ends an edge kind
6
- * may join) and the cloud's (spec 0031: the kinds of external, a cloud node's fields, `when` and `side`). Pure data and small pure checks with no imports, so the CLI (cli/cornfield.js), the proposal checks
6
+ * may join) and the cloud's (spec 0031: the kinds of external, a cloud node's fields, `when` and `side`), and the one check of a list of links (`linkProblems`: the config's and a proposal's). Pure data and small pure checks with no imports, so the CLI (cli/cornfield.js), the proposal checks
7
7
  * (core/proposal.js) and the explorer in the browser all read the one table.
8
8
  */
9
9
 
@@ -302,3 +302,50 @@ export function callFieldProblems(edge, from, to) {
302
302
  }
303
303
  return problems;
304
304
  }
305
+
306
+ /**
307
+ * Whether `href` is a place a link may lead: an `http:` or `https:` address, or a path in the site (`/docs`). Anything
308
+ * else is refused (`javascript:`, `data:`, a bare `//host`, a backslash, a space or a control character), so a link from a
309
+ * file of the repo or of a pull request can never run script or send the reader somewhere the check did not see.
310
+ * @param {unknown} href
311
+ * @returns {boolean}
312
+ */
313
+ export function isLinkHref(href) {
314
+ return typeof href === 'string' && (/^https?:\/\/[^\s\\/]/i.test(href) || /^\/(?![/\\])/.test(href)) && !/[\u0000-\u001f\u007f\s\\]/.test(href);
315
+ }
316
+
317
+ /**
318
+ * What is wrong with a list of links, `[{ label, href }]`, each with the place in the list it is at (`at`: `[1].href`) and
319
+ * a message that names it. The config's `links` and a proposal's share this one check.
320
+ * @param {unknown} list
321
+ * @param {{ max?: number, maxLabel?: number, closed?: boolean }} [limits] `max` links in the list, `maxLabel` characters in a label, `closed` refuses any key but label and href
322
+ * @returns {{ at: string, message: string }[]}
323
+ */
324
+ export function linkProblems(list, { max = Infinity, maxLabel = Infinity, closed = false } = {}) {
325
+ if (!Array.isArray(list)) return [{ at: '', message: '"links" must be a list of { label, href }.' }];
326
+ const out = [];
327
+ if (list.length > max) out.push({ at: '', message: `"links" has ${list.length} links; at most ${max}.` });
328
+ list.forEach((l, i) => {
329
+ const at = `[${i}]`;
330
+ if (l === null || typeof l !== 'object' || Array.isArray(l)) return out.push({ at, message: `links${at} must be { label, href }: an object with a label and an href.` });
331
+ if (closed) for (const key of Object.keys(l)) if (key !== 'label' && key !== 'href') out.push({ at: `${at}.${key}`, message: `Unknown key "${key}" in a link. A link has: label, href.` });
332
+ if (typeof l.label !== 'string' || l.label.trim() === '') out.push({ at: `${at}.label`, message: `links${at}.label must be the words of the link.` });
333
+ else if (l.label.length > maxLabel) out.push({ at: `${at}.label`, message: `links${at}.label is ${l.label.length} characters; at most ${maxLabel}.` });
334
+ if (!isLinkHref(l.href)) out.push({ at: `${at}.href`, message: `links${at}.href must be an http: or https: address, or a path in the site that starts with a single slash.` });
335
+ });
336
+ return out;
337
+ }
338
+
339
+ /**
340
+ * The links of a list that may be drawn, as `{ label, href }` and nothing else: what passes `linkProblems` entry by entry, at most `max`.
341
+ * A list read from a pull request is untrusted, so the page asks again here and leaves out what it would not have accepted.
342
+ * @param {unknown} list
343
+ * @param {{ max?: number, maxLabel?: number }} [limits]
344
+ * @returns {{ label: string, href: string }[]}
345
+ */
346
+ export function usableLinks(list, { max = Infinity, maxLabel = Infinity } = {}) {
347
+ if (!Array.isArray(list)) return [];
348
+ return list.slice(0, max)
349
+ .filter((l) => l && typeof l === 'object' && !Array.isArray(l) && linkProblems([l], { maxLabel }).length === 0)
350
+ .map((l) => ({ label: l.label, href: l.href }));
351
+ }
@@ -107,16 +107,24 @@ a.hx-proposal-source { min-height: 24px; } /* a link: at least the WCAG 2.5.8 ta
107
107
  a.hx-proposal-source:hover { color: var(--corn-text); text-decoration: underline; }
108
108
  a.hx-proposal-source:focus-visible { outline: var(--corn-focus-width) solid var(--corn-focus); outline-offset: var(--corn-focus-offset); }
109
109
  .hx-proposal-source[data-corn-state="draft"] { border-color: color-mix(in srgb, var(--corn-changed) 50%, transparent); }
110
+ /* The proposal's own links (its `links`): a row of text links under the head, each leaving the page in a new tab */
111
+ .hx-proposal-links { display: flex; flex-wrap: wrap; gap: var(--corn-space-1) var(--corn-space-4); margin-top: var(--corn-space-2); }
112
+ .hx-proposal-links[hidden] { display: none; }
113
+ .hx-proposal-link { display: inline-flex; align-items: center; gap: var(--corn-space-2); min-height: 24px; max-width: 100%; color: var(--corn-text-2); font-size: var(--corn-text-sm); text-decoration: underline; text-underline-offset: 3px; overflow-wrap: anywhere; }
114
+ .hx-proposal-link:hover { color: var(--corn-text); }
115
+ .hx-proposal-link:focus-visible { outline: var(--corn-focus-width) solid var(--corn-focus); outline-offset: var(--corn-focus-offset); }
116
+ .hx-proposal-link svg { flex: none; }
110
117
  .hx-proposal-ways { display: flex; flex-wrap: wrap; gap: var(--corn-space-2) var(--corn-space-3); margin-top: var(--corn-space-3); }
111
118
  .hx-proposal-ways[hidden] { display: none; }
112
119
  .hx-proposal-ways .hx-btn { height: auto; min-height: 32px; padding: var(--corn-space-1) var(--corn-space-4); white-space: normal; text-align: left; }
113
120
  @container corn-box (max-width: 767px) {
114
121
  .hx-proposal-source { grid-column: 1 / -1; order: 1; justify-self: start; }
122
+ .hx-proposal-link { min-height: 44px; font-size: var(--corn-text-md); } /* targets a thumb can hit */
115
123
  .hx-proposal-ways .hx-btn, a.hx-proposal-source { min-height: 44px; } /* targets a thumb can hit */
116
124
  .hx-proposal-source { padding: 0; border: 0; border-radius: 0; background: none; } /* a line of the sheet, not a pill 44px tall */
117
125
  a.hx-proposal-source { text-decoration: underline; text-underline-offset: 3px; }
118
126
  /* The note open: the field, Cancel and the send button are all there is, so all of them stay above the keyboard */
119
- .hx-proposal-bar:has(.hx-review-form:not([hidden])) :is(.hx-proposal-name, .hx-proposal-source, .hx-proposal-counts, .hx-proposal-tools, .hx-proposal-more, .hx-review-status, .hx-review-line, .hx-rc-scroll, .hx-rc-tabs, .hx-proposal-peek) { display: none; } /* the review column's lists too: the sheet holds the note and its buttons */
127
+ .hx-proposal-bar:has(.hx-review-form:not([hidden])) :is(.hx-proposal-name, .hx-proposal-source, .hx-proposal-links, .hx-proposal-counts, .hx-proposal-tools, .hx-proposal-more, .hx-review-status, .hx-review-line, .hx-rc-scroll, .hx-rc-tabs, .hx-proposal-peek) { display: none; } /* the review column's lists too: the sheet holds the note and its buttons */
120
128
  /* Reflow: nothing is wider than the sheet, and long words and counts wrap instead of being cut */
121
129
  .hx-proposal-bar { overflow-x: hidden; overflow-wrap: anywhere; }
122
130
  .hx-proposal-head { grid-template-columns: minmax(0, 1fr) auto; }
@@ -23,7 +23,8 @@
23
23
  */
24
24
 
25
25
  import { escapeHtml as escHtml } from '../../core/text.js';
26
- import { summarize } from '../../core/proposal.js';
26
+ import { usableLinks } from '../../core/schema.js';
27
+ import { summarize, MAX_LINKS, MAX_LINK_LABEL } from '../../core/proposal.js';
27
28
  import { PROPOSAL_VIEWS, chipText, barCounts, proposalPanelHtml, yoursText, sourceChip, pullNumberOf, malformedLinkWords } from '../proposal-model.js';
28
29
  import { reviewWords } from '../review-model.js';
29
30
  import { waitingLineWords } from '../waiting-model.js';
@@ -75,6 +76,8 @@ export const markup = ({ edit = true } = {}) => ({
75
76
  + '</div>'
76
77
  + '</div>'
77
78
  + '</div>'
79
+ // The proposal's own links (`links` of the file): made in renderBar, only when it has some
80
+ + '<nav id="proposal-links" class="hx-proposal-links" aria-label="Links for this proposal" hidden></nav>'
78
81
  + '<details id="proposal-errors" class="hx-proposal-errors" data-corn="proposal-errors" hidden>'
79
82
  + '<summary data-corn="summary" id="proposal-errors-count"></summary>'
80
83
  + '<ul id="proposal-errors-list"></ul>'
@@ -95,6 +98,9 @@ export function problemsHtml(errors, max = 8) {
95
98
  return shown.join('');
96
99
  }
97
100
 
101
+ /** One link of the proposal: it opens in a new tab, and says so to a screen reader. The label is escaped; the href passed `usableLinks` and goes nowhere but its attribute */
102
+ const linkHtml = (l) => `<a class="hx-proposal-link" data-corn="link" href="${escHtml(l.href)}" target="_blank" rel="noopener noreferrer"><span>${escHtml(l.label)}</span><span class="hx-sr-only"> (opens in a new tab)</span><svg viewBox="0 0 16 16" width="12" height="12" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="M6 3h7v7M13 3L7 9" /></svg></a>`;
103
+
98
104
  const plural = (n, word) => `${n} ${word}${n === 1 ? '' : 's'}`;
99
105
  const safeHref = (u) => (typeof u === 'string' && /^https?:\/\//i.test(u) ? u : null);
100
106
 
@@ -233,6 +239,7 @@ export function mount(root, inst, options = {}) {
233
239
  if (yours) chip.insertAdjacentHTML('beforeend', `<span class="hx-proposal-sep" aria-hidden="true"> · </span><span class="hx-proposal-count hx-proposal-yours"><span class="hx-mark-dot hx-mark-dot--edit" data-corn="mark-dot" data-corn-state="edit" aria-hidden="true"></span>${escHtml(yours)}</span>`);
234
240
  addParts(chip);
235
241
  own('proposal-mode').textContent = t(inst, state.proposalView);
242
+ paintLinks(p);
236
243
 
237
244
  for (const btn of bar.querySelectorAll('[data-action="proposal-view"]')) {
238
245
  const on = btn.dataset.view === state.proposalView;
@@ -255,6 +262,15 @@ export function mount(root, inst, options = {}) {
255
262
  }
256
263
  }
257
264
 
265
+ /** The proposal's links, beside Before / Diff / After. A proposal can come from a pull request, so each is checked again here: what is not a link the file format accepts is left out */
266
+ function paintLinks(p) {
267
+ const nav = own('proposal-links');
268
+ const links = usableLinks(p && p.links, { max: MAX_LINKS, maxLabel: MAX_LINK_LABEL });
269
+ nav.hidden = links.length === 0;
270
+ nav.innerHTML = links.map(linkHtml).join('');
271
+ addParts(nav);
272
+ }
273
+
258
274
  // ---------------------------------------------------------------------------
259
275
  // The waiting line: a run waits on the open proposal (spec 0010 D2)
260
276
  // ---------------------------------------------------------------------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cornfield",
3
- "version": "0.57.2",
3
+ "version": "0.58.0",
4
4
  "description": "A self-hostable dev dashboard with the topology map at its centre: proposals on the map, scenarios, outages and what-if edits, for any repo.",
5
5
  "license": "MIT",
6
6
  "author": "popcorn245 (https://github.com/popcorn245)",