@popcorn245/cornfield 0.51.3 → 0.51.5
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 +1 -1
- package/plugins/docs/markdown.js +93 -18
- package/plugins/docs/site.js +21 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@popcorn245/cornfield",
|
|
3
|
-
"version": "0.51.
|
|
3
|
+
"version": "0.51.5",
|
|
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)",
|
package/plugins/docs/markdown.js
CHANGED
|
@@ -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;
|
|
@@ -562,12 +562,74 @@ function fenceOf(line) {
|
|
|
562
562
|
if (!m || line.includes('`', m[0].length)) return null;
|
|
563
563
|
return [line, m[1], /^\s*([^`\s]*)/.exec(line.slice(m[0].length))[1]];
|
|
564
564
|
}
|
|
565
|
-
const
|
|
565
|
+
const LINE_BREAK = /[\n\r\u2028\u2029]/;
|
|
566
|
+
/**
|
|
567
|
+
* A heading line's `[line, level, text]`, or null: one to six `#`, then a space or tab (or the end of the line), the text
|
|
568
|
+
* and an optional closing run of `#`. (Read by hand: the one expression for it, `/^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/`,
|
|
569
|
+
* tries every split of a long run of spaces. As the expression's `.` did, a line with a line break in it is no heading.)
|
|
570
|
+
*/
|
|
571
|
+
function headingOf(line) {
|
|
572
|
+
let i = 0;
|
|
573
|
+
while (i < 3 && line[i] === ' ') i++;
|
|
574
|
+
const start = i;
|
|
575
|
+
while (line[i] === '#') i++;
|
|
576
|
+
const level = i - start;
|
|
577
|
+
if (level < 1 || level > 6) return null;
|
|
578
|
+
if (i < line.length && line[i] !== ' ' && line[i] !== '\t') return null;
|
|
579
|
+
if (LINE_BREAK.test(line)) return null;
|
|
580
|
+
while (line[i] === ' ' || line[i] === '\t') i++;
|
|
581
|
+
let end = line.length;
|
|
582
|
+
while (end > i && (line[end - 1] === ' ' || line[end - 1] === '\t')) end--;
|
|
583
|
+
// Cut a closing run of `#` that a space or tab sets off from the text
|
|
584
|
+
let j = end;
|
|
585
|
+
while (j > i && line[j - 1] === '#') j--;
|
|
586
|
+
if (j < end && j > i && (line[j - 1] === ' ' || line[j - 1] === '\t')) {
|
|
587
|
+
end = j;
|
|
588
|
+
while (end > i && (line[end - 1] === ' ' || line[end - 1] === '\t')) end--;
|
|
589
|
+
}
|
|
590
|
+
return [line, level, line.slice(i, end)];
|
|
591
|
+
}
|
|
566
592
|
const RULE = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
|
|
567
593
|
const QUOTE = /^ {0,3}> ?/;
|
|
568
|
-
const
|
|
594
|
+
const LIST_MARK = /^( {0,3})([-*+]|\d{1,9}[.)])([ \t]+|$)/;
|
|
595
|
+
/**
|
|
596
|
+
* A list item line's `[line, indent, marker, spaces, rest of the line]`, or null. (The prefix only is matched by the
|
|
597
|
+
* expression; with a `(.*)$` after it, a run of spaces and a line break at the end of the line made it try every split.)
|
|
598
|
+
*/
|
|
599
|
+
function listItem(line) {
|
|
600
|
+
const m = LIST_MARK.exec(line);
|
|
601
|
+
if (!m || LINE_BREAK.test(line)) return null;
|
|
602
|
+
return [line, m[1], m[2], m[3], line.slice(m[0].length)];
|
|
603
|
+
}
|
|
569
604
|
const HTML_START = /^ {0,3}<\/?(p|div|img|br|hr|details|summary|table|picture|center|section|!--)(?=[\s/>]|$)/i;
|
|
570
|
-
const
|
|
605
|
+
const SPACES = /\s*/y;
|
|
606
|
+
const SEP_CELL = /:?-+:?\s*/y;
|
|
607
|
+
/**
|
|
608
|
+
* Whether a line is a table's separator row: cells of dashes with an optional colon at either end, between pipes (the
|
|
609
|
+
* outer two optional). (By hand: the expression for it, `/^ {0,3}\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/`, tries every split of a long run of spaces.)
|
|
610
|
+
*/
|
|
611
|
+
function isTableSep(line) {
|
|
612
|
+
let i = 0;
|
|
613
|
+
while (i < 3 && line[i] === ' ') i++;
|
|
614
|
+
// Up to three spaces and a pipe; with no pipe the spaces are read as the cell's own leading spaces
|
|
615
|
+
if (line[i] !== '|') i = 0; else i++;
|
|
616
|
+
for (;;) {
|
|
617
|
+
SPACES.lastIndex = i;
|
|
618
|
+
SPACES.test(line);
|
|
619
|
+
SEP_CELL.lastIndex = SPACES.lastIndex;
|
|
620
|
+
if (!SEP_CELL.test(line)) return false;
|
|
621
|
+
i = SEP_CELL.lastIndex;
|
|
622
|
+
if (i === line.length) return true;
|
|
623
|
+
if (line[i] !== '|') return false;
|
|
624
|
+
SPACES.lastIndex = i + 1;
|
|
625
|
+
SPACES.test(line);
|
|
626
|
+
i = SPACES.lastIndex;
|
|
627
|
+
if (i === line.length) return true;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** How many empty cells a document's tables may add to rows shorter than their header (see renderBlocks) */
|
|
632
|
+
const MAX_PADDING = 65536;
|
|
571
633
|
|
|
572
634
|
const isBlank = (l) => /^\s*$/.test(l);
|
|
573
635
|
const indentOf = (l) => /^ */.exec(l.replace(/\t/g, ' '))[0].length;
|
|
@@ -592,12 +654,16 @@ function cells(line) {
|
|
|
592
654
|
}
|
|
593
655
|
|
|
594
656
|
/** Whether a line starts a block other than a paragraph (it interrupts a paragraph) */
|
|
595
|
-
const startsBlock = (line) =>
|
|
657
|
+
const startsBlock = (line) => {
|
|
658
|
+
if (fenceOf(line) || headingOf(line) || RULE.test(line) || QUOTE.test(line) || HTML_START.test(line)) return true;
|
|
659
|
+
const item = listItem(line);
|
|
660
|
+
return Boolean(item) && !isBlank(item[4]);
|
|
661
|
+
};
|
|
596
662
|
|
|
597
663
|
/**
|
|
598
664
|
* Lines of Markdown as HTML.
|
|
599
665
|
* @param {string[]} lines
|
|
600
|
-
* @param {{ link: Function, headings: object[], slugs: Map<string, number
|
|
666
|
+
* @param {{ link: Function, headings: object[], slugs: Map<string, number>, padding: number }} ctx (padding: the empty cells a table may still add)
|
|
601
667
|
* @param {boolean} [tight] paragraphs without <p> (a tight list item)
|
|
602
668
|
* @param {number} [depth] how many lists and quotes these lines sit in (from MAX_NEST on, a list or quote is text)
|
|
603
669
|
*/
|
|
@@ -623,10 +689,9 @@ function renderBlocks(lines, ctx, tight = false, depth = 0) {
|
|
|
623
689
|
continue;
|
|
624
690
|
}
|
|
625
691
|
|
|
626
|
-
const heading =
|
|
692
|
+
const heading = headingOf(line);
|
|
627
693
|
if (heading) {
|
|
628
|
-
const level = heading
|
|
629
|
-
const text = heading[2] || '';
|
|
694
|
+
const [, level, text] = heading;
|
|
630
695
|
const base = headingSlug(text);
|
|
631
696
|
const n = ctx.slugs.get(base) || 0;
|
|
632
697
|
ctx.slugs.set(base, n + 1);
|
|
@@ -653,7 +718,7 @@ function renderBlocks(lines, ctx, tight = false, depth = 0) {
|
|
|
653
718
|
continue;
|
|
654
719
|
}
|
|
655
720
|
|
|
656
|
-
if (line.includes('|') && i + 1 < lines.length &&
|
|
721
|
+
if (line.includes('|') && i + 1 < lines.length && isTableSep(lines[i + 1]) && lines[i + 1].includes('-')) {
|
|
657
722
|
const head = cells(line);
|
|
658
723
|
const align = cells(lines[i + 1]).map((c) => (c.startsWith(':') && c.endsWith(':') ? 'center' : c.endsWith(':') ? 'right' : c.startsWith(':') ? 'left' : null));
|
|
659
724
|
const cell = (tag, text, k) => `<${tag}${align[k] ? ` style="text-align:${align[k]}"` : ''}>${renderInline(text, ctx)}</${tag}>`;
|
|
@@ -661,13 +726,20 @@ function renderBlocks(lines, ctx, tight = false, depth = 0) {
|
|
|
661
726
|
const rows = [];
|
|
662
727
|
while (i < lines.length && !isBlank(lines[i]) && lines[i].includes('|')) {
|
|
663
728
|
const row = cells(lines[i++]);
|
|
664
|
-
|
|
729
|
+
// A short row is padded with empty cells to the header's width. A wide header over many short rows would so make output
|
|
730
|
+
// in the square of the input, so a document adds at most MAX_PADDING such cells: a row that would pass that keeps its own
|
|
731
|
+
let width = head.length;
|
|
732
|
+
if (row.length < width) {
|
|
733
|
+
if (width - row.length <= ctx.padding) ctx.padding -= width - row.length;
|
|
734
|
+
else width = row.length;
|
|
735
|
+
}
|
|
736
|
+
rows.push(`<tr>${head.slice(0, width).map((_, k) => cell('td', row[k] ?? '', k)).join('')}</tr>`);
|
|
665
737
|
}
|
|
666
738
|
out.push(`<div class="table"><table>\n<thead><tr>${head.map((h, k) => cell('th', h, k)).join('')}</tr></thead>\n<tbody>\n${rows.join('\n')}\n</tbody>\n</table></div>`);
|
|
667
739
|
continue;
|
|
668
740
|
}
|
|
669
741
|
|
|
670
|
-
const item = depth < MAX_NEST &&
|
|
742
|
+
const item = depth < MAX_NEST && listItem(line);
|
|
671
743
|
if (item) {
|
|
672
744
|
const ordered = /\d/.test(item[2]);
|
|
673
745
|
const kind = ordered ? item[2].slice(-1) : item[2];
|
|
@@ -675,28 +747,31 @@ function renderBlocks(lines, ctx, tight = false, depth = 0) {
|
|
|
675
747
|
const items = [];
|
|
676
748
|
let loose = false;
|
|
677
749
|
while (i < lines.length) {
|
|
678
|
-
const m =
|
|
750
|
+
const m = listItem(lines[i]);
|
|
679
751
|
if (!m || (/\d/.test(m[2]) ? m[2].slice(-1) : m[2]) !== kind) break;
|
|
680
752
|
const contentIndent = m[1].length + m[2].length + Math.min(m[3].length || 1, 4);
|
|
681
753
|
const body = [m[4]];
|
|
682
754
|
i++;
|
|
683
755
|
let sawBlank = false;
|
|
756
|
+
let filled = m[4] === '' ? 0 : 1;
|
|
684
757
|
while (i < lines.length) {
|
|
685
758
|
const l = lines[i];
|
|
686
759
|
if (isBlank(l)) { sawBlank = true; body.push(''); i++; continue; }
|
|
687
760
|
if (indentOf(l) >= contentIndent) {
|
|
688
|
-
if (sawBlank &&
|
|
689
|
-
|
|
761
|
+
if (sawBlank && filled) loose = loose || !listItem(l.slice(contentIndent));
|
|
762
|
+
const text = l.replace(/\t/g, ' ').slice(contentIndent);
|
|
763
|
+
body.push(text);
|
|
764
|
+
if (text !== '') filled++;
|
|
690
765
|
sawBlank = false;
|
|
691
766
|
i++;
|
|
692
767
|
continue;
|
|
693
768
|
}
|
|
694
769
|
// A lazy continuation of the item's paragraph
|
|
695
|
-
if (!sawBlank && !startsBlock(l) && !
|
|
770
|
+
if (!sawBlank && !startsBlock(l) && !listItem(l)) { body.push(l.trim()); i++; filled++; continue; }
|
|
696
771
|
break;
|
|
697
772
|
}
|
|
698
773
|
while (body.length && body[body.length - 1] === '') body.pop();
|
|
699
|
-
if (sawBlank && i < lines.length &&
|
|
774
|
+
if (sawBlank && i < lines.length && listItem(lines[i])) loose = true;
|
|
700
775
|
items.push(body);
|
|
701
776
|
}
|
|
702
777
|
const tag = ordered ? 'ol' : 'ul';
|
|
@@ -723,7 +798,7 @@ function renderBlocks(lines, ctx, tight = false, depth = 0) {
|
|
|
723
798
|
* @returns {{ html: string, title: string | null, headings: { level: number, text: string, id: string }[] }}
|
|
724
799
|
*/
|
|
725
800
|
export function renderMarkdown(text, { link = (href) => href } = {}) {
|
|
726
|
-
const ctx = { link, headings: [], slugs: new Map() };
|
|
801
|
+
const ctx = { link, headings: [], slugs: new Map(), padding: MAX_PADDING };
|
|
727
802
|
const html = renderBlocks(String(text).replace(/\r\n?/g, '\n').split('\n'), ctx);
|
|
728
803
|
const h1 = ctx.headings.find((h) => h.level === 1);
|
|
729
804
|
return { html, title: h1 ? h1.text : null, headings: ctx.headings };
|
package/plugins/docs/site.js
CHANGED
|
@@ -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
|
-
/**
|
|
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
|
-
|
|
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">
|