text-doc-ir 0.0.11 → 0.0.12

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/esm/deps.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { NodeVisitor } from "document-ir";
2
- export type { BlockQuoteNode, BreakNode, BubbleNode, CardNode, CenterNode, ColumnsNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureImageNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, StickerNode, StrikeThroughNode, TextNode, VideoNode, WarningNode, } from "document-ir";
2
+ export type { BlockQuoteNode, BreakNode, BubbleNode, CardNode, CenterNode, ColumnsNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureImageNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, StickerNode, StrikeThroughNode, TableOfContentsNode, TextNode, VideoNode, WarningNode, } from "document-ir";
package/esm/main.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { BlockQuoteNode, BreakNode, BubbleNode, CardNode, CenterNode, ColumnsNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureImageNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, LinkNode, ListNode, NodeVisitor, NoteNode, ParagraphNode, QuoteNode, StickerNode, StrikeThroughNode, TextNode, VideoNode, WarningNode } from "./deps.js";
1
+ import { BlockQuoteNode, BreakNode, BubbleNode, CardNode, CenterNode, ColumnsNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureImageNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, LinkNode, ListNode, NodeVisitor, NoteNode, ParagraphNode, QuoteNode, StickerNode, StrikeThroughNode, TableOfContentsNode, TextNode, VideoNode, WarningNode } from "./deps.js";
2
2
  export declare class FixedWidthTextVisitor extends NodeVisitor {
3
3
  private width;
4
4
  private lines;
@@ -10,6 +10,7 @@ export declare class FixedWidthTextVisitor extends NodeVisitor {
10
10
  private state;
11
11
  constructor(width: number);
12
12
  private setState;
13
+ private restoreState;
13
14
  protected text(node: TextNode): void;
14
15
  protected formattedText(node: FormattedTextNode): void;
15
16
  protected horizontalRule(node: HorizontalRuleNode): void;
@@ -36,6 +37,7 @@ export declare class FixedWidthTextVisitor extends NodeVisitor {
36
37
  protected card(node: CardNode): void;
37
38
  protected definitionReference(node: DefinitionReferenceNode): void;
38
39
  protected definition(node: DefinitionNode): void;
40
+ protected toc(node: TableOfContentsNode): void;
39
41
  protected document(node: DocumentNode): void;
40
42
  private counterToDepth;
41
43
  private pushBlockContentBegin;
package/esm/main.js CHANGED
@@ -66,11 +66,16 @@ export class FixedWidthTextVisitor extends NodeVisitor {
66
66
  linksReverse: new Map(),
67
67
  linkCount: 0,
68
68
  numericDepth: 0,
69
+ tocDepth: 0,
69
70
  };
70
71
  }
71
72
  setState(state) {
72
73
  this.state = state;
73
74
  }
75
+ restoreState(parent) {
76
+ parent.state.imageCount = this.state.imageCount;
77
+ parent.state.linkCount = this.state.linkCount;
78
+ }
74
79
  text(node) {
75
80
  this.pushText(node.text);
76
81
  }
@@ -133,6 +138,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
133
138
  }
134
139
  }
135
140
  }
141
+ visitor.restoreState(this);
136
142
  }
137
143
  }
138
144
  else {
@@ -149,6 +155,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
149
155
  this.lines.push(" " + lines[i]);
150
156
  }
151
157
  }
158
+ visitor.restoreState(this);
152
159
  }
153
160
  }
154
161
  this.pushBlockContentEnd();
@@ -175,6 +182,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
175
182
  columns.push(lines);
176
183
  maxLines = Math.max(maxLines, lines.length);
177
184
  consumedWidth += width;
185
+ visitor.restoreState(this);
178
186
  }
179
187
  this.pushBlockContentBegin();
180
188
  for (let i = 0; i < maxLines; i++) {
@@ -338,6 +346,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
338
346
  this.lines[Math.max(0, this.lines.length - 1)] =
339
347
  " ".repeat(Math.floor((this.width - line.length) / 2)) + line;
340
348
  }
349
+ visitor.restoreState(this);
341
350
  this.pushBlockContentEnd();
342
351
  }
343
352
  warning(node) {
@@ -352,6 +361,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
352
361
  this.pushEndOfLineIfAnyContent();
353
362
  this.lines[Math.max(0, this.lines.length - 1)] = "| " + line;
354
363
  }
364
+ visitor.restoreState(this);
355
365
  this.pushBlockContentEnd();
356
366
  }
357
367
  highTechAlert(node) {
@@ -376,6 +386,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
376
386
  this.pushLine();
377
387
  this.pushText("|" + "-".repeat(this.width - 2) + "|");
378
388
  }
389
+ centerVisitor.restoreState(this);
379
390
  const contentVisitor = new FixedWidthTextVisitor(this.width - 4);
380
391
  contentVisitor.setState({ ...this.state, numericDepth: 0 });
381
392
  contentVisitor.visit({
@@ -391,6 +402,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
391
402
  this.pushText("\\");
392
403
  this.pushText("-".repeat(this.width - 2));
393
404
  this.pushText("/");
405
+ contentVisitor.restoreState(this);
394
406
  this.pushBlockContentEnd();
395
407
  }
396
408
  blockQuote(node) {
@@ -405,6 +417,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
405
417
  this.pushEndOfLineIfAnyContent();
406
418
  this.lines[Math.max(0, this.lines.length - 1)] = "> " + line;
407
419
  }
420
+ visitor.restoreState(this);
408
421
  this.pushBlockContentEnd();
409
422
  }
410
423
  quote(node) {
@@ -424,6 +437,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
424
437
  this.pushEndOfLineIfAnyContent();
425
438
  this.lines[Math.max(0, this.lines.length - 1)] = "> " + line;
426
439
  }
440
+ visitor.restoreState(this);
427
441
  this.pushBlockContentEnd();
428
442
  }
429
443
  bubble(node) {
@@ -446,6 +460,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
446
460
  " ".repeat(this.width - 2 - line.length) + line + " |";
447
461
  }
448
462
  }
463
+ visitor.restoreState(this);
449
464
  this.pushBlockContentEnd();
450
465
  }
451
466
  sticker(node) {
@@ -497,6 +512,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
497
512
  this.pushText("\\");
498
513
  this.pushText("-".repeat(this.width - 2));
499
514
  this.pushText("/");
515
+ visitor.restoreState(this);
500
516
  this.pushBlockContentEnd();
501
517
  }
502
518
  card(node) {
@@ -534,6 +550,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
534
550
  this.pushLine();
535
551
  this.pushText("|" + "-".repeat(this.width - 2) + "|");
536
552
  }
553
+ centerVisitor.restoreState(this);
537
554
  }
538
555
  const contentVisitor = new FixedWidthTextVisitor(this.width - 4);
539
556
  contentVisitor.setState({ ...this.state, numericDepth: 0 });
@@ -585,6 +602,7 @@ export class FixedWidthTextVisitor extends NodeVisitor {
585
602
  this.pushText("\\");
586
603
  this.pushText("-".repeat(this.width - 2));
587
604
  this.pushText("/");
605
+ contentVisitor.restoreState(this);
588
606
  this.pushBlockContentEnd();
589
607
  }
590
608
  definitionReference(node) {
@@ -612,8 +630,89 @@ export class FixedWidthTextVisitor extends NodeVisitor {
612
630
  this.pushEndOfLineIfAnyContent();
613
631
  this.lines[Math.max(0, this.lines.length - 1)] = " " + line;
614
632
  }
633
+ visitor.restoreState(this);
615
634
  this.pushBlockContentEnd();
616
635
  }
636
+ toc(node) {
637
+ if (this.state.tocDepth > 0) {
638
+ const visitor = new FixedWidthTextVisitor(this.width - 3);
639
+ visitor.setState({ ...this.state, tocDepth: this.state.tocDepth + 1 });
640
+ if (node.date) {
641
+ visitor.choose(node.date);
642
+ visitor.pushText(" ");
643
+ }
644
+ visitor.chooseChildren(node.content);
645
+ if (node.href && !node.href.startsWith("#")) {
646
+ visitor.link({
647
+ type: "link",
648
+ url: node.href,
649
+ content: [],
650
+ });
651
+ visitor.spaceLazy = true;
652
+ }
653
+ let counter = 0;
654
+ for (const line of visitor.getLines()) {
655
+ this.pushEndOfLineIfAnyContent();
656
+ // First line gets a bullet point
657
+ if (counter == 0) {
658
+ this.lines[Math.max(0, this.lines.length - 1)] = "* " + line;
659
+ }
660
+ else {
661
+ this.lines[Math.max(0, this.lines.length - 1)] = " " + line;
662
+ }
663
+ counter++;
664
+ }
665
+ visitor.restoreState(this);
666
+ visitor.visit({
667
+ type: "array",
668
+ content: node.children,
669
+ });
670
+ for (const line of visitor.getLines()) {
671
+ this.pushEndOfLineIfAnyContent();
672
+ this.lines[Math.max(0, this.lines.length - 1)] = " " + line;
673
+ }
674
+ visitor.restoreState(this);
675
+ }
676
+ else {
677
+ const visitor = new FixedWidthTextVisitor(this.width - 4);
678
+ visitor.setState({ ...this.state, tocDepth: this.state.tocDepth + 1 });
679
+ if (node.date) {
680
+ visitor.choose(node.date);
681
+ visitor.pushText(" ");
682
+ }
683
+ visitor.chooseChildren(node.content);
684
+ if (node.href && !node.href.startsWith("#")) {
685
+ visitor.link({
686
+ type: "link",
687
+ url: node.href,
688
+ content: [],
689
+ });
690
+ visitor.spaceLazy = true;
691
+ }
692
+ visitor.pushLine();
693
+ visitor.visit({
694
+ type: "array",
695
+ content: node.children,
696
+ });
697
+ const tocLen = " Table of contents ".length;
698
+ const lines = visitor.getLines();
699
+ const maxLength = lines.reduce((prev, next) => Math.max(prev, next.length + 1), tocLen + 2);
700
+ const left = Math.floor((maxLength - tocLen) / 2) + 1;
701
+ const right = maxLength - tocLen - left + 1;
702
+ this.pushBlockContentBegin();
703
+ this.lines[Math.max(0, this.lines.length - 1)] = "/" + "-".repeat(left) +
704
+ " Table of contents " + "-".repeat(right) + "\\";
705
+ for (const line of visitor.getLines()) {
706
+ this.pushEndOfLineIfAnyContent();
707
+ this.lines[Math.max(0, this.lines.length - 1)] = "| " + line +
708
+ " ".repeat(maxLength - line.length) + "|";
709
+ }
710
+ this.lines[Math.max(0, this.lines.length - 1)] = "\\" +
711
+ "-".repeat(maxLength + 1) + "/";
712
+ visitor.restoreState(this);
713
+ this.pushBlockContentEnd();
714
+ }
715
+ }
617
716
  document(node) {
618
717
  this.pushBlockContentBegin();
619
718
  let date;
@@ -642,8 +741,9 @@ export class FixedWidthTextVisitor extends NodeVisitor {
642
741
  super.document(node);
643
742
  this.horizontalRule({ type: "horizontal-rule" });
644
743
  if (this.state.linkCount > 0) {
645
- const length = Math.max(7, `[L${this.state.linkCount}]: `.length);
646
- for (let i = 1; i <= this.state.linkCount; i++) {
744
+ const max = this.state.linkCount;
745
+ const length = Math.max(7, `[L${max}]: `.length);
746
+ for (let i = 1; i <= max; i++) {
647
747
  const label = `[L${i}]: `;
648
748
  const visitor = new FixedWidthTextVisitor(this.width - length);
649
749
  visitor.text({
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/main.js",
3
3
  "main": "./script/main.js",
4
4
  "name": "text-doc-ir",
5
- "version": "0.0.11",
5
+ "version": "0.0.12",
6
6
  "description": "Transforms a document intermediate representation into plain text with formatting",
7
7
  "license": "MIT",
8
8
  "repository": {
@@ -19,7 +19,7 @@
19
19
  }
20
20
  },
21
21
  "dependencies": {
22
- "document-ir": "0.0.14"
22
+ "document-ir": "0.0.15"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/node": "^18.11.9"
package/script/deps.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { NodeVisitor } from "document-ir";
2
- export type { BlockQuoteNode, BreakNode, BubbleNode, CardNode, CenterNode, ColumnsNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureImageNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, StickerNode, StrikeThroughNode, TextNode, VideoNode, WarningNode, } from "document-ir";
2
+ export type { BlockQuoteNode, BreakNode, BubbleNode, CardNode, CenterNode, ColumnsNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureImageNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, StickerNode, StrikeThroughNode, TableOfContentsNode, TextNode, VideoNode, WarningNode, } from "document-ir";
package/script/main.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { BlockQuoteNode, BreakNode, BubbleNode, CardNode, CenterNode, ColumnsNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureImageNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, LinkNode, ListNode, NodeVisitor, NoteNode, ParagraphNode, QuoteNode, StickerNode, StrikeThroughNode, TextNode, VideoNode, WarningNode } from "./deps.js";
1
+ import { BlockQuoteNode, BreakNode, BubbleNode, CardNode, CenterNode, ColumnsNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureImageNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, LinkNode, ListNode, NodeVisitor, NoteNode, ParagraphNode, QuoteNode, StickerNode, StrikeThroughNode, TableOfContentsNode, TextNode, VideoNode, WarningNode } from "./deps.js";
2
2
  export declare class FixedWidthTextVisitor extends NodeVisitor {
3
3
  private width;
4
4
  private lines;
@@ -10,6 +10,7 @@ export declare class FixedWidthTextVisitor extends NodeVisitor {
10
10
  private state;
11
11
  constructor(width: number);
12
12
  private setState;
13
+ private restoreState;
13
14
  protected text(node: TextNode): void;
14
15
  protected formattedText(node: FormattedTextNode): void;
15
16
  protected horizontalRule(node: HorizontalRuleNode): void;
@@ -36,6 +37,7 @@ export declare class FixedWidthTextVisitor extends NodeVisitor {
36
37
  protected card(node: CardNode): void;
37
38
  protected definitionReference(node: DefinitionReferenceNode): void;
38
39
  protected definition(node: DefinitionNode): void;
40
+ protected toc(node: TableOfContentsNode): void;
39
41
  protected document(node: DocumentNode): void;
40
42
  private counterToDepth;
41
43
  private pushBlockContentBegin;
package/script/main.js CHANGED
@@ -69,11 +69,16 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
69
69
  linksReverse: new Map(),
70
70
  linkCount: 0,
71
71
  numericDepth: 0,
72
+ tocDepth: 0,
72
73
  };
73
74
  }
74
75
  setState(state) {
75
76
  this.state = state;
76
77
  }
78
+ restoreState(parent) {
79
+ parent.state.imageCount = this.state.imageCount;
80
+ parent.state.linkCount = this.state.linkCount;
81
+ }
77
82
  text(node) {
78
83
  this.pushText(node.text);
79
84
  }
@@ -136,6 +141,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
136
141
  }
137
142
  }
138
143
  }
144
+ visitor.restoreState(this);
139
145
  }
140
146
  }
141
147
  else {
@@ -152,6 +158,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
152
158
  this.lines.push(" " + lines[i]);
153
159
  }
154
160
  }
161
+ visitor.restoreState(this);
155
162
  }
156
163
  }
157
164
  this.pushBlockContentEnd();
@@ -178,6 +185,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
178
185
  columns.push(lines);
179
186
  maxLines = Math.max(maxLines, lines.length);
180
187
  consumedWidth += width;
188
+ visitor.restoreState(this);
181
189
  }
182
190
  this.pushBlockContentBegin();
183
191
  for (let i = 0; i < maxLines; i++) {
@@ -341,6 +349,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
341
349
  this.lines[Math.max(0, this.lines.length - 1)] =
342
350
  " ".repeat(Math.floor((this.width - line.length) / 2)) + line;
343
351
  }
352
+ visitor.restoreState(this);
344
353
  this.pushBlockContentEnd();
345
354
  }
346
355
  warning(node) {
@@ -355,6 +364,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
355
364
  this.pushEndOfLineIfAnyContent();
356
365
  this.lines[Math.max(0, this.lines.length - 1)] = "| " + line;
357
366
  }
367
+ visitor.restoreState(this);
358
368
  this.pushBlockContentEnd();
359
369
  }
360
370
  highTechAlert(node) {
@@ -379,6 +389,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
379
389
  this.pushLine();
380
390
  this.pushText("|" + "-".repeat(this.width - 2) + "|");
381
391
  }
392
+ centerVisitor.restoreState(this);
382
393
  const contentVisitor = new FixedWidthTextVisitor(this.width - 4);
383
394
  contentVisitor.setState({ ...this.state, numericDepth: 0 });
384
395
  contentVisitor.visit({
@@ -394,6 +405,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
394
405
  this.pushText("\\");
395
406
  this.pushText("-".repeat(this.width - 2));
396
407
  this.pushText("/");
408
+ contentVisitor.restoreState(this);
397
409
  this.pushBlockContentEnd();
398
410
  }
399
411
  blockQuote(node) {
@@ -408,6 +420,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
408
420
  this.pushEndOfLineIfAnyContent();
409
421
  this.lines[Math.max(0, this.lines.length - 1)] = "> " + line;
410
422
  }
423
+ visitor.restoreState(this);
411
424
  this.pushBlockContentEnd();
412
425
  }
413
426
  quote(node) {
@@ -427,6 +440,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
427
440
  this.pushEndOfLineIfAnyContent();
428
441
  this.lines[Math.max(0, this.lines.length - 1)] = "> " + line;
429
442
  }
443
+ visitor.restoreState(this);
430
444
  this.pushBlockContentEnd();
431
445
  }
432
446
  bubble(node) {
@@ -449,6 +463,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
449
463
  " ".repeat(this.width - 2 - line.length) + line + " |";
450
464
  }
451
465
  }
466
+ visitor.restoreState(this);
452
467
  this.pushBlockContentEnd();
453
468
  }
454
469
  sticker(node) {
@@ -500,6 +515,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
500
515
  this.pushText("\\");
501
516
  this.pushText("-".repeat(this.width - 2));
502
517
  this.pushText("/");
518
+ visitor.restoreState(this);
503
519
  this.pushBlockContentEnd();
504
520
  }
505
521
  card(node) {
@@ -537,6 +553,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
537
553
  this.pushLine();
538
554
  this.pushText("|" + "-".repeat(this.width - 2) + "|");
539
555
  }
556
+ centerVisitor.restoreState(this);
540
557
  }
541
558
  const contentVisitor = new FixedWidthTextVisitor(this.width - 4);
542
559
  contentVisitor.setState({ ...this.state, numericDepth: 0 });
@@ -588,6 +605,7 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
588
605
  this.pushText("\\");
589
606
  this.pushText("-".repeat(this.width - 2));
590
607
  this.pushText("/");
608
+ contentVisitor.restoreState(this);
591
609
  this.pushBlockContentEnd();
592
610
  }
593
611
  definitionReference(node) {
@@ -615,8 +633,89 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
615
633
  this.pushEndOfLineIfAnyContent();
616
634
  this.lines[Math.max(0, this.lines.length - 1)] = " " + line;
617
635
  }
636
+ visitor.restoreState(this);
618
637
  this.pushBlockContentEnd();
619
638
  }
639
+ toc(node) {
640
+ if (this.state.tocDepth > 0) {
641
+ const visitor = new FixedWidthTextVisitor(this.width - 3);
642
+ visitor.setState({ ...this.state, tocDepth: this.state.tocDepth + 1 });
643
+ if (node.date) {
644
+ visitor.choose(node.date);
645
+ visitor.pushText(" ");
646
+ }
647
+ visitor.chooseChildren(node.content);
648
+ if (node.href && !node.href.startsWith("#")) {
649
+ visitor.link({
650
+ type: "link",
651
+ url: node.href,
652
+ content: [],
653
+ });
654
+ visitor.spaceLazy = true;
655
+ }
656
+ let counter = 0;
657
+ for (const line of visitor.getLines()) {
658
+ this.pushEndOfLineIfAnyContent();
659
+ // First line gets a bullet point
660
+ if (counter == 0) {
661
+ this.lines[Math.max(0, this.lines.length - 1)] = "* " + line;
662
+ }
663
+ else {
664
+ this.lines[Math.max(0, this.lines.length - 1)] = " " + line;
665
+ }
666
+ counter++;
667
+ }
668
+ visitor.restoreState(this);
669
+ visitor.visit({
670
+ type: "array",
671
+ content: node.children,
672
+ });
673
+ for (const line of visitor.getLines()) {
674
+ this.pushEndOfLineIfAnyContent();
675
+ this.lines[Math.max(0, this.lines.length - 1)] = " " + line;
676
+ }
677
+ visitor.restoreState(this);
678
+ }
679
+ else {
680
+ const visitor = new FixedWidthTextVisitor(this.width - 4);
681
+ visitor.setState({ ...this.state, tocDepth: this.state.tocDepth + 1 });
682
+ if (node.date) {
683
+ visitor.choose(node.date);
684
+ visitor.pushText(" ");
685
+ }
686
+ visitor.chooseChildren(node.content);
687
+ if (node.href && !node.href.startsWith("#")) {
688
+ visitor.link({
689
+ type: "link",
690
+ url: node.href,
691
+ content: [],
692
+ });
693
+ visitor.spaceLazy = true;
694
+ }
695
+ visitor.pushLine();
696
+ visitor.visit({
697
+ type: "array",
698
+ content: node.children,
699
+ });
700
+ const tocLen = " Table of contents ".length;
701
+ const lines = visitor.getLines();
702
+ const maxLength = lines.reduce((prev, next) => Math.max(prev, next.length + 1), tocLen + 2);
703
+ const left = Math.floor((maxLength - tocLen) / 2) + 1;
704
+ const right = maxLength - tocLen - left + 1;
705
+ this.pushBlockContentBegin();
706
+ this.lines[Math.max(0, this.lines.length - 1)] = "/" + "-".repeat(left) +
707
+ " Table of contents " + "-".repeat(right) + "\\";
708
+ for (const line of visitor.getLines()) {
709
+ this.pushEndOfLineIfAnyContent();
710
+ this.lines[Math.max(0, this.lines.length - 1)] = "| " + line +
711
+ " ".repeat(maxLength - line.length) + "|";
712
+ }
713
+ this.lines[Math.max(0, this.lines.length - 1)] = "\\" +
714
+ "-".repeat(maxLength + 1) + "/";
715
+ visitor.restoreState(this);
716
+ this.pushBlockContentEnd();
717
+ }
718
+ }
620
719
  document(node) {
621
720
  this.pushBlockContentBegin();
622
721
  let date;
@@ -645,8 +744,9 @@ class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
645
744
  super.document(node);
646
745
  this.horizontalRule({ type: "horizontal-rule" });
647
746
  if (this.state.linkCount > 0) {
648
- const length = Math.max(7, `[L${this.state.linkCount}]: `.length);
649
- for (let i = 1; i <= this.state.linkCount; i++) {
747
+ const max = this.state.linkCount;
748
+ const length = Math.max(7, `[L${max}]: `.length);
749
+ for (let i = 1; i <= max; i++) {
650
750
  const label = `[L${i}]: `;
651
751
  const visitor = new FixedWidthTextVisitor(this.width - length);
652
752
  visitor.text({