@tooee/renderers 0.13.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 (62) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/markdown/blocks/flat-block.d.ts.map +1 -1
  5. package/dist/markdown/blocks/flat-block.js +38 -33
  6. package/dist/markdown/blocks/flat-block.js.map +1 -1
  7. package/dist/markdown/blocks/list-line.d.ts +2 -2
  8. package/dist/markdown/blocks/list-line.d.ts.map +1 -1
  9. package/dist/markdown/blocks/list-line.js +2 -3
  10. package/dist/markdown/blocks/list-line.js.map +1 -1
  11. package/dist/markdown/blocks/table.d.ts +17 -3
  12. package/dist/markdown/blocks/table.d.ts.map +1 -1
  13. package/dist/markdown/blocks/table.js +29 -29
  14. package/dist/markdown/blocks/table.js.map +1 -1
  15. package/dist/markdown/links.d.ts +10 -0
  16. package/dist/markdown/links.d.ts.map +1 -1
  17. package/dist/markdown/links.js +30 -18
  18. package/dist/markdown/links.js.map +1 -1
  19. package/dist/markdown/table-layout.d.ts +28 -0
  20. package/dist/markdown/table-layout.d.ts.map +1 -0
  21. package/dist/markdown/table-layout.js +100 -0
  22. package/dist/markdown/table-layout.js.map +1 -0
  23. package/dist/markdown/table-links.d.ts +5 -4
  24. package/dist/markdown/table-links.d.ts.map +1 -1
  25. package/dist/markdown/table-links.js +6 -23
  26. package/dist/markdown/table-links.js.map +1 -1
  27. package/dist/markdown/tokens.d.ts +2 -0
  28. package/dist/markdown/tokens.d.ts.map +1 -1
  29. package/dist/markdown/tokens.js.map +1 -1
  30. package/dist/markdown-blocks.d.ts +72 -14
  31. package/dist/markdown-blocks.d.ts.map +1 -1
  32. package/dist/markdown-blocks.js +137 -77
  33. package/dist/markdown-blocks.js.map +1 -1
  34. package/dist/markdown-images.d.ts.map +1 -1
  35. package/dist/markdown-images.js +5 -8
  36. package/dist/markdown-images.js.map +1 -1
  37. package/dist/markdown-view.d.ts +0 -1
  38. package/dist/markdown-view.d.ts.map +1 -1
  39. package/dist/markdown-view.js +0 -1
  40. package/dist/markdown-view.js.map +1 -1
  41. package/package.json +6 -6
  42. package/src/index.ts +14 -1
  43. package/src/markdown/blocks/flat-block.tsx +115 -95
  44. package/src/markdown/blocks/list-line.tsx +4 -7
  45. package/src/markdown/blocks/table.tsx +81 -53
  46. package/src/markdown/links.ts +47 -29
  47. package/src/markdown/table-layout.ts +156 -0
  48. package/src/markdown/table-links.ts +11 -31
  49. package/src/markdown/tokens.ts +2 -0
  50. package/src/markdown-blocks.ts +267 -100
  51. package/src/markdown-images.ts +9 -9
  52. package/src/markdown-view.tsx +0 -1
  53. package/dist/text-table-internals.d.ts +0 -34
  54. package/dist/text-table-internals.d.ts.map +0 -1
  55. package/dist/text-table-internals.js +0 -68
  56. package/dist/text-table-internals.js.map +0 -1
  57. package/dist/text-table.d.ts +0 -7
  58. package/dist/text-table.d.ts.map +0 -1
  59. package/dist/text-table.js +0 -4
  60. package/dist/text-table.js.map +0 -1
  61. package/src/text-table-internals.ts +0 -136
  62. package/src/text-table.ts +0 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tooee/renderers",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Content renderers (markdown, code, text, table) for Tooee",
5
5
  "keywords": [
6
6
  "cli",
@@ -45,11 +45,11 @@
45
45
  "typecheck": "tsc --noEmit"
46
46
  },
47
47
  "dependencies": {
48
- "@tooee/commands": "0.13.0",
49
- "@tooee/fuzzy": "0.13.0",
50
- "@tooee/layout": "0.13.0",
51
- "@tooee/marks": "0.13.0",
52
- "@tooee/themes": "0.13.0",
48
+ "@tooee/commands": "0.14.0",
49
+ "@tooee/fuzzy": "0.14.0",
50
+ "@tooee/layout": "0.14.0",
51
+ "@tooee/marks": "0.14.0",
52
+ "@tooee/themes": "0.14.0",
53
53
  "@xstate/store": "^4.2.3",
54
54
  "@xstate/store-react": "^2",
55
55
  "beautiful-mermaid": "^1.1.3",
package/src/index.ts CHANGED
@@ -6,7 +6,20 @@ export { flattenMarkdown, getFlatBlockText } from "./markdown-blocks.js";
6
6
 
7
7
  export { getPlainText } from "./markdown/plain-text.js";
8
8
 
9
- export type { FlatBlock, FlattenMarkdownOptions } from "./markdown-blocks.js";
9
+ export type {
10
+ FlatBlock,
11
+ FlatBlockKind,
12
+ FlatBlockquoteBlock,
13
+ FlatCodeBlock,
14
+ FlatHeadingBlock,
15
+ FlatListLineBlock,
16
+ FlatOtherBlock,
17
+ FlatParagraphBlock,
18
+ FlatRuleBlock,
19
+ FlatTableRow,
20
+ FlatTableRowBlock,
21
+ FlattenMarkdownOptions,
22
+ } from "./markdown-blocks.js";
10
23
 
11
24
  export {
12
25
  parseObsidianImageEmbed,
@@ -1,21 +1,50 @@
1
1
  import type { SyntaxStyle, TextBufferRenderable } from "@opentui/core";
2
2
  import type { ResolvedTheme } from "@tooee/themes";
3
+ import type { Token } from "marked";
3
4
  import type { ReactNode, RefObject } from "react";
4
5
 
5
6
  import { CodeBlock } from "../../code-blocks.js";
6
7
  import type { CodeBlockRenderer } from "../../code-blocks.js";
7
8
  import type { FlatBlock } from "../../markdown-blocks.js";
8
9
  import type { MarkdownLinkHandler } from "../links.js";
9
- import { hasMarkedText, narrowToken } from "../tokens.js";
10
+ import { hasMarkedText } from "../tokens.js";
10
11
  import { BlockquoteRenderer } from "./blockquote.js";
11
12
  import { HeadingRenderer } from "./heading.js";
12
13
  import { HorizontalRule } from "./horizontal-rule.js";
13
14
  import { ListLineRenderer } from "./list-line.js";
14
15
  import { ParagraphRenderer } from "./paragraph.js";
15
- import { MarkdownTableRenderer } from "./table.js";
16
+ import { MarkdownTableRowRenderer } from "./table.js";
16
17
 
17
18
  type CodeBlockRendererRegistry = Record<string, CodeBlockRenderer>;
18
19
 
20
+ /** A block with no dedicated renderer: its plain text, when the token has any. */
21
+ const OtherBlockRenderer = function OtherBlockRenderer({
22
+ token,
23
+ theme,
24
+ indent,
25
+ }: {
26
+ token: Token;
27
+ theme: ResolvedTheme;
28
+ indent: number;
29
+ }): ReactNode {
30
+ if (!hasMarkedText(token)) {
31
+ return null;
32
+ }
33
+
34
+ return (
35
+ <text
36
+ content={token.text}
37
+ style={{
38
+ fg: theme.markdownText,
39
+ marginBottom: 1,
40
+ marginLeft: 1 + indent,
41
+ marginRight: 1,
42
+ marginTop: 0,
43
+ }}
44
+ />
45
+ );
46
+ };
47
+
19
48
  export const FlatBlockRenderer = function FlatBlockRenderer({
20
49
  block,
21
50
  blockIndex,
@@ -40,98 +69,89 @@ export const FlatBlockRenderer = function FlatBlockRenderer({
40
69
  onLinkActivate?: MarkdownLinkHandler;
41
70
  imageBasePath?: string;
42
71
  }): ReactNode {
43
- const { token, indent, bullet } = block;
44
-
45
- if (bullet !== undefined) {
46
- return <ListLineRenderer block={block} theme={theme} onLinkActivate={onLinkActivate} />;
47
- }
48
-
49
- const heading = narrowToken(token, "heading");
50
-
51
- if (heading !== null) {
52
- return (
53
- <HeadingRenderer
54
- token={heading}
55
- theme={theme}
56
- indent={indent}
57
- hiddenCount={hiddenCount}
58
- onLinkActivate={onLinkActivate}
59
- />
60
- );
61
- }
62
-
63
- const paragraph = narrowToken(token, "paragraph");
64
-
65
- if (paragraph !== null) {
66
- return (
67
- <ParagraphRenderer
68
- token={paragraph}
69
- theme={theme}
70
- indent={indent}
71
- onLinkActivate={onLinkActivate}
72
- imageBasePath={imageBasePath}
73
- />
74
- );
75
- }
76
-
77
- const code = narrowToken(token, "code");
78
-
79
- if (code !== null) {
80
- return (
81
- <CodeBlock
82
- token={code}
83
- blockIndex={blockIndex}
84
- theme={theme}
85
- syntax={syntax}
86
- indent={indent}
87
- contentWidth={contentWidth}
88
- hScrollableBlocksRef={hScrollableBlocksRef}
89
- renderers={codeBlockRenderers}
90
- />
91
- );
92
- }
93
-
94
- const blockquote = narrowToken(token, "blockquote");
95
-
96
- if (blockquote !== null) {
97
- return (
98
- <BlockquoteRenderer
99
- token={blockquote}
100
- theme={theme}
101
- indent={indent}
102
- onLinkActivate={onLinkActivate}
103
- />
104
- );
105
- }
106
-
107
- const table = narrowToken(token, "table");
108
-
109
- if (table !== null) {
110
- return <MarkdownTableRenderer token={table} indent={indent} onLinkActivate={onLinkActivate} />;
111
- }
112
-
113
- if (token.type === "hr") {
114
- return <HorizontalRule theme={theme} indent={indent} />;
115
- }
116
-
117
- if (token.type === "space" || token.type === "html") {
118
- return null;
119
- }
120
-
121
- if (!hasMarkedText(token)) {
122
- return null;
72
+ const { indent } = block;
73
+
74
+ switch (block.kind) {
75
+ case "table-row": {
76
+ return (
77
+ <MarkdownTableRowRenderer
78
+ table={block.row.table}
79
+ index={block.row.index}
80
+ indent={indent}
81
+ theme={theme}
82
+ contentWidth={contentWidth}
83
+ onLinkActivate={onLinkActivate}
84
+ />
85
+ );
86
+ }
87
+
88
+ case "list-line": {
89
+ return <ListLineRenderer block={block} theme={theme} onLinkActivate={onLinkActivate} />;
90
+ }
91
+
92
+ case "heading": {
93
+ return (
94
+ <HeadingRenderer
95
+ token={block.token}
96
+ theme={theme}
97
+ indent={indent}
98
+ hiddenCount={hiddenCount}
99
+ onLinkActivate={onLinkActivate}
100
+ />
101
+ );
102
+ }
103
+
104
+ case "paragraph": {
105
+ return (
106
+ <ParagraphRenderer
107
+ token={block.token}
108
+ theme={theme}
109
+ indent={indent}
110
+ onLinkActivate={onLinkActivate}
111
+ imageBasePath={imageBasePath}
112
+ />
113
+ );
114
+ }
115
+
116
+ case "code": {
117
+ return (
118
+ <CodeBlock
119
+ token={block.token}
120
+ blockIndex={blockIndex}
121
+ theme={theme}
122
+ syntax={syntax}
123
+ indent={indent}
124
+ contentWidth={contentWidth}
125
+ hScrollableBlocksRef={hScrollableBlocksRef}
126
+ renderers={codeBlockRenderers}
127
+ />
128
+ );
129
+ }
130
+
131
+ case "blockquote": {
132
+ return (
133
+ <BlockquoteRenderer
134
+ token={block.token}
135
+ theme={theme}
136
+ indent={indent}
137
+ onLinkActivate={onLinkActivate}
138
+ />
139
+ );
140
+ }
141
+
142
+ case "hr": {
143
+ return <HorizontalRule theme={theme} indent={indent} />;
144
+ }
145
+
146
+ case "other": {
147
+ return <OtherBlockRenderer token={block.token} theme={theme} indent={indent} />;
148
+ }
149
+
150
+ default: {
151
+ // A new `FlatBlock` kind without a case here fails to compile.
152
+ const unhandled: never = block;
153
+
154
+ return unhandled;
155
+ }
123
156
  }
124
-
125
- return (
126
- <text
127
- content={token.text}
128
- style={{
129
- fg: theme.markdownText,
130
- marginBottom: 1,
131
- marginLeft: 1 + indent,
132
- marginRight: 1,
133
- marginTop: 0,
134
- }}
135
- />
136
- );
137
157
  };
@@ -3,28 +3,25 @@ import type { Token } from "marked";
3
3
  import type { ReactNode } from "react";
4
4
 
5
5
  import { checkboxMarker } from "../../markdown-blocks.js";
6
- import type { FlatBlock } from "../../markdown-blocks.js";
6
+ import type { FlatListLineBlock } from "../../markdown-blocks.js";
7
7
  import { InlineTokens } from "../inline.js";
8
8
  import { linkMouseHandler } from "../links.js";
9
9
  import type { MarkdownLinkHandler } from "../links.js";
10
- import { hasMarkedText } from "../tokens.js";
11
10
 
12
11
  export const ListLineRenderer = function ListLineRenderer({
13
12
  block,
14
13
  theme,
15
14
  onLinkActivate,
16
15
  }: {
17
- block: FlatBlock;
16
+ block: FlatListLineBlock;
18
17
  theme: ResolvedTheme;
19
18
  onLinkActivate?: MarkdownLinkHandler;
20
19
  }): ReactNode {
21
20
  const { token, indent, bullet, checked } = block;
22
21
  const checkboxPrefix = checkboxMarker(checked);
23
22
 
24
- const inlineTokens: Token[] =
25
- "tokens" in token && Array.isArray(token.tokens) ? token.tokens : [];
26
-
27
- const tokenText = hasMarkedText(token) ? token.text : "";
23
+ const inlineTokens: Token[] = token.tokens ?? [];
24
+ const tokenText = token.text;
28
25
  const hasContent = inlineTokens.length > 0 || tokenText.length > 0;
29
26
 
30
27
  let content: ReactNode = null;
@@ -1,71 +1,99 @@
1
- import { bold as boldChunk } from "@opentui/core";
2
- import type { TextTableCellContent, TextTableContent } from "@opentui/core";
3
- import { useTheme } from "@tooee/themes";
1
+ import { StyledText } from "@opentui/core";
2
+ import type { ResolvedTheme } from "@tooee/themes";
4
3
  import type { Tokens } from "marked";
5
4
  import { useMemo } from "react";
6
5
  import type { ReactNode } from "react";
7
6
 
8
- import { inlineTokensToChunks } from "../chunks.js";
9
7
  import type { MarkdownLinkHandler } from "../links.js";
10
- import { getPlainText } from "../plain-text.js";
11
- import { tableLinkMouseHandler } from "../table-links.js";
8
+ import {
9
+ tableBorderColumns,
10
+ tableBorderLine,
11
+ tableCellChunks,
12
+ tableColumnWidths,
13
+ tableRowCells,
14
+ } from "../table-layout.js";
15
+ import { tableCellLinkMouseHandler } from "../table-links.js";
12
16
 
13
- export const MarkdownTableRenderer = function MarkdownTableRenderer({
14
- token,
15
- indent,
16
- onLinkActivate,
17
- }: {
18
- token: Tokens.Table;
17
+ /** Columns of the block margins: one plus the indent on the left, one on the right. */
18
+ const tableMarginColumns = function tableMarginColumns(indent: number): number {
19
+ return 1 + indent + 1;
20
+ };
21
+
22
+ interface TableRowProps {
23
+ table: Tokens.Table;
24
+ /** `0` is the header row. Body rows start at `1`. */
25
+ index: number;
19
26
  indent: number;
27
+ theme: ResolvedTheme;
28
+ /** Columns the Markdown view gives each block, before margins. */
29
+ contentWidth: number;
20
30
  onLinkActivate?: MarkdownLinkHandler;
21
- }): ReactNode {
22
- const { theme } = useTheme();
23
-
24
- const content: TextTableContent = useMemo(() => {
25
- const headerRow: TextTableCellContent[] = token.header.map((cell) => {
26
- const chunks = inlineTokensToChunks(cell.tokens, theme);
27
-
28
- return chunks.length > 0
29
- ? chunks.map((item) => boldChunk(item))
30
- : [boldChunk(getPlainText(cell.tokens).trim())];
31
- });
32
-
33
- const dataRows = token.rows.map((row) =>
34
- row.map((cell) => {
35
- const chunks = inlineTokensToChunks(cell.tokens, theme);
31
+ }
36
32
 
37
- if (chunks.length > 0) {
38
- return chunks;
39
- }
40
-
41
- const fallbackCell: TextTableCellContent = [
42
- { __isChunk: true, text: getPlainText(cell.tokens) },
43
- ];
33
+ /**
34
+ * One Markdown table row as one document row. Every row of a table gets the
35
+ * same column widths from `tableColumnWidths`, so the rows line up as one
36
+ * grid. The header row draws the top border and the header separator. Each
37
+ * row draws the line under it, and the last row draws the bottom border.
38
+ */
39
+ export const MarkdownTableRowRenderer = function MarkdownTableRowRenderer({
40
+ table,
41
+ index,
42
+ indent,
43
+ theme,
44
+ contentWidth,
45
+ onLinkActivate,
46
+ }: TableRowProps): ReactNode {
47
+ const header = index === 0;
48
+ const last = index === table.rows.length;
44
49
 
45
- return fallbackCell;
46
- })
47
- );
50
+ const available =
51
+ contentWidth - tableMarginColumns(indent) - tableBorderColumns(table.header.length);
48
52
 
49
- return [headerRow, ...dataRows];
50
- }, [token, theme]);
53
+ const widths = tableColumnWidths(table, available, theme);
51
54
 
52
- const onMouseDown = useMemo(
53
- () => tableLinkMouseHandler(content, onLinkActivate),
54
- [content, onLinkActivate]
55
+ const cells = useMemo(
56
+ () => tableRowCells(table, index).map((cell) => tableCellChunks(cell, theme, header)),
57
+ [table, index, theme, header]
55
58
  );
56
59
 
57
60
  return (
58
- <box style={{ marginBottom: 1, marginLeft: 1 + indent, marginRight: 1 }}>
59
- <text-table
60
- content={content}
61
- wrapMode="word"
62
- columnWidthMode="content"
63
- cellPadding={0}
64
- border={true}
65
- borderStyle="single"
66
- borderColor={theme.border}
67
- fg={theme.text}
68
- onMouseDown={onMouseDown}
61
+ <box
62
+ style={{
63
+ flexDirection: "column",
64
+ marginBottom: last ? 1 : 0,
65
+ marginLeft: 1 + indent,
66
+ marginRight: 1,
67
+ }}
68
+ >
69
+ {header ? <text content={tableBorderLine(widths, "┌", "┬", "┐")} fg={theme.border} /> : null}
70
+ <box style={{ flexDirection: "row" }}>
71
+ {widths.map((width, column): ReactNode => {
72
+ const chunks = cells[column] ?? [];
73
+
74
+ return (
75
+ <box
76
+ key={column}
77
+ border={column === 0 ? ["left", "right"] : ["right"]}
78
+ borderStyle="single"
79
+ borderColor={theme.border}
80
+ style={{ flexShrink: 0, width: width + (column === 0 ? 2 : 1) }}
81
+ >
82
+ <text
83
+ content={new StyledText(chunks)}
84
+ wrapMode="word"
85
+ fg={theme.text}
86
+ onMouseDown={tableCellLinkMouseHandler(chunks, onLinkActivate)}
87
+ />
88
+ </box>
89
+ );
90
+ })}
91
+ </box>
92
+ <text
93
+ content={
94
+ last ? tableBorderLine(widths, "└", "┴", "┘") : tableBorderLine(widths, "├", "┼", "┤")
95
+ }
96
+ fg={theme.border}
69
97
  />
70
98
  </box>
71
99
  );
@@ -110,10 +110,40 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
110
110
  return found;
111
111
  };
112
112
 
113
- export const linkMouseHandler = function linkMouseHandler(
114
- tokens: readonly Token[],
115
- onLinkActivate: MarkdownLinkHandler | undefined,
116
- initialColumn = 0
113
+ /**
114
+ * The source line and column under the pointer in a text renderable, or `null`
115
+ * when the event did not hit a text buffer line.
116
+ */
117
+ export const textPositionAt = function textPositionAt(
118
+ event: MouseEvent
119
+ ): InlineLinkPosition | null {
120
+ const { target } = event;
121
+
122
+ if (target === null || target === undefined || !("lineInfo" in target)) {
123
+ return null;
124
+ }
125
+
126
+ // SAFETY: OpenTUI supplies a renderable target, and the lineInfo guard proves this text-buffer member.
127
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- OpenTUI supplies renderable mouse targets
128
+ const text = target as TextBufferRenderable;
129
+ const visualLine = event.y - text.y;
130
+ const sourceLine = text.lineInfo.lineSources[visualLine];
131
+ const startColumn = text.lineInfo.lineStartCols[visualLine];
132
+
133
+ if (sourceLine === undefined || startColumn === undefined) {
134
+ return null;
135
+ }
136
+
137
+ return { column: startColumn + event.x - text.x, line: sourceLine };
138
+ };
139
+
140
+ /**
141
+ * Primary-click handler that activates the link `hrefAt` finds under the
142
+ * pointer. It consumes the event only when the host handler returns literal true.
143
+ */
144
+ export const positionLinkMouseHandler = function positionLinkMouseHandler(
145
+ hrefAt: (position: InlineLinkPosition) => string | null,
146
+ onLinkActivate: MarkdownLinkHandler | undefined
117
147
  ): ((event: MouseEvent) => void) | undefined {
118
148
  if (onLinkActivate === undefined) {
119
149
  return undefined;
@@ -124,31 +154,8 @@ export const linkMouseHandler = function linkMouseHandler(
124
154
  return;
125
155
  }
126
156
 
127
- const { target } = event;
128
-
129
- if (target === null || target === undefined || !("lineInfo" in target)) {
130
- return;
131
- }
132
-
133
- // SAFETY: OpenTUI supplies a renderable target, and the lineInfo guard proves this text-buffer member.
134
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- OpenTUI supplies renderable mouse targets
135
- const text = target as TextBufferRenderable;
136
- const visualLine = event.y - text.y;
137
- const sourceLine = text.lineInfo.lineSources[visualLine];
138
- const startColumn = text.lineInfo.lineStartCols[visualLine];
139
-
140
- if (sourceLine === undefined || startColumn === undefined) {
141
- return;
142
- }
143
-
144
- const href = inlineLinkAtPosition(
145
- tokens,
146
- {
147
- column: startColumn + event.x - text.x,
148
- line: sourceLine,
149
- },
150
- initialColumn
151
- );
157
+ const position = textPositionAt(event);
158
+ const href = position === null ? null : hrefAt(position);
152
159
 
153
160
  if (href === null || onLinkActivate(href) !== true) {
154
161
  return;
@@ -158,3 +165,14 @@ export const linkMouseHandler = function linkMouseHandler(
158
165
  event.stopPropagation();
159
166
  };
160
167
  };
168
+
169
+ export const linkMouseHandler = function linkMouseHandler(
170
+ tokens: readonly Token[],
171
+ onLinkActivate: MarkdownLinkHandler | undefined,
172
+ initialColumn = 0
173
+ ): ((event: MouseEvent) => void) | undefined {
174
+ return positionLinkMouseHandler(
175
+ (position) => inlineLinkAtPosition(tokens, position, initialColumn),
176
+ onLinkActivate
177
+ );
178
+ };