@tooee/renderers 0.12.0 → 0.14.0

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.
Files changed (124) hide show
  1. package/dist/chooser/choose-filter.d.ts.map +1 -1
  2. package/dist/chooser/choose-filter.js.map +1 -1
  3. package/dist/chooser/choose-highlighted-text.d.ts.map +1 -1
  4. package/dist/chooser/choose-highlighted-text.js.map +1 -1
  5. package/dist/chooser/choose-list.d.ts.map +1 -1
  6. package/dist/chooser/choose-list.js.map +1 -1
  7. package/dist/chooser/choose-store.d.ts +3 -3
  8. package/dist/chooser/choose-store.d.ts.map +1 -1
  9. package/dist/chooser/choose-store.js.map +1 -1
  10. package/dist/chooser/chooser.d.ts.map +1 -1
  11. package/dist/chooser/chooser.js.map +1 -1
  12. package/dist/chooser/source.d.ts.map +1 -1
  13. package/dist/chooser/source.js.map +1 -1
  14. package/dist/chooser/use-choose.d.ts.map +1 -1
  15. package/dist/chooser/use-choose.js.map +1 -1
  16. package/dist/code-blocks.d.ts.map +1 -1
  17. package/dist/code-blocks.js.map +1 -1
  18. package/dist/command-palette.d.ts.map +1 -1
  19. package/dist/command-palette.js.map +1 -1
  20. package/dist/context-menu.d.ts.map +1 -1
  21. package/dist/context-menu.js.map +1 -1
  22. package/dist/index.d.ts +1 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/markdown/blocks/blockquote.d.ts.map +1 -1
  26. package/dist/markdown/blocks/blockquote.js.map +1 -1
  27. package/dist/markdown/blocks/flat-block.d.ts.map +1 -1
  28. package/dist/markdown/blocks/flat-block.js +38 -33
  29. package/dist/markdown/blocks/flat-block.js.map +1 -1
  30. package/dist/markdown/blocks/heading.d.ts.map +1 -1
  31. package/dist/markdown/blocks/heading.js.map +1 -1
  32. package/dist/markdown/blocks/list-line.d.ts +2 -2
  33. package/dist/markdown/blocks/list-line.d.ts.map +1 -1
  34. package/dist/markdown/blocks/list-line.js +2 -3
  35. package/dist/markdown/blocks/list-line.js.map +1 -1
  36. package/dist/markdown/blocks/paragraph.d.ts.map +1 -1
  37. package/dist/markdown/blocks/paragraph.js.map +1 -1
  38. package/dist/markdown/blocks/table.d.ts +17 -3
  39. package/dist/markdown/blocks/table.d.ts.map +1 -1
  40. package/dist/markdown/blocks/table.js +29 -29
  41. package/dist/markdown/blocks/table.js.map +1 -1
  42. package/dist/markdown/links.d.ts +10 -0
  43. package/dist/markdown/links.d.ts.map +1 -1
  44. package/dist/markdown/links.js +30 -18
  45. package/dist/markdown/links.js.map +1 -1
  46. package/dist/markdown/plain-text.d.ts.map +1 -1
  47. package/dist/markdown/plain-text.js.map +1 -1
  48. package/dist/markdown/table-layout.d.ts +28 -0
  49. package/dist/markdown/table-layout.d.ts.map +1 -0
  50. package/dist/markdown/table-layout.js +100 -0
  51. package/dist/markdown/table-layout.js.map +1 -0
  52. package/dist/markdown/table-links.d.ts +5 -4
  53. package/dist/markdown/table-links.d.ts.map +1 -1
  54. package/dist/markdown/table-links.js +6 -23
  55. package/dist/markdown/table-links.js.map +1 -1
  56. package/dist/markdown/tokens.d.ts +2 -0
  57. package/dist/markdown/tokens.d.ts.map +1 -1
  58. package/dist/markdown/tokens.js.map +1 -1
  59. package/dist/markdown/visit-inline.d.ts.map +1 -1
  60. package/dist/markdown/visit-inline.js.map +1 -1
  61. package/dist/markdown-blocks.d.ts +72 -14
  62. package/dist/markdown-blocks.d.ts.map +1 -1
  63. package/dist/markdown-blocks.js +137 -77
  64. package/dist/markdown-blocks.js.map +1 -1
  65. package/dist/markdown-images.d.ts.map +1 -1
  66. package/dist/markdown-images.js +5 -8
  67. package/dist/markdown-images.js.map +1 -1
  68. package/dist/markdown-view.d.ts +0 -1
  69. package/dist/markdown-view.d.ts.map +1 -1
  70. package/dist/markdown-view.js +0 -1
  71. package/dist/markdown-view.js.map +1 -1
  72. package/dist/mermaid.d.ts.map +1 -1
  73. package/dist/mermaid.js +8 -4
  74. package/dist/mermaid.js.map +1 -1
  75. package/dist/parsers.d.ts.map +1 -1
  76. package/dist/parsers.js.map +1 -1
  77. package/dist/row-document-renderable.d.ts.map +1 -1
  78. package/dist/row-document-renderable.js.map +1 -1
  79. package/dist/source.d.ts.map +1 -1
  80. package/dist/source.js.map +1 -1
  81. package/dist/table.d.ts.map +1 -1
  82. package/dist/table.js.map +1 -1
  83. package/package.json +6 -6
  84. package/src/chooser/choose-filter.tsx +1 -0
  85. package/src/chooser/choose-highlighted-text.tsx +2 -0
  86. package/src/chooser/choose-list.tsx +6 -0
  87. package/src/chooser/choose-store.ts +20 -3
  88. package/src/chooser/chooser.tsx +2 -0
  89. package/src/chooser/source.ts +5 -0
  90. package/src/chooser/use-choose.ts +44 -0
  91. package/src/code-blocks.tsx +14 -0
  92. package/src/command-palette.tsx +4 -0
  93. package/src/context-menu.tsx +13 -0
  94. package/src/index.ts +56 -1
  95. package/src/markdown/blocks/blockquote.tsx +2 -0
  96. package/src/markdown/blocks/flat-block.tsx +113 -80
  97. package/src/markdown/blocks/heading.tsx +1 -0
  98. package/src/markdown/blocks/list-line.tsx +6 -6
  99. package/src/markdown/blocks/paragraph.tsx +1 -0
  100. package/src/markdown/blocks/table.tsx +84 -49
  101. package/src/markdown/links.ts +69 -25
  102. package/src/markdown/plain-text.ts +5 -0
  103. package/src/markdown/table-layout.ts +156 -0
  104. package/src/markdown/table-links.ts +17 -25
  105. package/src/markdown/tokens.ts +4 -0
  106. package/src/markdown/visit-inline.ts +20 -0
  107. package/src/markdown-blocks.ts +298 -100
  108. package/src/markdown-images.ts +30 -9
  109. package/src/markdown-view.tsx +4 -1
  110. package/src/mermaid.ts +21 -4
  111. package/src/parsers.ts +39 -0
  112. package/src/row-document-renderable.ts +53 -0
  113. package/src/source.ts +18 -0
  114. package/src/table.tsx +32 -0
  115. package/dist/text-table-internals.d.ts +0 -34
  116. package/dist/text-table-internals.d.ts.map +0 -1
  117. package/dist/text-table-internals.js +0 -68
  118. package/dist/text-table-internals.js.map +0 -1
  119. package/dist/text-table.d.ts +0 -7
  120. package/dist/text-table.d.ts.map +0 -1
  121. package/dist/text-table.js +0 -4
  122. package/dist/text-table.js.map +0 -1
  123. package/src/text-table-internals.ts +0 -124
  124. package/src/text-table.ts +0 -10
@@ -0,0 +1,156 @@
1
+ import { bold as boldChunk } from "@opentui/core";
2
+ import type { TextChunk } from "@opentui/core";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import type { Tokens } from "marked";
5
+
6
+ import { inlineTokensToChunks } from "./chunks.js";
7
+ import { getPlainText } from "./plain-text.js";
8
+
9
+ /** Border columns around a table row: one before each cell and one after the last. */
10
+ export const tableBorderColumns = function tableBorderColumns(columnCount: number): number {
11
+ return columnCount + 1;
12
+ };
13
+
14
+ /**
15
+ * Styled chunks for one table cell. Header cells are bold. A cell with no
16
+ * inline chunks falls back to its plain text.
17
+ */
18
+ export const tableCellChunks = function tableCellChunks(
19
+ cell: Tokens.TableCell,
20
+ theme: ResolvedTheme,
21
+ header: boolean
22
+ ): TextChunk[] {
23
+ const chunks = inlineTokensToChunks(cell.tokens, theme);
24
+
25
+ if (header) {
26
+ return chunks.length > 0
27
+ ? chunks.map((item) => boldChunk(item))
28
+ : [boldChunk(getPlainText(cell.tokens).trim())];
29
+ }
30
+
31
+ return chunks.length > 0 ? chunks : [{ __isChunk: true, text: getPlainText(cell.tokens) }];
32
+ };
33
+
34
+ /** Cells of row `index` of a table. Row `0` is the header. */
35
+ export const tableRowCells = function tableRowCells(
36
+ table: Tokens.Table,
37
+ index: number
38
+ ): readonly Tokens.TableCell[] {
39
+ return index === 0 ? table.header : (table.rows[index - 1] ?? []);
40
+ };
41
+
42
+ /** Display width of the widest line of a cell's chunks. Never less than 1. */
43
+ export const chunksDisplayWidth = function chunksDisplayWidth(
44
+ chunks: readonly TextChunk[]
45
+ ): number {
46
+ const lines = chunks
47
+ .map((item) => item.text)
48
+ .join("")
49
+ .split("\n");
50
+
51
+ return Math.max(1, ...lines.map((line) => Bun.stringWidth(line)));
52
+ };
53
+
54
+ /**
55
+ * Fit natural column widths into `available` cells. When they do not fit,
56
+ * narrow columns keep their width and the widest columns share what is left,
57
+ * so the widest ones wrap first. Every column keeps at least one cell.
58
+ */
59
+ export const fitTableColumnWidths = function fitTableColumnWidths(
60
+ natural: readonly number[],
61
+ available: number
62
+ ): number[] {
63
+ const total = natural.reduce((sum, width) => sum + width, 0);
64
+
65
+ if (total <= available) {
66
+ return [...natural];
67
+ }
68
+
69
+ const budget = Math.max(available, natural.length);
70
+ const ascending = natural.toSorted((a, b) => a - b);
71
+ let remaining = budget;
72
+ let cap = 1;
73
+
74
+ for (const [position, width] of ascending.entries()) {
75
+ const share = Math.floor(remaining / (ascending.length - position));
76
+
77
+ if (width > share) {
78
+ cap = Math.max(1, share);
79
+ break;
80
+ }
81
+
82
+ remaining -= width;
83
+ }
84
+
85
+ const widths = natural.map((width) => Math.min(width, cap));
86
+ let spare = budget - widths.reduce((sum, width) => sum + width, 0);
87
+
88
+ for (const [column, width] of natural.entries()) {
89
+ if (spare <= 0) {
90
+ break;
91
+ }
92
+
93
+ if (width > cap) {
94
+ widths[column] = cap + 1;
95
+ spare -= 1;
96
+ }
97
+ }
98
+
99
+ return widths;
100
+ };
101
+
102
+ interface CachedWidths {
103
+ available: number;
104
+ widths: number[];
105
+ }
106
+
107
+ /**
108
+ * Column widths per table token. Every row of a table renders on its own, so
109
+ * the cache makes all rows share one result and measures each cell once.
110
+ */
111
+ const widthCache = new WeakMap<Tokens.Table, CachedWidths>();
112
+
113
+ /**
114
+ * Column widths, without borders, for every row of `table`. The widths depend
115
+ * only on the cell text and `available`, so theme changes reuse the cache.
116
+ */
117
+ export const tableColumnWidths = function tableColumnWidths(
118
+ table: Tokens.Table,
119
+ available: number,
120
+ theme: ResolvedTheme
121
+ ): number[] {
122
+ const cached = widthCache.get(table);
123
+
124
+ if (cached?.available === available) {
125
+ return cached.widths;
126
+ }
127
+
128
+ const natural = table.header.map((_cell, column) => {
129
+ let widest = 1;
130
+
131
+ for (let index = 0; index <= table.rows.length; index += 1) {
132
+ const cell = tableRowCells(table, index)[column];
133
+
134
+ if (cell !== undefined) {
135
+ widest = Math.max(widest, chunksDisplayWidth(tableCellChunks(cell, theme, index === 0)));
136
+ }
137
+ }
138
+
139
+ return widest;
140
+ });
141
+
142
+ const widths = fitTableColumnWidths(natural, available);
143
+ widthCache.set(table, { available, widths });
144
+
145
+ return widths;
146
+ };
147
+
148
+ /** A horizontal border line such as `┌──┬──┐` for the given column widths. */
149
+ export const tableBorderLine = function tableBorderLine(
150
+ widths: readonly number[],
151
+ left: string,
152
+ middle: string,
153
+ right: string
154
+ ): string {
155
+ return left + widths.map((width) => "─".repeat(width)).join(middle) + right;
156
+ };
@@ -1,6 +1,6 @@
1
- import type { MouseEvent, TextChunk, TextTableContent } from "@opentui/core";
1
+ import type { MouseEvent, TextChunk } from "@opentui/core";
2
2
 
3
- import { textTableCellAt } from "../text-table-internals.js";
3
+ import { positionLinkMouseHandler } from "./links.js";
4
4
  import type { MarkdownLinkHandler } from "./links.js";
5
5
 
6
6
  /** The OSC 8 link URL of the chunk at a text position in one table cell. */
@@ -11,48 +11,40 @@ export const chunkLinkAt = function chunkLinkAt(
11
11
  ): string | null {
12
12
  let currentLine = 0;
13
13
  let column = 0;
14
+
14
15
  for (const chunk of chunks) {
15
16
  const parts = chunk.text.split("\n");
17
+
16
18
  for (const [index, part] of parts.entries()) {
17
19
  const width = Bun.stringWidth(part);
20
+
18
21
  if (currentLine === line && offset >= column && offset < column + width) {
19
22
  return chunk.link?.url ?? null;
20
23
  }
24
+
21
25
  column += width;
26
+
22
27
  if (index < parts.length - 1) {
23
28
  currentLine += 1;
24
29
  column = 0;
25
30
  }
26
31
  }
27
32
  }
33
+
28
34
  return null;
29
35
  };
30
36
 
31
37
  /**
32
- * Primary-click handler for a Markdown table. It activates the link under the pointer and
33
- * consumes the event only when the host handler returns literal true.
38
+ * Primary-click handler for one Markdown table cell. It activates the link
39
+ * under the pointer and consumes the event only when the host handler returns
40
+ * literal true.
34
41
  */
35
- export const tableLinkMouseHandler = function tableLinkMouseHandler(
36
- content: TextTableContent,
42
+ export const tableCellLinkMouseHandler = function tableCellLinkMouseHandler(
43
+ chunks: readonly TextChunk[],
37
44
  onLinkActivate?: MarkdownLinkHandler
38
45
  ): ((event: MouseEvent) => void) | undefined {
39
- if (onLinkActivate === undefined) {
40
- return undefined;
41
- }
42
- return (event: MouseEvent): void => {
43
- if (event.button !== 0 || event.target === null) {
44
- return;
45
- }
46
- const hit = textTableCellAt(event.target, event.x, event.y);
47
- const chunks = hit === null ? null : content[hit.row]?.[hit.column];
48
- if (hit === null || chunks === null || chunks === undefined) {
49
- return;
50
- }
51
- const href = chunkLinkAt(chunks, hit.line, hit.offset);
52
- if (href === null || onLinkActivate(href) !== true) {
53
- return;
54
- }
55
- event.preventDefault();
56
- event.stopPropagation();
57
- };
46
+ return positionLinkMouseHandler(
47
+ (position) => chunkLinkAt(chunks, position.line, position.column),
48
+ onLinkActivate
49
+ );
58
50
  };
@@ -9,8 +9,10 @@ interface KnownTokenByType {
9
9
  em: Tokens.Em;
10
10
  escape: Tokens.Escape;
11
11
  heading: Tokens.Heading;
12
+ hr: Tokens.Hr;
12
13
  image: Tokens.Image;
13
14
  link: Tokens.Link;
15
+ list: Tokens.List;
14
16
  paragraph: Tokens.Paragraph;
15
17
  space: Tokens.Space;
16
18
  strong: Tokens.Strong;
@@ -33,6 +35,7 @@ export const narrowToken = function narrowToken<Type extends keyof KnownTokenByT
33
35
  if (token.type !== type) {
34
36
  return null;
35
37
  }
38
+
36
39
  // SAFETY: Marked creates the token member selected by the checked type discriminator.
37
40
  // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the Generic fallback prevents discriminator narrowing
38
41
  return token as KnownTokenByType[Type];
@@ -43,6 +46,7 @@ export const hasMarkedText = function hasMarkedText(token: Token): token is Toke
43
46
  if (!("text" in token)) {
44
47
  return false;
45
48
  }
49
+
46
50
  // oxlint-disable-next-line anti-slop/no-runtime-typeof -- primitive check stays at the Marked token boundary
47
51
  return typeof token.text === "string";
48
52
  };
@@ -27,55 +27,75 @@ export const visitInline = function visitInline<Result>(
27
27
  for (const [index, token] of tokens.entries()) {
28
28
  const key = `${keyPrefix}${index}`;
29
29
  const text = narrowToken(token, "text");
30
+
30
31
  if (text !== null) {
31
32
  result.push(...sink.text(text, key));
32
33
  continue;
33
34
  }
35
+
34
36
  const strong = narrowToken(token, "strong");
37
+
35
38
  if (strong !== null) {
36
39
  result.push(...sink.strong(strong, visitInline(strong.tokens, sink, `${key}.`), key));
37
40
  continue;
38
41
  }
42
+
39
43
  const em = narrowToken(token, "em");
44
+
40
45
  if (em !== null) {
41
46
  result.push(...sink.em(em, visitInline(em.tokens, sink, `${key}.`), key));
42
47
  continue;
43
48
  }
49
+
44
50
  const codespan = narrowToken(token, "codespan");
51
+
45
52
  if (codespan !== null) {
46
53
  result.push(...sink.codespan(codespan, key));
47
54
  continue;
48
55
  }
56
+
49
57
  const link = narrowToken(token, "link");
58
+
50
59
  if (link !== null) {
51
60
  result.push(...sink.link(link, visitInline(link.tokens, sink, `${key}.`), key));
52
61
  continue;
53
62
  }
63
+
54
64
  const del = narrowToken(token, "del");
65
+
55
66
  if (del !== null) {
56
67
  result.push(...sink.del(del, visitInline(del.tokens, sink, `${key}.`), key));
57
68
  continue;
58
69
  }
70
+
59
71
  const image = narrowToken(token, "image");
72
+
60
73
  if (image !== null) {
61
74
  result.push(...sink.image(image, key));
62
75
  continue;
63
76
  }
77
+
64
78
  const br = narrowToken(token, "br");
79
+
65
80
  if (br !== null) {
66
81
  result.push(...sink.br(br, key));
67
82
  continue;
68
83
  }
84
+
69
85
  const escape = narrowToken(token, "escape");
86
+
70
87
  if (escape !== null) {
71
88
  result.push(...sink.escape(escape, key));
72
89
  continue;
73
90
  }
91
+
74
92
  const space = narrowToken(token, "space");
93
+
75
94
  if (space !== null) {
76
95
  result.push(...sink.space(space, key));
77
96
  continue;
78
97
  }
98
+
79
99
  result.push(...sink.fallback(token, key));
80
100
  }
81
101