cornfield 0.51.4 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cornfield",
3
- "version": "0.51.4",
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)",
@@ -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 HEADING = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/;
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 LIST_ITEM = /^( {0,3})([-*+]|\d{1,9}[.)])([ \t]+|$)(.*)$/;
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 TABLE_SEP = /^ {0,3}\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
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) => 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]));
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> }} ctx
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 = HEADING.exec(line);
692
+ const heading = headingOf(line);
627
693
  if (heading) {
628
- const level = heading[1].length;
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 && TABLE_SEP.test(lines[i + 1]) && lines[i + 1].includes('-')) {
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
- rows.push(`<tr>${head.map((_, k) => cell('td', row[k] ?? '', k)).join('')}</tr>`);
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 && LIST_ITEM.exec(line);
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 = LIST_ITEM.exec(lines[i]);
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 && body.filter((b) => b !== '').length) loose = loose || !LIST_ITEM.test(l.slice(contentIndent));
689
- body.push(l.replace(/\t/g, ' ').slice(contentIndent));
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) && !LIST_ITEM.test(l)) { body.push(l.trim()); i++; continue; }
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 && LIST_ITEM.test(lines[i])) loose = true;
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 };