@tooee/renderers 0.7.3 → 0.8.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 (126) hide show
  1. package/dist/code-blocks.d.ts +2 -1
  2. package/dist/code-blocks.d.ts.map +1 -1
  3. package/dist/code-blocks.js +20 -18
  4. package/dist/code-blocks.js.map +1 -1
  5. package/dist/code-view.d.ts +2 -1
  6. package/dist/code-view.d.ts.map +1 -1
  7. package/dist/code-view.js.map +1 -1
  8. package/dist/command-palette.d.ts +2 -1
  9. package/dist/command-palette.d.ts.map +1 -1
  10. package/dist/command-palette.js +16 -23
  11. package/dist/command-palette.js.map +1 -1
  12. package/dist/context-menu.d.ts +2 -1
  13. package/dist/context-menu.d.ts.map +1 -1
  14. package/dist/context-menu.js +50 -15
  15. package/dist/context-menu.js.map +1 -1
  16. package/dist/index.d.ts +3 -3
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/markdown/blocks/blockquote.d.ts +11 -0
  21. package/dist/markdown/blocks/blockquote.d.ts.map +1 -0
  22. package/dist/markdown/blocks/blockquote.js +26 -0
  23. package/dist/markdown/blocks/blockquote.js.map +1 -0
  24. package/dist/markdown/blocks/flat-block.d.ts +20 -0
  25. package/dist/markdown/blocks/flat-block.d.ts.map +1 -0
  26. package/dist/markdown/blocks/flat-block.js +52 -0
  27. package/dist/markdown/blocks/flat-block.js.map +1 -0
  28. package/dist/markdown/blocks/heading.d.ts +11 -0
  29. package/dist/markdown/blocks/heading.d.ts.map +1 -0
  30. package/dist/markdown/blocks/heading.js +23 -0
  31. package/dist/markdown/blocks/heading.js.map +1 -0
  32. package/dist/markdown/blocks/horizontal-rule.d.ts +7 -0
  33. package/dist/markdown/blocks/horizontal-rule.d.ts.map +1 -0
  34. package/dist/markdown/blocks/horizontal-rule.js +5 -0
  35. package/dist/markdown/blocks/horizontal-rule.js.map +1 -0
  36. package/dist/markdown/blocks/list-line.d.ts +10 -0
  37. package/dist/markdown/blocks/list-line.d.ts.map +1 -0
  38. package/dist/markdown/blocks/list-line.js +21 -0
  39. package/dist/markdown/blocks/list-line.js.map +1 -0
  40. package/dist/markdown/blocks/paragraph.d.ts +12 -0
  41. package/dist/markdown/blocks/paragraph.d.ts.map +1 -0
  42. package/dist/markdown/blocks/paragraph.js +27 -0
  43. package/dist/markdown/blocks/paragraph.js.map +1 -0
  44. package/dist/markdown/blocks/table.d.ts +7 -0
  45. package/dist/markdown/blocks/table.d.ts.map +1 -0
  46. package/dist/markdown/blocks/table.js +30 -0
  47. package/dist/markdown/blocks/table.js.map +1 -0
  48. package/dist/markdown/chunks.d.ts +5 -0
  49. package/dist/markdown/chunks.d.ts.map +1 -0
  50. package/dist/markdown/chunks.js +29 -0
  51. package/dist/markdown/chunks.js.map +1 -0
  52. package/dist/markdown/inline.d.ts +8 -0
  53. package/dist/markdown/inline.d.ts.map +1 -0
  54. package/dist/markdown/inline.js +27 -0
  55. package/dist/markdown/inline.js.map +1 -0
  56. package/dist/markdown/links.d.ts +11 -0
  57. package/dist/markdown/links.d.ts.map +1 -0
  58. package/dist/markdown/links.js +107 -0
  59. package/dist/markdown/links.js.map +1 -0
  60. package/dist/markdown/plain-text.d.ts +4 -0
  61. package/dist/markdown/plain-text.d.ts.map +1 -0
  62. package/dist/markdown/plain-text.js +21 -0
  63. package/dist/markdown/plain-text.js.map +1 -0
  64. package/dist/markdown/tokens.d.ts +32 -0
  65. package/dist/markdown/tokens.d.ts.map +1 -0
  66. package/dist/markdown/tokens.js +25 -0
  67. package/dist/markdown/tokens.js.map +1 -0
  68. package/dist/markdown/visit-inline.d.ts +17 -0
  69. package/dist/markdown/visit-inline.d.ts.map +1 -0
  70. package/dist/markdown/visit-inline.js +61 -0
  71. package/dist/markdown/visit-inline.js.map +1 -0
  72. package/dist/markdown-blocks.d.ts.map +1 -1
  73. package/dist/markdown-blocks.js +6 -4
  74. package/dist/markdown-blocks.js.map +1 -1
  75. package/dist/markdown-images.d.ts.map +1 -1
  76. package/dist/markdown-images.js +14 -5
  77. package/dist/markdown-images.js.map +1 -1
  78. package/dist/markdown-view.d.ts +10 -32
  79. package/dist/markdown-view.d.ts.map +1 -1
  80. package/dist/markdown-view.js +9 -468
  81. package/dist/markdown-view.js.map +1 -1
  82. package/dist/mermaid.d.ts +5 -4
  83. package/dist/mermaid.d.ts.map +1 -1
  84. package/dist/mermaid.js +5 -5
  85. package/dist/mermaid.js.map +1 -1
  86. package/dist/parsers.d.ts +6 -13
  87. package/dist/parsers.d.ts.map +1 -1
  88. package/dist/parsers.js +66 -46
  89. package/dist/parsers.js.map +1 -1
  90. package/dist/row-document-renderable.d.ts +7 -6
  91. package/dist/row-document-renderable.d.ts.map +1 -1
  92. package/dist/row-document-renderable.js +2 -7
  93. package/dist/row-document-renderable.js.map +1 -1
  94. package/dist/table-types.d.ts +4 -0
  95. package/dist/table-types.d.ts.map +1 -1
  96. package/dist/table.d.ts +4 -1
  97. package/dist/table.d.ts.map +1 -1
  98. package/dist/table.js +30 -23
  99. package/dist/table.js.map +1 -1
  100. package/package.json +7 -5
  101. package/src/code-blocks.tsx +28 -26
  102. package/src/code-view.tsx +2 -1
  103. package/src/command-palette.tsx +46 -31
  104. package/src/context-menu.tsx +54 -15
  105. package/src/index.ts +7 -3
  106. package/src/markdown/blocks/blockquote.tsx +53 -0
  107. package/src/markdown/blocks/flat-block.tsx +119 -0
  108. package/src/markdown/blocks/heading.tsx +53 -0
  109. package/src/markdown/blocks/horizontal-rule.tsx +16 -0
  110. package/src/markdown/blocks/list-line.tsx +52 -0
  111. package/src/markdown/blocks/paragraph.tsx +90 -0
  112. package/src/markdown/blocks/table.tsx +55 -0
  113. package/src/markdown/chunks.ts +42 -0
  114. package/src/markdown/inline.tsx +45 -0
  115. package/src/markdown/links.ts +133 -0
  116. package/src/markdown/plain-text.ts +22 -0
  117. package/src/markdown/tokens.ts +53 -0
  118. package/src/markdown/visit-inline.ts +82 -0
  119. package/src/markdown-blocks.ts +6 -4
  120. package/src/markdown-images.ts +15 -6
  121. package/src/markdown-view.tsx +20 -845
  122. package/src/mermaid.ts +11 -9
  123. package/src/parsers.ts +86 -62
  124. package/src/row-document-renderable.ts +13 -15
  125. package/src/table-types.ts +5 -0
  126. package/src/table.tsx +36 -30
@@ -202,10 +202,14 @@ export const mermaidCodeBlockRenderer: CodeBlockRenderer = ({
202
202
  );
203
203
  };
204
204
 
205
- /** Built-in code block renderers, merged under user-provided entries. */
206
- export const DEFAULT_CODE_BLOCK_RENDERERS: Record<string, CodeBlockRenderer> = {
205
+ const DEFAULT_CODE_BLOCK_RENDERER_ENTRIES = {
207
206
  mermaid: mermaidCodeBlockRenderer,
208
- };
207
+ } satisfies Record<string, CodeBlockRenderer>;
208
+
209
+ /** Built-in code block renderers, merged under user-provided entries. */
210
+ // oxlint-disable-next-line anti-slop/no-known-value-widening -- public registry contract permits arbitrary fence-type lookup
211
+ const defaultRendererMap: Record<string, CodeBlockRenderer> = DEFAULT_CODE_BLOCK_RENDERER_ENTRIES;
212
+ export { defaultRendererMap as DEFAULT_CODE_BLOCK_RENDERERS };
209
213
 
210
214
  // ---------------------------------------------------------------------------
211
215
  // Horizontal panning registration
@@ -265,6 +269,27 @@ const useHScrollableBlock = function useHScrollableBlock(
265
269
  // Dispatching block component
266
270
  // ---------------------------------------------------------------------------
267
271
 
272
+ /**
273
+ * Invokes a custom renderer, falling back to the default code block when it
274
+ * returns `null` or throws. Isolated in its own component so renderer hooks
275
+ * have a stable home that remounts when the fence type changes.
276
+ */
277
+ const CustomCodeBlock = function CustomCodeBlock({
278
+ renderer,
279
+ rendererProps,
280
+ }: {
281
+ renderer: CodeBlockRenderer;
282
+ rendererProps: CodeBlockRendererProps;
283
+ }): ReactNode {
284
+ let node: ReactNode = null;
285
+ try {
286
+ node = renderer(rendererProps);
287
+ } catch {
288
+ node = null;
289
+ }
290
+ return node ?? defaultCodeBlockRenderer(rendererProps);
291
+ };
292
+
268
293
  /**
269
294
  * Renders a fenced code block, dispatching to a registered custom renderer
270
295
  * for the fence type when one exists, and falling back to the default
@@ -306,36 +331,13 @@ export const CodeBlock = function CodeBlock({
306
331
  };
307
332
 
308
333
  const custom = rendererProps.lang === "" ? undefined : renderers?.[rendererProps.lang];
309
- // oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): preserve deliberate top-down custom renderer organization
310
334
  if (custom && custom !== defaultCodeBlockRenderer) {
311
335
  // Keyed by fence type so a type change remounts the custom renderer
312
336
  // (renderers may use hooks; see CodeBlockRenderer docs).
313
337
  return (
314
- // oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): preserve deliberate top-down custom renderer organization
315
338
  <CustomCodeBlock key={rendererProps.lang} renderer={custom} rendererProps={rendererProps} />
316
339
  );
317
340
  }
318
341
 
319
342
  return defaultCodeBlockRenderer(rendererProps);
320
343
  };
321
-
322
- /**
323
- * Invokes a custom renderer, falling back to the default code block when it
324
- * returns `null` or throws. Isolated in its own component so renderer hooks
325
- * have a stable home that remounts when the fence type changes.
326
- */
327
- const CustomCodeBlock = function CustomCodeBlock({
328
- renderer,
329
- rendererProps,
330
- }: {
331
- renderer: CodeBlockRenderer;
332
- rendererProps: CodeBlockRendererProps;
333
- }): ReactNode {
334
- let node: ReactNode = null;
335
- try {
336
- node = renderer(rendererProps);
337
- } catch {
338
- node = null;
339
- }
340
- return node ?? defaultCodeBlockRenderer(rendererProps);
341
- };
package/src/code-view.tsx CHANGED
@@ -3,6 +3,7 @@ import type { DocumentBindings } from "./document-bindings.js";
3
3
  import { DEFAULT_SIGN_COLUMN_WIDTH } from "./row-document-renderable.js";
4
4
  import { useGutterPalette } from "./use-gutter-palette.js";
5
5
  import "./row-document.js";
6
+ import type { ReactNode } from "react";
6
7
 
7
8
  interface CodeViewProps {
8
9
  content: string;
@@ -26,7 +27,7 @@ export const CodeView = function CodeView({
26
27
  language,
27
28
  showLineNumbers = true,
28
29
  document,
29
- }: CodeViewProps): React.ReactNode {
30
+ }: CodeViewProps): ReactNode {
30
31
  const { syntax } = useTheme();
31
32
  const palette = useGutterPalette();
32
33
 
@@ -1,7 +1,9 @@
1
1
  import { useState, useMemo, useCallback } from "react";
2
+ import type { ReactNode } from "react";
2
3
  import { useCommand } from "@tooee/commands";
3
- import { useTheme, CloseButton } from "@tooee/themes";
4
- import { fuzzyMatch } from "@tooee/fuzzy";
4
+ import { CloseButton } from "@tooee/layout";
5
+ import { useTheme } from "@tooee/themes";
6
+ import { rankBy } from "@tooee/fuzzy";
5
7
 
6
8
  export interface CommandPaletteEntry {
7
9
  id: string;
@@ -17,44 +19,57 @@ interface CommandPaletteProps {
17
19
  onClose: () => void;
18
20
  }
19
21
 
22
+ const HighlightedTitle = function HighlightedTitle({
23
+ title,
24
+ positions,
25
+ }: {
26
+ title: string;
27
+ positions: readonly number[];
28
+ }): ReactNode {
29
+ const { theme } = useTheme();
30
+ const highlighted = new Set(positions);
31
+ return (
32
+ <text fg={theme.text} style={{ flexGrow: 1 }}>
33
+ {Array.from(
34
+ title,
35
+ (character, index): ReactNode => (
36
+ <span key={index} fg={highlighted.has(index) ? theme.warning : theme.text}>
37
+ {character}
38
+ </span>
39
+ ),
40
+ )}
41
+ </text>
42
+ );
43
+ };
44
+
20
45
  export const CommandPalette = function CommandPalette({
21
46
  commands,
22
47
  onSelect,
23
48
  onClose,
24
- }: CommandPaletteProps): React.ReactNode {
49
+ }: CommandPaletteProps): ReactNode {
25
50
  const { theme } = useTheme();
26
51
  const [filter, setFilter] = useState("");
27
52
  const [activeIndex, setActiveIndex] = useState(0);
28
53
 
29
- const filtered = useMemo(() => {
30
- if (!filter) {
31
- return commands;
32
- }
33
- const results: { entry: CommandPaletteEntry; score: number }[] = [];
34
- for (const entry of commands) {
35
- const score = fuzzyMatch(filter, entry.title);
36
- if (score !== null) {
37
- results.push({ entry, score });
38
- }
39
- }
40
- results.sort((a, b) => b.score - a.score);
41
- return results.map((r) => r.entry);
42
- }, [commands, filter]);
54
+ const matches = useMemo(
55
+ () => rankBy(commands, filter, (entry) => entry.title),
56
+ [commands, filter],
57
+ );
43
58
 
44
59
  const handleSelect = useCallback(() => {
45
- const item = filtered[activeIndex];
60
+ const item = matches[activeIndex]?.item;
46
61
  if (item !== undefined) {
47
62
  onSelect(item.id);
48
63
  }
49
- }, [filtered, activeIndex, onSelect]);
64
+ }, [matches, activeIndex, onSelect]);
50
65
 
51
66
  const moveUp = useCallback(() => {
52
67
  setActiveIndex((i) => Math.max(0, i - 1));
53
68
  }, []);
54
69
 
55
70
  const moveDown = useCallback(() => {
56
- setActiveIndex((i) => Math.min(filtered.length - 1, i + 1));
57
- }, [filtered.length]);
71
+ setActiveIndex((i) => Math.min(matches.length - 1, i + 1));
72
+ }, [matches.length]);
58
73
 
59
74
  useCommand({
60
75
  handler: onClose,
@@ -119,7 +134,7 @@ export const CommandPalette = function CommandPalette({
119
134
  cursorColor={theme.accent}
120
135
  style={{ flexGrow: 1 }}
121
136
  />
122
- <text content={` ${filtered.length}`} fg={theme.textMuted} />
137
+ <text content={` ${matches.length}`} fg={theme.textMuted} />
123
138
  <CloseButton onClose={onClose} />
124
139
  </box>
125
140
 
@@ -128,10 +143,10 @@ export const CommandPalette = function CommandPalette({
128
143
 
129
144
  {/* Command list */}
130
145
  <scrollbox focused={false} style={{ flexGrow: 1 }}>
131
- {filtered.map(
132
- (entry, i): React.ReactNode => (
146
+ {matches.map(
147
+ (match, i): ReactNode => (
133
148
  <box
134
- key={entry.id}
149
+ key={match.item.id}
135
150
  flexDirection="row"
136
151
  paddingLeft={1}
137
152
  paddingRight={1}
@@ -144,15 +159,15 @@ export const CommandPalette = function CommandPalette({
144
159
  }
145
160
  event.preventDefault();
146
161
  event.stopPropagation();
147
- onSelect(entry.id);
162
+ onSelect(match.item.id);
148
163
  }}
149
164
  >
150
- <text content={entry.title} fg={theme.text} style={{ flexGrow: 1 }} />
151
- {(entry.hotkey?.length ?? 0) > 0 && (
152
- <text content={entry.hotkey} fg={theme.textMuted} />
165
+ <HighlightedTitle title={match.item.title} positions={match.positions} />
166
+ {(match.item.hotkey?.length ?? 0) > 0 && (
167
+ <text content={match.item.hotkey} fg={theme.textMuted} />
153
168
  )}
154
- {(entry.category?.length ?? 0) > 0 && (
155
- <text content={` ${entry.category}`} fg={theme.textMuted} />
169
+ {(match.item.category?.length ?? 0) > 0 && (
170
+ <text content={` ${match.item.category}`} fg={theme.textMuted} />
156
171
  )}
157
172
  </box>
158
173
  ),
@@ -1,6 +1,8 @@
1
1
  import { useState, useCallback } from "react";
2
- import { useKeyboard, useTerminalDimensions } from "@opentui/react";
2
+ import { useTerminalDimensions } from "@opentui/react";
3
+ import { useCommand } from "@tooee/commands";
3
4
  import { useTheme } from "@tooee/themes";
5
+ import type { ReactNode } from "react";
4
6
 
5
7
  export interface ContextMenuEntry {
6
8
  id: string;
@@ -35,7 +37,7 @@ export const ContextMenu = function ContextMenu({
35
37
  y,
36
38
  onSelect,
37
39
  onClose,
38
- }: ContextMenuProps): React.ReactNode {
40
+ }: ContextMenuProps): ReactNode {
39
41
  const { theme } = useTheme();
40
42
  const { width: termWidth, height: termHeight } = useTerminalDimensions();
41
43
  const [activeIndex, setActiveIndex] = useState(0);
@@ -50,18 +52,55 @@ export const ContextMenu = function ContextMenu({
50
52
  [entries, onSelect],
51
53
  );
52
54
 
53
- useKeyboard((key) => {
54
- if (key.name === "up" || key.raw === "k") {
55
- key.preventDefault();
56
- setActiveIndex((i) => Math.max(0, i - 1));
57
- } else if (key.name === "down" || key.raw === "j") {
58
- key.preventDefault();
59
- setActiveIndex((i) => Math.min(entries.length - 1, i + 1));
60
- } else if (key.name === "return") {
61
- key.preventDefault();
62
- select(activeIndex);
63
- }
64
- // Escape is handled by the overlay layer (dismissOnEscape).
55
+ const moveUp = useCallback(() => {
56
+ setActiveIndex((index) => Math.max(0, index - 1));
57
+ }, []);
58
+ const moveDown = useCallback(() => {
59
+ setActiveIndex((index) => Math.min(entries.length - 1, index + 1));
60
+ }, [entries.length]);
61
+ const selectActive = useCallback(() => {
62
+ select(activeIndex);
63
+ }, [activeIndex, select]);
64
+
65
+ useCommand({
66
+ handler: moveUp,
67
+ hidden: true,
68
+ hotkey: "up",
69
+ id: "context-menu:move-up",
70
+ modes: ["insert", "cursor"],
71
+ title: "Move up",
72
+ });
73
+ useCommand({
74
+ handler: moveUp,
75
+ hidden: true,
76
+ hotkey: "k",
77
+ id: "context-menu:move-up-vim",
78
+ modes: ["insert", "cursor"],
79
+ title: "Move up",
80
+ });
81
+ useCommand({
82
+ handler: moveDown,
83
+ hidden: true,
84
+ hotkey: "down",
85
+ id: "context-menu:move-down",
86
+ modes: ["insert", "cursor"],
87
+ title: "Move down",
88
+ });
89
+ useCommand({
90
+ handler: moveDown,
91
+ hidden: true,
92
+ hotkey: "j",
93
+ id: "context-menu:move-down-vim",
94
+ modes: ["insert", "cursor"],
95
+ title: "Move down",
96
+ });
97
+ useCommand({
98
+ handler: selectActive,
99
+ hidden: true,
100
+ hotkey: "Enter",
101
+ id: "context-menu:select",
102
+ modes: ["insert", "cursor"],
103
+ title: "Select action",
65
104
  });
66
105
 
67
106
  // Size the panel from its contents (terminal cell width, not code units,
@@ -122,7 +161,7 @@ export const ContextMenu = function ContextMenu({
122
161
  </box>
123
162
  ) : (
124
163
  entries.map(
125
- (entry, i): React.ReactNode => (
164
+ (entry, i): ReactNode => (
126
165
  <box
127
166
  key={entry.id}
128
167
  flexDirection="row"
package/src/index.ts CHANGED
@@ -25,7 +25,7 @@ export {
25
25
  } from "./code-blocks.js";
26
26
  export type { CodeBlockRenderer, CodeBlockRendererProps, CodeBlockHScroll } from "./code-blocks.js";
27
27
  export { CodeView } from "./code-view.js";
28
- export { Table, computeColumnWidths, isNumeric, sampleRows } from "./table.js";
28
+ export { Table, computeColumnWidths, formatTableCell, isNumeric, sampleRows } from "./table.js";
29
29
  export type { TableProps, ColumnWidthOptions } from "./table.js";
30
30
  export type { ColumnDef, TableRow } from "./table-types.js";
31
31
  export { CommandPalette } from "./command-palette.js";
@@ -33,13 +33,17 @@ export type { CommandPaletteEntry } from "./command-palette.js";
33
33
  export { ContextMenu } from "./context-menu.js";
34
34
  export type { ContextMenuEntry } from "./context-menu.js";
35
35
  export { parseCSV, parseTSV, parseJSON, parseAuto, detectFormat } from "./parsers.js";
36
- export type { Format, ParsedTable } from "./parsers.js";
36
+ export type { Format, ParsedTable, TableData } from "./parsers.js";
37
37
  export { RowDocumentRenderable } from "./row-document-renderable.js";
38
38
  export {
39
39
  DEFAULT_SIGN_COLUMN_WIDTH,
40
40
  computeRowDocumentGutterWidth,
41
41
  } from "./row-document-renderable.js";
42
- export type { RowDocumentOptions, RowDocumentPalette } from "./row-document-renderable.js";
42
+ export type {
43
+ RowDocumentOptions,
44
+ RowDocumentPalette,
45
+ VisibleRowRange,
46
+ } from "./row-document-renderable.js";
43
47
  export type { DecorationLayer, RowDecoration } from "./decoration-layer.js";
44
48
  export type { DocumentBindings } from "./document-bindings.js";
45
49
  export { useGutterPalette } from "./use-gutter-palette.js";
@@ -0,0 +1,53 @@
1
+ import type { Token, Tokens } from "marked";
2
+ import type { ReactNode } from "react";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { InlineTokens } from "../inline.js";
5
+ import { linkMouseHandler } from "../links.js";
6
+ import type { MarkdownLinkHandler } from "../links.js";
7
+ import { hasMarkedText } from "../tokens.js";
8
+
9
+ export const BlockquoteRenderer = function BlockquoteRenderer({
10
+ token,
11
+ theme,
12
+ indent,
13
+ onLinkActivate,
14
+ }: {
15
+ token: Tokens.Blockquote;
16
+ theme: ResolvedTheme;
17
+ indent: number;
18
+ onLinkActivate?: MarkdownLinkHandler;
19
+ }): ReactNode {
20
+ const inlineTokens: Token[] = [];
21
+ for (const child of token.tokens) {
22
+ if ("tokens" in child && Array.isArray(child.tokens)) {
23
+ if (inlineTokens.length > 0) {
24
+ inlineTokens.push({ raw: "\n", type: "br" });
25
+ }
26
+ inlineTokens.push(...child.tokens);
27
+ } else if (hasMarkedText(child)) {
28
+ inlineTokens.push(child);
29
+ }
30
+ }
31
+
32
+ return (
33
+ <box
34
+ border={["left"]}
35
+ borderColor={theme.markdownBlockQuote}
36
+ borderStyle="single"
37
+ style={{
38
+ marginBottom: 1,
39
+ marginLeft: 1 + indent,
40
+ marginRight: 1,
41
+ marginTop: 0,
42
+ paddingLeft: 1,
43
+ }}
44
+ >
45
+ <text
46
+ style={{ fg: theme.textMuted }}
47
+ onMouseDown={linkMouseHandler(inlineTokens, onLinkActivate)}
48
+ >
49
+ <InlineTokens tokens={inlineTokens} theme={theme} />
50
+ </text>
51
+ </box>
52
+ );
53
+ };
@@ -0,0 +1,119 @@
1
+ import type { ReactNode, RefObject } from "react";
2
+ import type { SyntaxStyle, TextBufferRenderable } from "@opentui/core";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { CodeBlock } from "../../code-blocks.js";
5
+ import type { CodeBlockRenderer } from "../../code-blocks.js";
6
+ import type { FlatBlock } from "../../markdown-blocks.js";
7
+ import { BlockquoteRenderer } from "./blockquote.js";
8
+ import { HeadingRenderer } from "./heading.js";
9
+ import { HorizontalRule } from "./horizontal-rule.js";
10
+ import { ListLineRenderer } from "./list-line.js";
11
+ import { ParagraphRenderer } from "./paragraph.js";
12
+ import { MarkdownTableRenderer } from "./table.js";
13
+ import type { MarkdownLinkHandler } from "../links.js";
14
+ import { hasMarkedText, narrowToken } from "../tokens.js";
15
+
16
+ type CodeBlockRendererRegistry = Record<string, CodeBlockRenderer>;
17
+
18
+ export const FlatBlockRenderer = function FlatBlockRenderer({
19
+ block,
20
+ blockIndex,
21
+ theme,
22
+ syntax,
23
+ contentWidth,
24
+ hScrollableBlocksRef,
25
+ codeBlockRenderers,
26
+ onLinkActivate,
27
+ imageBasePath,
28
+ }: {
29
+ block: FlatBlock;
30
+ blockIndex: number;
31
+ theme: ResolvedTheme;
32
+ syntax: SyntaxStyle;
33
+ contentWidth: number;
34
+ hScrollableBlocksRef?: RefObject<Map<number, TextBufferRenderable>>;
35
+ codeBlockRenderers?: CodeBlockRendererRegistry;
36
+ onLinkActivate?: MarkdownLinkHandler;
37
+ imageBasePath?: string;
38
+ }): ReactNode {
39
+ const { token, indent, bullet } = block;
40
+
41
+ if (bullet !== undefined) {
42
+ return <ListLineRenderer block={block} theme={theme} onLinkActivate={onLinkActivate} />;
43
+ }
44
+
45
+ const heading = narrowToken(token, "heading");
46
+ if (heading !== null) {
47
+ return (
48
+ <HeadingRenderer
49
+ token={heading}
50
+ theme={theme}
51
+ indent={indent}
52
+ onLinkActivate={onLinkActivate}
53
+ />
54
+ );
55
+ }
56
+ const paragraph = narrowToken(token, "paragraph");
57
+ if (paragraph !== null) {
58
+ return (
59
+ <ParagraphRenderer
60
+ token={paragraph}
61
+ theme={theme}
62
+ indent={indent}
63
+ onLinkActivate={onLinkActivate}
64
+ imageBasePath={imageBasePath}
65
+ />
66
+ );
67
+ }
68
+ const code = narrowToken(token, "code");
69
+ if (code !== null) {
70
+ return (
71
+ <CodeBlock
72
+ token={code}
73
+ blockIndex={blockIndex}
74
+ theme={theme}
75
+ syntax={syntax}
76
+ indent={indent}
77
+ contentWidth={contentWidth}
78
+ hScrollableBlocksRef={hScrollableBlocksRef}
79
+ renderers={codeBlockRenderers}
80
+ />
81
+ );
82
+ }
83
+ const blockquote = narrowToken(token, "blockquote");
84
+ if (blockquote !== null) {
85
+ return (
86
+ <BlockquoteRenderer
87
+ token={blockquote}
88
+ theme={theme}
89
+ indent={indent}
90
+ onLinkActivate={onLinkActivate}
91
+ />
92
+ );
93
+ }
94
+ const table = narrowToken(token, "table");
95
+ if (table !== null) {
96
+ return <MarkdownTableRenderer token={table} indent={indent} />;
97
+ }
98
+ if (token.type === "hr") {
99
+ return <HorizontalRule theme={theme} indent={indent} />;
100
+ }
101
+ if (token.type === "space" || token.type === "html") {
102
+ return null;
103
+ }
104
+ if (!hasMarkedText(token)) {
105
+ return null;
106
+ }
107
+ return (
108
+ <text
109
+ content={token.text}
110
+ style={{
111
+ fg: theme.markdownText,
112
+ marginBottom: 1,
113
+ marginLeft: 1 + indent,
114
+ marginRight: 1,
115
+ marginTop: 0,
116
+ }}
117
+ />
118
+ );
119
+ };
@@ -0,0 +1,53 @@
1
+ import type { Tokens } from "marked";
2
+ import type { ReactNode } from "react";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { InlineTokens } from "../inline.js";
5
+ import { linkMouseHandler } from "../links.js";
6
+ import type { MarkdownLinkHandler } from "../links.js";
7
+
8
+ export const HeadingRenderer = function HeadingRenderer({
9
+ token,
10
+ theme,
11
+ indent,
12
+ onLinkActivate,
13
+ }: {
14
+ token: Tokens.Heading;
15
+ theme: ResolvedTheme;
16
+ indent: number;
17
+ onLinkActivate?: MarkdownLinkHandler;
18
+ }): ReactNode {
19
+ const headingColors = new Map<number, string>([
20
+ [1, theme.markdownHeading],
21
+ [2, theme.secondary],
22
+ [3, theme.accent],
23
+ [4, theme.text],
24
+ [5, theme.textMuted],
25
+ [6, theme.textMuted],
26
+ ]);
27
+ const prefixes = new Map<number, string>([
28
+ [1, "# "],
29
+ [2, "## "],
30
+ [3, "### "],
31
+ [4, "#### "],
32
+ [5, "##### "],
33
+ [6, "###### "],
34
+ ]);
35
+
36
+ return (
37
+ <box style={{ marginBottom: 1, marginLeft: indent, marginTop: 1 }}>
38
+ <text
39
+ style={{ fg: headingColors.get(token.depth) ?? theme.text }}
40
+ onMouseDown={linkMouseHandler(
41
+ token.tokens,
42
+ onLinkActivate,
43
+ Bun.stringWidth(prefixes.get(token.depth) ?? ""),
44
+ )}
45
+ >
46
+ <span fg={theme.textMuted}>{prefixes.get(token.depth)}</span>
47
+ <strong>
48
+ <InlineTokens tokens={token.tokens} theme={theme} />
49
+ </strong>
50
+ </text>
51
+ </box>
52
+ );
53
+ };
@@ -0,0 +1,16 @@
1
+ import type { ReactNode } from "react";
2
+ import type { ResolvedTheme } from "@tooee/themes";
3
+
4
+ export const HorizontalRule = function HorizontalRule({
5
+ theme,
6
+ indent,
7
+ }: {
8
+ theme: ResolvedTheme;
9
+ indent: number;
10
+ }): ReactNode {
11
+ return (
12
+ <box style={{ marginBottom: 1, marginLeft: 1 + indent, marginRight: 1, marginTop: 0 }}>
13
+ <text style={{ fg: theme.markdownHorizontalRule }} content={"─".repeat(40)} />
14
+ </box>
15
+ );
16
+ };
@@ -0,0 +1,52 @@
1
+ import type { Token } from "marked";
2
+ import type { ReactNode } from "react";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { checkboxMarker } from "../../markdown-blocks.js";
5
+ import type { FlatBlock } from "../../markdown-blocks.js";
6
+ import { InlineTokens } from "../inline.js";
7
+ import { linkMouseHandler } from "../links.js";
8
+ import type { MarkdownLinkHandler } from "../links.js";
9
+ import { hasMarkedText } from "../tokens.js";
10
+
11
+ export const ListLineRenderer = function ListLineRenderer({
12
+ block,
13
+ theme,
14
+ onLinkActivate,
15
+ }: {
16
+ block: FlatBlock;
17
+ theme: ResolvedTheme;
18
+ onLinkActivate?: MarkdownLinkHandler;
19
+ }): ReactNode {
20
+ const { token, indent, bullet, checked } = block;
21
+ const checkboxPrefix = checkboxMarker(checked);
22
+ const inlineTokens: Token[] =
23
+ "tokens" in token && Array.isArray(token.tokens) ? token.tokens : [];
24
+ const tokenText = hasMarkedText(token) ? token.text : "";
25
+ const hasContent = inlineTokens.length > 0 || tokenText.length > 0;
26
+
27
+ let content: ReactNode = null;
28
+ if (inlineTokens.length > 0) {
29
+ content = <InlineTokens tokens={inlineTokens} theme={theme} />;
30
+ } else if (tokenText.length > 0) {
31
+ content = tokenText;
32
+ }
33
+
34
+ return (
35
+ <box style={{ marginLeft: 1 + indent, marginRight: 1 }}>
36
+ <text
37
+ style={{ fg: theme.markdownText }}
38
+ onMouseDown={linkMouseHandler(
39
+ inlineTokens,
40
+ onLinkActivate,
41
+ Bun.stringWidth(`${bullet}${checkboxPrefix}`),
42
+ )}
43
+ >
44
+ <span fg={theme.markdownListItem}>{bullet}</span>
45
+ {checkboxPrefix !== "" && (
46
+ <span fg={checked === true ? theme.accent : theme.textMuted}>{checkboxPrefix}</span>
47
+ )}
48
+ {hasContent && content}
49
+ </text>
50
+ </box>
51
+ );
52
+ };