@popcorn245/cornfield 0.51.3 → 0.51.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@popcorn245/cornfield",
3
- "version": "0.51.3",
3
+ "version": "0.51.4",
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)",
@@ -24,7 +24,7 @@ const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
24
24
  * An href or src the page may keep: http(s), mailto, or a relative or anchor link. Checked also as a browser reads
25
25
  * it, which drops every tab and newline and any control or space at the start, so `java\tscript:` is `javascript:`.
26
26
  */
27
- const safeUrl = (u) => {
27
+ export const safeUrl = (u) => {
28
28
  if (typeof u !== 'string') return null;
29
29
  const read = u.replace(/[\t\n\r]/g, '').replace(/^[\u0000- ]+/, '');
30
30
  return (WEB_URL.test(u) || !SCHEME.test(u.trim())) && (WEB_URL.test(read) || !SCHEME.test(read)) ? u : null;
@@ -17,7 +17,7 @@ import path from 'node:path';
17
17
  import { escapeHtml as esc } from '../../core/text.js';
18
18
  import { deriveTopology } from '../../core/derive.js';
19
19
  import { readTopology } from '../../core/validate.js';
20
- import { renderMarkdown } from './markdown.js';
20
+ import { renderMarkdown, safeUrl } from './markdown.js';
21
21
  import docsDeriver, { resolveLink } from './index.js';
22
22
 
23
23
  /** The file a built site holds, so a rebuild knows the folder is its own */
@@ -33,10 +33,24 @@ export function sourceUrlOf(config) {
33
33
  return f.type === 'forgejo' ? `${origin}/${f.repo}/src/branch/main` : `${origin}/${f.repo}/blob/HEAD`;
34
34
  }
35
35
 
36
- /** A link from one site file to another, relative */
36
+ /**
37
+ * A link from one site file to another, relative. A file name is data: a `%`, `#`, `?`, `\` (a browser reads it as `/`),
38
+ * space or control character is written as `%XX`, so the link names the file and nothing else; and a first segment
39
+ * that holds a `:` gets a leading `./`, or a browser reads it as a scheme (`javascript:x.html`).
40
+ */
37
41
  const relHref = (fromOutput, toOutput) => {
38
- const rel = path.posix.relative(path.posix.dirname(fromOutput), toOutput);
39
- return rel || path.posix.basename(toOutput);
42
+ const rel = path.posix.relative(path.posix.dirname(fromOutput), toOutput) || path.posix.basename(toOutput);
43
+ const href = rel.replace(/[%#?\\\u0000-\u0020\u007f]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0')}`);
44
+ return href.split('/')[0].includes(':') ? `./${href}` : href;
45
+ };
46
+
47
+ /**
48
+ * A config's URL the site may link to: absolute http or https with a host (`https://`; not `https:x`, which a page
49
+ * on https reads as a path of its own), and one a browser reads as such (see `safeUrl`), or null
50
+ */
51
+ const webUrl = (u) => {
52
+ const url = safeUrl(u);
53
+ return url && /^https?:\/\//i.test(url) ? url : null;
40
54
  };
41
55
 
42
56
  /**
@@ -48,6 +62,7 @@ const relHref = (fromOutput, toOutput) => {
48
62
  * @returns {Record<string, string | { copy: string }>}
49
63
  */
50
64
  export function siteFiles({ title, pages, nodes, edges, read, exists, isDir, sourceUrl = null }) {
65
+ sourceUrl = webUrl(sourceUrl);
51
66
  const files = {};
52
67
  const byId = new Map(pages.map((p) => [p.id, p]));
53
68
  const byFile = new Map(pages.map((p) => [p.file, p]));
@@ -162,7 +177,7 @@ function mapBody({ pages, nodes, out, back }) {
162
177
  const words = `<text class="${cls}" x="${f(tx)}" y="${f(y)}" text-anchor="${right ? 'start' : 'end'}" dominant-baseline="middle" transform="rotate(${f(deg)} ${f(x)} ${f(y)})">${esc(short)}</text>`;
163
178
  return href ? `<a href="${esc(href)}"><title>${esc(text)}</title>${dot}${words}</a>` : `<g><title>${esc(text)}</title>${dot}${words}</g>`;
164
179
  };
165
- const docLabels = pages.map((p) => label(p.id, p.title, p.output, 'doc'));
180
+ const docLabels = pages.map((p) => label(p.id, p.title, relHref('map.html', p.output), 'doc'));
166
181
  const codeLabels = codeIds.map((id) => label(id, nodes.get(id).file || nodes.get(id).label, null, 'code'));
167
182
  const pad = 230;
168
183
  const svg = `<svg class="map" viewBox="${-pad} ${-pad / 2} ${size + 2 * pad} ${size + pad}" role="img" aria-label="The docs and their links">
@@ -173,7 +188,7 @@ ${codeLabels.join('\n')}
173
188
  const rows = pages.map((p) => {
174
189
  const to = out.get(p.id).map((id) => byId.get(id)?.title || nodes.get(id)?.file).filter(Boolean);
175
190
  const from = back.get(p.id).map((id) => byId.get(id).title);
176
- return `<tr><td><a href="${esc(p.output)}">${esc(p.title)}</a><br><code>${esc(p.file)}</code></td><td>${to.length ? esc(to.join(', ')) : '—'}</td><td>${from.length ? esc(from.join(', ')) : '—'}</td></tr>`;
191
+ return `<tr><td><a href="${esc(relHref('map.html', p.output))}">${esc(p.title)}</a><br><code>${esc(p.file)}</code></td><td>${to.length ? esc(to.join(', ')) : '—'}</td><td>${from.length ? esc(from.join(', ')) : '—'}</td></tr>`;
177
192
  });
178
193
  const edgeCount = pages.reduce((s, p) => s + out.get(p.id).length, 0);
179
194
  return `<article class="doc wide">