@popcorn245/cornfield 0.51.1 → 0.51.2

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.1",
3
+ "version": "0.51.2",
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)",
@@ -18,8 +18,17 @@ const ALLOWED_TAGS = {
18
18
  details: ['open'], summary: [], sub: [], sup: [], kbd: [], b: [], i: [], em: [], strong: [], code: [], a: ['href', 'title'],
19
19
  };
20
20
  const VOID_TAGS = new Set(['img', 'br', 'hr']);
21
- /** An href or src the page may keep: http(s), mailto, or a relative or anchor link */
22
- const safeUrl = (u) => (typeof u === 'string' && (/^(https?:|mailto:)/i.test(u) || !/^[a-z][a-z0-9+.-]*:/i.test(u.trim())) ? u : null);
21
+ const WEB_URL = /^(https?:|mailto:)/i;
22
+ const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
23
+ /**
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
+ * it, which drops every tab and newline and any control or space at the start, so `java\tscript:` is `javascript:`.
26
+ */
27
+ const safeUrl = (u) => {
28
+ if (typeof u !== 'string') return null;
29
+ const read = u.replace(/[\t\n\r]/g, '').replace(/^[\u0000- ]+/, '');
30
+ return (WEB_URL.test(u) || !SCHEME.test(u.trim())) && (WEB_URL.test(read) || !SCHEME.test(read)) ? u : null;
31
+ };
23
32
 
24
33
  /**
25
34
  * The anchor GitHub gives a heading: lower case, punctuation dropped, spaces to hyphens.
@@ -160,90 +169,216 @@ const headingSlug = (src) => slugify(inlineText(src, { images: false }));
160
169
  // Inline
161
170
  // ---------------------------------------------------------------------------
162
171
 
163
- /** The `[text](dest "title")` starting at `i` (src[i] === '['), or null */
164
- function parseLink(src, i) {
165
- let depth = 0;
166
- let j = i;
167
- for (; j < src.length; j++) {
168
- const c = src[j];
169
- if (c === '\\') { j++; continue; }
170
- if (c === '`') {
171
- const run = /^`+/.exec(src.slice(j))[0];
172
- const close = src.indexOf(run, j + run.length);
173
- if (close > 0) { j = close + run.length - 1; continue; }
172
+ /**
173
+ * The deepest a link or emphasis may sit inside another, and a list or quote inside another: each level reads its
174
+ * text again, so deeper marks stay as written (and no crafted line can run the reader out of stack)
175
+ */
176
+ const MAX_NEST = 16;
177
+
178
+ /**
179
+ * What a line's links and code spans need, each worked out once, from the right, so that no failed match makes a
180
+ * later one look ahead to the end of the line again:
181
+ * - `up[x]`: the first `]` after x that is not matched by a `[` after x (or -1), reading as a link's text is read: an
182
+ * escaped character and a code span (a run of backticks up to the next run at least as long) are stepped over whole.
183
+ * For a `[` at x, that is the `]` which closes it;
184
+ * - `kRun[x]`: for a run of backticks that starts (or, after an escape, continues) at x, the next run after it at
185
+ * least as long, which a code span may close on (or -1).
186
+ * The next run at least that long is found among the runs after the one being passed, kept as a stack in which each
187
+ * is longer than every one nearer to it, so it is a binary search.
188
+ */
189
+ function scanIndex(src) {
190
+ const n = src.length;
191
+ const rs = [];
192
+ const re = [];
193
+ for (let p = 0; p < n; p++) {
194
+ if (src.charCodeAt(p) !== 96) continue;
195
+ let q = p + 1;
196
+ while (src.charCodeAt(q) === 96) q++;
197
+ rs.push(p);
198
+ re.push(q);
199
+ p = q - 1;
200
+ }
201
+ const up = new Int32Array(n + 1).fill(-1);
202
+ const kRun = new Int32Array(n + 1).fill(-1);
203
+ const stack = []; // runs after the one passed, the nearest last; each longer than all nearer ones
204
+ // The last of the stack with a length of at least m (there is one when m is at most the longest)
205
+ const nearest = (m) => {
206
+ let lo = 0;
207
+ let hi = stack.length - 1;
208
+ while (lo < hi) {
209
+ const mid = (lo + hi + 1) >> 1;
210
+ if (re[stack[mid]] - rs[stack[mid]] >= m) lo = mid; else hi = mid - 1;
211
+ }
212
+ return stack[lo];
213
+ };
214
+ let r = rs.length - 1;
215
+ for (let x = n - 1; x >= 0; x--) {
216
+ const c = src.charCodeAt(x);
217
+ let next = x + 1; // where the bracket scan goes on from x
218
+ if (c === 92) next = Math.min(x + 2, n);
219
+ else if (c === 96) {
220
+ const len = re[r] - x;
221
+ const longest = stack.length ? re[stack[0]] - rs[stack[0]] : 0;
222
+ if (!longest) next = re[r];
223
+ else {
224
+ // A code span that finds no end gives the scan one character on, and the run one shorter to try; so the scan
225
+ // lands after the nearest run that is as long as the longest it can use
226
+ const use = Math.min(len, longest);
227
+ const k = nearest(use);
228
+ next = rs[k] + use;
229
+ if (len <= longest) kRun[x] = k;
230
+ }
231
+ }
232
+ // From `next`: a `]` is the answer; a `[` opens a pair of its own, closed at up[next], and the scan goes on after
233
+ // that `]` (which up[] at the `]` gives); anything else, the scan goes on from it
234
+ const cn = next < n ? src.charCodeAt(next) : 0;
235
+ if (next >= n) up[x] = -1;
236
+ else if (cn === 93) up[x] = next;
237
+ else if (cn === 91) up[x] = up[next] < 0 ? -1 : up[up[next]];
238
+ else up[x] = up[next];
239
+ if (c === 96 && x === rs[r]) {
240
+ while (stack.length && re[stack[stack.length - 1]] - rs[stack[stack.length - 1]] <= re[r] - rs[r]) stack.pop();
241
+ stack.push(r--);
174
242
  }
175
- if (c === '[') depth++;
176
- else if (c === ']' && --depth === 0) break;
177
243
  }
178
- if (depth !== 0 || src[j + 1] !== '(') return null;
244
+ return { up, kRun, rs, re };
245
+ }
246
+
247
+ /** For the ends of a link, each a table over the places in the line, worked out once from the right (see parseLink) */
248
+ function linkTables(src) {
249
+ const n = src.length;
250
+ const skip = new Int32Array(n + 3); // the first place at or after x that is not a space
251
+ const gt = new Int32Array(n + 3).fill(-1); // the next `>`
252
+ const dq = new Int32Array(n + 3).fill(-1); // the next `"`
253
+ const sq = new Int32Array(n + 3).fill(-1); // the next `'`
254
+ const stop = new Int32Array(n + 3); // where a bare destination read from x ends
255
+ for (let x = n; x < n + 3; x++) { skip[x] = x; stop[x] = n; }
256
+ for (let x = n - 1; x >= 0; x--) {
257
+ const c = src.charCodeAt(x);
258
+ skip[x] = c === 32 ? skip[x + 1] : x;
259
+ gt[x] = c === 62 ? x : gt[x + 1];
260
+ dq[x] = c === 34 ? x : dq[x + 1];
261
+ sq[x] = c === 39 ? x : sq[x + 1];
262
+ if (c === 92 && x + 1 < n) stop[x] = stop[x + 2]; // an escaped character is stepped over
263
+ else if (c === 32 || c === 41) stop[x] = x; // a space, or a `)` that closes no `(` of the destination
264
+ else if (c === 40) {
265
+ // A `(` is closed by the first `)` its own text leaves unclosed; the destination goes on after that
266
+ const m = stop[x + 1];
267
+ stop[x] = m >= n || src.charCodeAt(m) === 32 ? m : stop[m + 1];
268
+ } else stop[x] = stop[x + 1];
269
+ }
270
+ return { skip, gt, dq, sq, stop };
271
+ }
272
+
273
+ /**
274
+ * The `[text](dest "title")` starting at `i` (src[i] === '['), or null. `index` and `tables` give what scanIndex and
275
+ * linkTables worked out for `src`, each built when first asked for.
276
+ */
277
+ function parseLink(src, i, index, tables) {
278
+ const j = index().up[i];
279
+ if (j < 0 || src[j + 1] !== '(') return null;
280
+ const t = tables();
179
281
  const text = src.slice(i + 1, j);
180
- let k = j + 2;
181
- while (src[k] === ' ') k++;
182
- let dest = '';
282
+ let k = t.skip[j + 2];
283
+ let dest;
284
+ let bare = -1;
183
285
  if (src[k] === '<') {
184
- const close = src.indexOf('>', k);
286
+ const close = t.gt[k];
185
287
  if (close < 0) return null;
186
288
  dest = src.slice(k + 1, close);
187
289
  k = close + 1;
188
290
  } else {
189
- let parens = 0;
190
- for (; k < src.length; k++) {
191
- const c = src[k];
192
- if (c === '\\' && k + 1 < src.length) { dest += src[++k]; continue; }
193
- if (c === ' ' || (c === ')' && parens === 0)) break;
194
- if (c === '(') parens++;
195
- if (c === ')') parens--;
196
- dest += c;
197
- }
291
+ bare = k;
292
+ k = t.stop[k];
198
293
  }
199
- while (src[k] === ' ') k++;
294
+ const destEnd = k;
295
+ k = t.skip[k];
200
296
  let title = null;
201
297
  const quote = src[k];
202
298
  if (quote === '"' || quote === "'") {
203
- const close = src.indexOf(quote, k + 1);
299
+ const close = (quote === '"' ? t.dq : t.sq)[k + 1];
204
300
  if (close < 0) return null;
205
301
  title = src.slice(k + 1, close);
206
- k = close + 1;
207
- while (src[k] === ' ') k++;
302
+ k = t.skip[close + 1];
208
303
  }
209
304
  if (src[k] !== ')') return null;
305
+ if (bare >= 0) {
306
+ dest = '';
307
+ for (let p = bare; p < destEnd; p++) {
308
+ if (src[p] === '\\' && p + 1 < src.length) p++;
309
+ dest += src[p];
310
+ }
311
+ }
210
312
  return { text, dest, title, end: k + 1 };
211
313
  }
212
314
 
213
315
  const isWordChar = (c) => c !== undefined && /[\p{L}\p{N}]/u.test(c);
214
316
 
215
317
  /**
216
- * One line (or a paragraph's lines) of inline Markdown as HTML.
318
+ * Where the mark that opens at some place is closed (`close[from]`, or -1), for a mark of one character or two, from
319
+ * the place the search starts: the first mark from there that has no space before it, is not (for `_`) followed by a
320
+ * letter or digit, and (for one character) is not the first of two. A pair is stepped over whole.
321
+ */
322
+ function closers(src, c, strong) {
323
+ const n = src.length;
324
+ const close = new Int32Array(n + 4).fill(-1);
325
+ for (let q = n - 1; q >= 0; q--) {
326
+ if (src[q] !== c || (strong && src[q + 1] !== c)) close[q] = close[q + 1];
327
+ else if (!strong && src[q + 1] === c) close[q] = close[q + 2];
328
+ else close[q] = /\s/.test(src[q - 1]) || (c === '_' && isWordChar(src[q + (strong ? 2 : 1)])) ? close[q + 1] : q;
329
+ }
330
+ return close;
331
+ }
332
+
333
+ const AUTOLINK = /<((?:https?:\/\/|mailto:)[^\s<>]+)>/iy;
334
+
335
+ /**
336
+ * One line (or a paragraph's lines) of inline Markdown as HTML. Linear in the length of the line (times the depth
337
+ * to which marks sit inside one another, at most MAX_NEST).
217
338
  * @param {string} src
218
339
  * @param {{ link: Function }} ctx
219
340
  */
220
341
  export function renderInline(src, ctx) {
342
+ return inline(src, ctx, 0);
343
+ }
344
+
345
+ function inline(src, ctx, depth) {
346
+ let ix = null;
347
+ let lt = null;
348
+ const index = () => (ix ??= scanIndex(src));
349
+ const tables = () => (lt ??= linkTables(src));
350
+ const marks = {};
351
+ let linkAt = -1; // the link parsed at this place, for the image attempt and then the link attempt at the same `[`
352
+ let link = null;
221
353
  let out = '';
222
354
  let i = 0;
223
355
  while (i < src.length) {
224
356
  const c = src[i];
225
357
  if (c === '\\' && /[!-/:-@[-`{-~]/.test(src[i + 1] || '')) { out += esc(src[i + 1]); i += 2; continue; }
226
358
  if (c === '\n') {
227
- out += / $/.test(src.slice(0, i)) ? '<br>\n' : '\n';
359
+ out += i >= 2 && src[i - 1] === ' ' && src[i - 2] === ' ' ? '<br>\n' : '\n';
228
360
  i++;
229
361
  continue;
230
362
  }
231
363
  if (c === '`') {
232
- const run = /^`+/.exec(src.slice(i))[0];
233
- const close = src.indexOf(run, i + run.length);
234
- if (close > 0 && src[close + run.length] !== '`') {
235
- let code = src.slice(i + run.length, close).replace(/\n/g, ' ');
364
+ const { kRun, rs, re } = index();
365
+ let n = 1;
366
+ while (src[i + n] === '`') n++;
367
+ const k = kRun[i];
368
+ if (k >= 0 && re[k] - rs[k] === n) {
369
+ let code = src.slice(i + n, rs[k]).replace(/\n/g, ' ');
236
370
  if (/^ .* $/.test(code)) code = code.slice(1, -1);
237
371
  out += `<code>${esc(code)}</code>`;
238
- i = close + run.length;
372
+ i = re[k];
239
373
  continue;
240
374
  }
241
- out += esc(run);
242
- i += run.length;
375
+ out += esc(src.slice(i, i + n));
376
+ i += n;
243
377
  continue;
244
378
  }
245
379
  if (c === '!' && src[i + 1] === '[') {
246
- const l = parseLink(src, i + 1);
380
+ if (linkAt !== i + 1) { link = parseLink(src, i + 1, index, tables); linkAt = i + 1; }
381
+ const l = link;
247
382
  if (l) {
248
383
  const href = safeUrl(ctx.link(l.dest, { image: true }));
249
384
  const alt = plainText(l.text);
@@ -252,44 +387,37 @@ export function renderInline(src, ctx) {
252
387
  continue;
253
388
  }
254
389
  }
255
- if (c === '[') {
256
- const l = parseLink(src, i);
390
+ if (c === '[' && depth < MAX_NEST) {
391
+ if (linkAt !== i) { link = parseLink(src, i, index, tables); linkAt = i; }
392
+ const l = link;
257
393
  if (l) {
258
394
  const href = safeUrl(ctx.link(l.dest, { image: false }));
259
- const inner = renderInline(l.text, ctx);
395
+ const inner = inline(l.text, ctx, depth + 1);
260
396
  out += href ? `<a href="${esc(href)}"${l.title ? ` title="${esc(l.title)}"` : ''}>${inner}</a>` : inner;
261
397
  i = l.end;
262
398
  continue;
263
399
  }
264
400
  }
265
401
  if (c === '<') {
266
- const auto = /^<((?:https?:\/\/|mailto:)[^\s<>]+)>/i.exec(src.slice(i));
402
+ AUTOLINK.lastIndex = i;
403
+ const auto = AUTOLINK.exec(src);
267
404
  if (auto) {
268
405
  out += `<a href="${esc(auto[1])}">${esc(auto[1].replace(/^mailto:/i, ''))}</a>`;
269
406
  i += auto[0].length;
270
407
  continue;
271
408
  }
272
409
  }
273
- if (c === '*' || c === '_') {
410
+ if ((c === '*' || c === '_') && depth < MAX_NEST) {
274
411
  const strong = src[i + 1] === c;
275
- const mark = strong ? c + c : c;
276
- const opens = !/\s/.test(src[i + mark.length] || ' ') && (c === '*' || !isWordChar(src[i - 1]));
412
+ const size = strong ? 2 : 1;
413
+ const opens = !/\s/.test(src[i + size] || ' ') && (c === '*' || !isWordChar(src[i - 1]));
277
414
  if (opens) {
278
- let close = i + mark.length;
279
- for (;;) {
280
- close = src.indexOf(mark, close + 1);
281
- if (close < 0) break;
282
- const before = src[close - 1];
283
- const after = src[close + mark.length];
284
- // `**` must not be read as the end of a `*` run, and `_` closes only at a word's end
285
- if (!strong && src[close + 1] === c) { close++; continue; }
286
- if (/\s/.test(before) || (c === '_' && isWordChar(after))) continue;
287
- break;
288
- }
289
- if (close > i + mark.length) {
415
+ // `**` is not the end of a `*` run, and `_` closes only at a word's end (see closers)
416
+ const close = (marks[c + size] ??= closers(src, c, strong))[i + size + 1];
417
+ if (close >= 0) {
290
418
  const tag = strong ? 'strong' : 'em';
291
- out += `<${tag}>${renderInline(src.slice(i + mark.length, close), ctx)}</${tag}>`;
292
- i = close + mark.length;
419
+ out += `<${tag}>${inline(src.slice(i + size, close), ctx, depth + 1)}</${tag}>`;
420
+ i = close + size;
293
421
  continue;
294
422
  }
295
423
  }
@@ -304,21 +432,108 @@ export function renderInline(src, ctx) {
304
432
  // HTML blocks
305
433
  // ---------------------------------------------------------------------------
306
434
 
435
+ const isSpaceCode = (c, ch) => c === 32 || (c >= 9 && c <= 13) || (c >= 128 && /\s/.test(ch));
436
+
437
+ /**
438
+ * The tags and comments of an HTML block, as `/<(\/?)([a-zA-Z][\w-]*)((?:\s+[^\s=>/]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?)*)\s*(\/?)>|<!--[\s\S]*?-->/g`
439
+ * finds them, but in linear time (that expression starts again at every `<` and reads to the end of the block each
440
+ * time). A tag's attributes are read the one way the expression can read them (it has no other that matches), and
441
+ * where the rest of the attributes lead from a place is worked out once; a comment's end is looked for once.
442
+ * @returns {{ index: number, end: number, closing: string, tag: string, attrs: string, comment: boolean }[]}
443
+ */
444
+ function* htmlTokens(src) {
445
+ const n = src.length;
446
+ let tables = null;
447
+ const build = () => {
448
+ // From each place: the next that is not a space, not a name's character, not an unquoted value's; the next quote
449
+ const skip = new Int32Array(n + 2);
450
+ const name = new Int32Array(n + 2);
451
+ const bare = new Int32Array(n + 2);
452
+ skip[n] = skip[n + 1] = name[n] = name[n + 1] = bare[n] = bare[n + 1] = n;
453
+ for (let x = n - 1; x >= 0; x--) {
454
+ const ch = src[x];
455
+ const c = src.charCodeAt(x);
456
+ const space = isSpaceCode(c, ch);
457
+ skip[x] = space ? skip[x + 1] : x;
458
+ name[x] = space || ch === '=' || ch === '>' || ch === '/' ? x : name[x + 1];
459
+ bare[x] = space || ch === '"' || ch === "'" || ch === '=' || ch === '<' || ch === '>' || ch === '`' ? x : bare[x + 1];
460
+ }
461
+ return { skip, name, bare, memo: new Int32Array(n + 2).fill(-2) };
462
+ };
463
+ // Where the attributes that can be read from q end (the place after the last), or -1 when they lead to no `>`
464
+ const attrsEnd = (q0) => {
465
+ const { skip, name, bare, memo } = (tables ??= build());
466
+ const seen = [];
467
+ let q = q0;
468
+ let result;
469
+ for (;;) {
470
+ if (memo[q] !== -2) { result = memo[q]; break; }
471
+ seen.push(q);
472
+ const w = skip[q];
473
+ if (w > q && w < n && name[w] > w) {
474
+ const e = name[w];
475
+ const z = skip[e];
476
+ if (src[z] !== '=') { q = e; continue; }
477
+ const v = skip[z + 1];
478
+ let vEnd = -1;
479
+ if (src[v] === '"' || src[v] === "'") {
480
+ const close = src.indexOf(src[v], v + 1);
481
+ if (close >= 0) vEnd = close + 1;
482
+ } else if (bare[v] > v) vEnd = bare[v];
483
+ if (vEnd < 0) { result = -1; break; }
484
+ q = vEnd;
485
+ continue;
486
+ }
487
+ // No more attributes: a `>`, or a `/` and then a `>`, after any spaces
488
+ result = src[w] === '>' || (src[w] === '/' && src[w + 1] === '>') ? q : -1;
489
+ break;
490
+ }
491
+ for (const x of seen) tables.memo[x] = result;
492
+ return result;
493
+ };
494
+ const head = /<(\/?)([a-zA-Z][\w-]*)/y;
495
+ let noCommentEnd = false;
496
+ let p = src.indexOf('<');
497
+ while (p >= 0) {
498
+ head.lastIndex = p;
499
+ const m = head.exec(src);
500
+ if (m) {
501
+ const e = p + m[0].length;
502
+ const a = attrsEnd(e);
503
+ if (a >= 0) {
504
+ const w = tables.skip[a];
505
+ const end = (src[w] === '/' ? w + 1 : w) + 1;
506
+ yield { index: p, end, closing: m[1], tag: m[2], attrs: src.slice(e, a), comment: false };
507
+ p = src.indexOf('<', end);
508
+ continue;
509
+ }
510
+ } else if (!noCommentEnd && src.startsWith('<!--', p)) {
511
+ const close = src.indexOf('-->', p + 4);
512
+ if (close < 0) noCommentEnd = true; // no later `<!--` has an end either
513
+ else {
514
+ yield { index: p, end: close + 3, comment: true };
515
+ p = src.indexOf('<', close + 3);
516
+ continue;
517
+ }
518
+ }
519
+ p = src.indexOf('<', p + 1);
520
+ }
521
+ }
522
+
307
523
  /** An HTML block with only the allowed tags and attributes kept; every other tag dropped, the text escaped */
308
524
  function sanitizeHtml(src, ctx) {
309
525
  let out = '';
310
526
  let last = 0;
311
- const tagRe = /<(\/?)([a-zA-Z][\w-]*)((?:\s+[^\s=>/]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?)*)\s*(\/?)>|<!--[\s\S]*?-->/g;
312
- for (const m of src.matchAll(tagRe)) {
527
+ for (const m of htmlTokens(src)) {
313
528
  out += esc(src.slice(last, m.index));
314
- last = m.index + m[0].length;
315
- if (!m[2]) continue; // a comment
316
- const tag = m[2].toLowerCase();
529
+ last = m.end;
530
+ if (m.comment) continue;
531
+ const tag = m.tag.toLowerCase();
317
532
  const allowed = ALLOWED_TAGS[tag];
318
533
  if (!allowed) continue;
319
- if (m[1]) { if (!VOID_TAGS.has(tag)) out += `</${tag}>`; continue; }
534
+ if (m.closing) { if (!VOID_TAGS.has(tag)) out += `</${tag}>`; continue; }
320
535
  let attrs = '';
321
- for (const a of m[3].matchAll(/([^\s=>/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g)) {
536
+ for (const a of m.attrs.matchAll(/([^\s=>/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g)) {
322
537
  const name = a[1].toLowerCase();
323
538
  if (!allowed.includes(name)) continue;
324
539
  let value = a[2] ?? a[3] ?? a[4] ?? '';
@@ -337,7 +552,16 @@ function sanitizeHtml(src, ctx) {
337
552
  // Blocks
338
553
  // ---------------------------------------------------------------------------
339
554
 
340
- const FENCE = /^ {0,3}(`{3,}|~{3,})\s*([^`\s]*)[^`]*$/;
555
+ const FENCE_MARK = /^ {0,3}(`{3,}|~{3,})/;
556
+ /**
557
+ * A fence line's `[line, mark, info word]`, or null: the mark, then no backtick in the rest of the line. (Checked
558
+ * once; a single expression for it, `/^ {0,3}(`{3,}|~{3,})\s*([^`\s]*)[^`]*$/`, reads a long run of spaces again from each one.)
559
+ */
560
+ function fenceOf(line) {
561
+ const m = FENCE_MARK.exec(line);
562
+ if (!m || line.includes('`', m[0].length)) return null;
563
+ return [line, m[1], /^\s*([^`\s]*)/.exec(line.slice(m[0].length))[1]];
564
+ }
341
565
  const HEADING = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/;
342
566
  const RULE = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
343
567
  const QUOTE = /^ {0,3}> ?/;
@@ -368,22 +592,23 @@ function cells(line) {
368
592
  }
369
593
 
370
594
  /** Whether a line starts a block other than a paragraph (it interrupts a paragraph) */
371
- const startsBlock = (line) => FENCE.test(line) || HEADING.test(line) || RULE.test(line) || QUOTE.test(line) || HTML_START.test(line) || (LIST_ITEM.test(line) && !isBlank(LIST_ITEM.exec(line)[4]));
595
+ const startsBlock = (line) => fenceOf(line) || HEADING.test(line) || RULE.test(line) || QUOTE.test(line) || HTML_START.test(line) || (LIST_ITEM.test(line) && !isBlank(LIST_ITEM.exec(line)[4]));
372
596
 
373
597
  /**
374
598
  * Lines of Markdown as HTML.
375
599
  * @param {string[]} lines
376
600
  * @param {{ link: Function, headings: object[], slugs: Map<string, number> }} ctx
377
601
  * @param {boolean} [tight] paragraphs without <p> (a tight list item)
602
+ * @param {number} [depth] how many lists and quotes these lines sit in (from MAX_NEST on, a list or quote is text)
378
603
  */
379
- function renderBlocks(lines, ctx, tight = false) {
604
+ function renderBlocks(lines, ctx, tight = false, depth = 0) {
380
605
  const out = [];
381
606
  let i = 0;
382
607
  while (i < lines.length) {
383
608
  const line = lines[i];
384
609
  if (isBlank(line)) { i++; continue; }
385
610
 
386
- const fence = FENCE.exec(line);
611
+ const fence = fenceOf(line);
387
612
  if (fence) {
388
613
  const [, mark, lang] = fence;
389
614
  const indent = indentOf(line);
@@ -414,10 +639,10 @@ function renderBlocks(lines, ctx, tight = false) {
414
639
 
415
640
  if (RULE.test(line)) { out.push('<hr>'); i++; continue; }
416
641
 
417
- if (QUOTE.test(line)) {
642
+ if (QUOTE.test(line) && depth < MAX_NEST) {
418
643
  const body = [];
419
644
  while (i < lines.length && !isBlank(lines[i]) && (QUOTE.test(lines[i]) || !startsBlock(lines[i]))) body.push(lines[i++].replace(QUOTE, ''));
420
- out.push(`<blockquote>\n${renderBlocks(body, ctx)}\n</blockquote>`);
645
+ out.push(`<blockquote>\n${renderBlocks(body, ctx, false, depth + 1)}\n</blockquote>`);
421
646
  continue;
422
647
  }
423
648
 
@@ -442,7 +667,7 @@ function renderBlocks(lines, ctx, tight = false) {
442
667
  continue;
443
668
  }
444
669
 
445
- const item = LIST_ITEM.exec(line);
670
+ const item = depth < MAX_NEST && LIST_ITEM.exec(line);
446
671
  if (item) {
447
672
  const ordered = /\d/.test(item[2]);
448
673
  const kind = ordered ? item[2].slice(-1) : item[2];
@@ -475,7 +700,7 @@ function renderBlocks(lines, ctx, tight = false) {
475
700
  items.push(body);
476
701
  }
477
702
  const tag = ordered ? 'ol' : 'ul';
478
- const lis = items.map((b) => `<li>${renderBlocks(b, ctx, !loose)}</li>`).join('\n');
703
+ const lis = items.map((b) => `<li>${renderBlocks(b, ctx, !loose, depth + 1)}</li>`).join('\n');
479
704
  out.push(`<${tag}${ordered && start !== 1 ? ` start="${start}"` : ''}>\n${lis}\n</${tag}>`);
480
705
  continue;
481
706
  }