@popcorn245/cornfield 0.51.0 → 0.51.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/package.json +1 -1
- package/plugins/docs/markdown.js +126 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@popcorn245/cornfield",
|
|
3
|
-
"version": "0.51.
|
|
3
|
+
"version": "0.51.1",
|
|
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
|
@@ -26,20 +26,136 @@ const safeUrl = (u) => (typeof u === 'string' && (/^(https?:|mailto:)/i.test(u)
|
|
|
26
26
|
* @param {string} text the heading's plain text
|
|
27
27
|
*/
|
|
28
28
|
export function slugify(text) {
|
|
29
|
-
|
|
29
|
+
// Letters keep their marks (the vowel signs of Devanagari, a decomposed \u00e9); only a plain space becomes a hyphen,
|
|
30
|
+
// so a no-break space or a tab is dropped, as GitHub drops it
|
|
31
|
+
return text.toLowerCase().replace(/[^\p{L}\p{M}\p{N}_ -]/gu, '').replace(/ /g, '-');
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
const NAMED_ENTITIES = {
|
|
35
|
+
lt: '<', gt: '>', amp: '&', quot: '"', apos: "'", nbsp: '\u00a0', copy: '\u00a9', reg: '\u00ae', trade: '\u2122', hellip: '\u2026',
|
|
36
|
+
mdash: '\u2014', ndash: '\u2013', middot: '\u00b7', bull: '\u2022', times: '\u00d7', deg: '\u00b0', laquo: '\u00ab', raquo: '\u00bb',
|
|
37
|
+
lsquo: '\u2018', rsquo: '\u2019', ldquo: '\u201c', rdquo: '\u201d', larr: '\u2190', rarr: '\u2192', uarr: '\u2191', darr: '\u2193', harr: '\u2194',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** A character reference's text (`<`, `<`, `<`); one this does not know stays as written */
|
|
41
|
+
const decodeEntities = (s) => s.replace(/&(?:#(\d{1,7})|#[xX]([0-9a-fA-F]{1,6})|([a-zA-Z]+));/g, (all, dec, hex, name) => {
|
|
42
|
+
if (name) return NAMED_ENTITIES[name] ?? all;
|
|
43
|
+
const cp = dec ? Number(dec) : parseInt(hex, 16);
|
|
44
|
+
return cp > 0 && cp <= 0x10ffff && !(cp >= 0xd800 && cp <= 0xdfff) ? String.fromCodePoint(cp) : all;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const WORD = /[\p{L}\p{N}]/u;
|
|
48
|
+
const isWordAt = (s, i) => i >= 0 && i < s.length && WORD.test(String.fromCodePoint(s.codePointAt(i)));
|
|
49
|
+
const isWordBefore = (s, i) => isWordAt(s, i >= 2 && /[\udc00-\udfff]/.test(s[i - 1]) && /[\ud800-\udbff]/.test(s[i - 2]) ? i - 2 : i - 1);
|
|
50
|
+
const PUNCT = /[!-/:-@[-`{-~]/;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Backslash escapes and code spans, in one pass from the left as CommonMark reads them: an escaped mark and a code
|
|
54
|
+
* span's text are handed to `keep` (a backslash inside code is literal), an unmatched run of backticks is text.
|
|
55
|
+
* Linear: a run is closed by the next run of the same length, found from the runs listed once.
|
|
56
|
+
*/
|
|
57
|
+
function codeAndEscapes(s, keep) {
|
|
58
|
+
const runs = new Map();
|
|
59
|
+
for (const m of s.matchAll(/`+/g)) {
|
|
60
|
+
if (!runs.has(m[0].length)) runs.set(m[0].length, { at: [], next: 0 });
|
|
61
|
+
runs.get(m[0].length).at.push(m.index);
|
|
62
|
+
}
|
|
63
|
+
let out = '';
|
|
64
|
+
let i = 0;
|
|
65
|
+
while (i < s.length) {
|
|
66
|
+
const c = s[i];
|
|
67
|
+
if (c === '\\' && PUNCT.test(s[i + 1] || '')) { out += keep(s[i + 1]); i += 2; continue; }
|
|
68
|
+
if (c !== '`') { out += c; i++; continue; }
|
|
69
|
+
let n = 1;
|
|
70
|
+
while (s[i + n] === '`') n++;
|
|
71
|
+
const same = runs.get(n);
|
|
72
|
+
// A run that starts after an escaped backtick is not in the list (it is shorter than the run it is part of)
|
|
73
|
+
while (same && same.next < same.at.length && same.at[same.next] < i + n) same.next++;
|
|
74
|
+
const close = same && same.next < same.at.length ? same.at[same.next] : -1;
|
|
75
|
+
if (close < 0) { out += s.slice(i, i + n); i += n; continue; }
|
|
76
|
+
const code = s.slice(i + n, close).replace(/\n/g, ' ');
|
|
77
|
+
out += keep(/^ .*[^ ].* $/s.test(code) ? code.slice(1, -1) : code);
|
|
78
|
+
i = close + n;
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Each `<!-- … -->` taken out (linear: once one has no end, no later one has) */
|
|
84
|
+
function dropComments(s) {
|
|
85
|
+
let out = '';
|
|
86
|
+
let i = 0;
|
|
87
|
+
for (;;) {
|
|
88
|
+
const open = s.indexOf('<!--', i);
|
|
89
|
+
const close = open < 0 ? -1 : s.indexOf('-->', open + 4);
|
|
90
|
+
if (close < 0) return out + s.slice(i);
|
|
91
|
+
out += s.slice(i, open);
|
|
92
|
+
i = close + 3;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* `_em_` and `__strong__` as their text: a mark opens only where no letter or digit is before it and closes only
|
|
98
|
+
* where none is after it, so `snake_case_name` keeps its underscores. Linear: the next closing mark from each
|
|
99
|
+
* place is worked out once, from the right.
|
|
100
|
+
*/
|
|
101
|
+
function dropUnderscores(s) {
|
|
102
|
+
const n = s.length;
|
|
103
|
+
const close1 = new Int32Array(n + 3).fill(-1);
|
|
104
|
+
const close2 = new Int32Array(n + 3).fill(-1);
|
|
105
|
+
const newline = new Int32Array(n + 3).fill(-1);
|
|
106
|
+
for (let p = n - 1; p >= 0; p--) {
|
|
107
|
+
close1[p] = s[p] === '_' && !isWordAt(s, p + 1) ? p : close1[p + 1];
|
|
108
|
+
close2[p] = s[p] === '_' && s[p + 1] === '_' && !isWordAt(s, p + 2) ? p : close2[p + 1];
|
|
109
|
+
newline[p] = s[p] === '\n' ? p : newline[p + 1];
|
|
110
|
+
}
|
|
111
|
+
let out = '';
|
|
112
|
+
let i = 0;
|
|
113
|
+
outer: while (i < n) {
|
|
114
|
+
if (s[i] === '_' && !isWordBefore(s, i)) {
|
|
115
|
+
for (const mark of s[i + 1] === '_' ? [2, 1] : [1]) {
|
|
116
|
+
const start = i + mark;
|
|
117
|
+
if (start >= n || /\s/u.test(s[start])) continue;
|
|
118
|
+
const end = (mark === 2 ? close2 : close1)[start + 1];
|
|
119
|
+
if (end < 0 || (newline[start] >= 0 && newline[start] < end)) continue;
|
|
120
|
+
out += s.slice(start, end);
|
|
121
|
+
i = end + mark;
|
|
122
|
+
continue outer;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
out += s[i];
|
|
126
|
+
i++;
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The text a heading or link text renders to, the way GitHub reads it before it makes a heading's anchor, untrimmed:
|
|
133
|
+
* a code span keeps its literal text (so `<tag>` in backticks stays, brackets and all), a real inline HTML tag
|
|
134
|
+
* outside code gives only its inner text, emphasis and links give their text, an autolink its address, and a
|
|
135
|
+
* character reference is its character. An image gives its alt text, or nothing with `images: false` (GitHub's
|
|
136
|
+
* anchor reads the heading's text, and an image has none).
|
|
137
|
+
*/
|
|
138
|
+
function inlineText(src, { images = true } = {}) {
|
|
139
|
+
// What must reach the end as written (code spans, escaped marks, autolinks) is set aside first, so no later step
|
|
140
|
+
// reads it. A NUL in the source is U+FFFD (as CommonMark has it), so the source cannot forge a set-aside mark.
|
|
141
|
+
const kept = [];
|
|
142
|
+
const keep = (t) => `\u0000${kept.push(t) - 1}\u0000`;
|
|
143
|
+
let s = codeAndEscapes(String(src).replace(/\u0000/g, '�'), keep);
|
|
144
|
+
s = s.replace(/<((?:https?:\/\/|mailto:)[^\s<>]+)>/gi, (_, url) => keep(url.replace(/^mailto:/i, '')));
|
|
145
|
+
// A link or image: `[text](dest "title")`; the spaces are matched so that no two runs of them can trade places
|
|
146
|
+
s = s.replace(/(!?)\[([^[\]]*)\]\(\s*(?:(?:<[^<>\n]*>|(?:[^()\s]|\([^()\s]*\))+)(?:\s+(?:"[^"]*"|'[^']*'|\([^()]*\)))?\s*|(?:"[^"]*"|'[^']*')\s*)?\)/g, (_, bang, text) => (bang && !images ? '' : text));
|
|
147
|
+
s = dropComments(s).replace(/<\/?[a-zA-Z][^<>]*>/g, '').replace(/(\*\*|\*)(?=\S)(.+?)\1/g, '$2');
|
|
148
|
+
return decodeEntities(dropUnderscores(s)).replace(/\u0000(\d+)\u0000/g, (_, k) => kept[k]);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A heading's or link text's plain words (see inlineText), trimmed */
|
|
33
152
|
export function plainText(src) {
|
|
34
|
-
return src
|
|
35
|
-
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
36
|
-
.replace(/`+([^`]*)`+/g, '$1')
|
|
37
|
-
.replace(/(\*\*|__|\*|_)(.+?)\1/g, '$2')
|
|
38
|
-
.replace(/<[^>]+>/g, '')
|
|
39
|
-
.replace(/\\(.)/g, '$1')
|
|
40
|
-
.trim();
|
|
153
|
+
return inlineText(src).trim();
|
|
41
154
|
}
|
|
42
155
|
|
|
156
|
+
/** A heading's anchor before it is numbered: from its text as GitHub reads it, images giving nothing, untrimmed */
|
|
157
|
+
const headingSlug = (src) => slugify(inlineText(src, { images: false }));
|
|
158
|
+
|
|
43
159
|
// ---------------------------------------------------------------------------
|
|
44
160
|
// Inline
|
|
45
161
|
// ---------------------------------------------------------------------------
|
|
@@ -286,7 +402,7 @@ function renderBlocks(lines, ctx, tight = false) {
|
|
|
286
402
|
if (heading) {
|
|
287
403
|
const level = heading[1].length;
|
|
288
404
|
const text = heading[2] || '';
|
|
289
|
-
const base =
|
|
405
|
+
const base = headingSlug(text);
|
|
290
406
|
const n = ctx.slugs.get(base) || 0;
|
|
291
407
|
ctx.slugs.set(base, n + 1);
|
|
292
408
|
const id = n ? `${base}-${n}` : base;
|