@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
@@ -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,85 +69,89 @@ export const FlatBlockRenderer = function FlatBlockRenderer({
40
69
  onLinkActivate?: MarkdownLinkHandler;
41
70
  imageBasePath?: string;
42
71
  }): ReactNode {
43
- const { token, indent, bullet } = block;
72
+ const { indent } = block;
44
73
 
45
- if (bullet !== undefined) {
46
- return <ListLineRenderer block={block} theme={theme} onLinkActivate={onLinkActivate} />;
47
- }
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
+ }
48
87
 
49
- const heading = narrowToken(token, "heading");
50
- if (heading !== null) {
51
- return (
52
- <HeadingRenderer
53
- token={heading}
54
- theme={theme}
55
- indent={indent}
56
- hiddenCount={hiddenCount}
57
- onLinkActivate={onLinkActivate}
58
- />
59
- );
60
- }
61
- const paragraph = narrowToken(token, "paragraph");
62
- if (paragraph !== null) {
63
- return (
64
- <ParagraphRenderer
65
- token={paragraph}
66
- theme={theme}
67
- indent={indent}
68
- onLinkActivate={onLinkActivate}
69
- imageBasePath={imageBasePath}
70
- />
71
- );
72
- }
73
- const code = narrowToken(token, "code");
74
- if (code !== null) {
75
- return (
76
- <CodeBlock
77
- token={code}
78
- blockIndex={blockIndex}
79
- theme={theme}
80
- syntax={syntax}
81
- indent={indent}
82
- contentWidth={contentWidth}
83
- hScrollableBlocksRef={hScrollableBlocksRef}
84
- renderers={codeBlockRenderers}
85
- />
86
- );
87
- }
88
- const blockquote = narrowToken(token, "blockquote");
89
- if (blockquote !== null) {
90
- return (
91
- <BlockquoteRenderer
92
- token={blockquote}
93
- theme={theme}
94
- indent={indent}
95
- onLinkActivate={onLinkActivate}
96
- />
97
- );
98
- }
99
- const table = narrowToken(token, "table");
100
- if (table !== null) {
101
- return <MarkdownTableRenderer token={table} indent={indent} onLinkActivate={onLinkActivate} />;
102
- }
103
- if (token.type === "hr") {
104
- return <HorizontalRule theme={theme} indent={indent} />;
105
- }
106
- if (token.type === "space" || token.type === "html") {
107
- return null;
108
- }
109
- if (!hasMarkedText(token)) {
110
- return null;
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
+ }
111
156
  }
112
- return (
113
- <text
114
- content={token.text}
115
- style={{
116
- fg: theme.markdownText,
117
- marginBottom: 1,
118
- marginLeft: 1 + indent,
119
- marginRight: 1,
120
- marginTop: 0,
121
- }}
122
- />
123
- );
124
157
  };
@@ -33,6 +33,7 @@ export const HeadingRenderer = function HeadingRenderer({
33
33
  [5, theme.textMuted],
34
34
  [6, theme.textMuted],
35
35
  ]);
36
+
36
37
  const prefixes = new Map<number, string>([
37
38
  [1, "# "],
38
39
  [2, "## "],
@@ -3,29 +3,29 @@ 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
- const inlineTokens: Token[] =
24
- "tokens" in token && Array.isArray(token.tokens) ? token.tokens : [];
25
- const tokenText = hasMarkedText(token) ? token.text : "";
22
+
23
+ const inlineTokens: Token[] = token.tokens ?? [];
24
+ const tokenText = token.text;
26
25
  const hasContent = inlineTokens.length > 0 || tokenText.length > 0;
27
26
 
28
27
  let content: ReactNode = null;
28
+
29
29
  if (inlineTokens.length > 0) {
30
30
  content = <InlineTokens tokens={inlineTokens} theme={theme} />;
31
31
  } else if (tokenText.length > 0) {
@@ -63,6 +63,7 @@ export const ParagraphRenderer = function ParagraphRenderer({
63
63
  imageBasePath?: string;
64
64
  }): ReactNode {
65
65
  const segments = splitMarkdownImages(token.tokens);
66
+
66
67
  return (
67
68
  <box
68
69
  style={{
@@ -1,64 +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();
31
+ }
23
32
 
24
- const content: TextTableContent = useMemo(() => {
25
- const headerRow: TextTableCellContent[] = token.header.map((cell) => {
26
- const chunks = inlineTokensToChunks(cell.tokens, theme);
27
- return chunks.length > 0
28
- ? chunks.map((item) => boldChunk(item))
29
- : [boldChunk(getPlainText(cell.tokens).trim())];
30
- });
31
- const dataRows = token.rows.map((row) =>
32
- row.map((cell) => {
33
- const chunks = inlineTokensToChunks(cell.tokens, theme);
34
- if (chunks.length > 0) {
35
- return chunks;
36
- }
37
- const fallbackCell: TextTableCellContent = [
38
- { __isChunk: true, text: getPlainText(cell.tokens) },
39
- ];
40
- return fallbackCell;
41
- })
42
- );
43
- return [headerRow, ...dataRows];
44
- }, [token, theme]);
45
- const onMouseDown = useMemo(
46
- () => tableLinkMouseHandler(content, onLinkActivate),
47
- [content, onLinkActivate]
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;
49
+
50
+ const available =
51
+ contentWidth - tableMarginColumns(indent) - tableBorderColumns(table.header.length);
52
+
53
+ const widths = tableColumnWidths(table, available, theme);
54
+
55
+ const cells = useMemo(
56
+ () => tableRowCells(table, index).map((cell) => tableCellChunks(cell, theme, header)),
57
+ [table, index, theme, header]
48
58
  );
49
59
 
50
60
  return (
51
- <box style={{ marginBottom: 1, marginLeft: 1 + indent, marginRight: 1 }}>
52
- <text-table
53
- content={content}
54
- wrapMode="word"
55
- columnWidthMode="content"
56
- cellPadding={0}
57
- border={true}
58
- borderStyle="single"
59
- borderColor={theme.border}
60
- fg={theme.text}
61
- 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}
62
97
  />
63
98
  </box>
64
99
  );
@@ -23,8 +23,10 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
23
23
 
24
24
  const advanceText = (value: string, href?: string): void => {
25
25
  const parts = value.split("\n");
26
+
26
27
  for (const [index, part] of parts.entries()) {
27
28
  const width = Bun.stringWidth(part);
29
+
28
30
  if (
29
31
  href !== undefined &&
30
32
  line === position.line &&
@@ -33,7 +35,9 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
33
35
  ) {
34
36
  found = href;
35
37
  }
38
+
36
39
  column += width;
40
+
37
41
  if (index < parts.length - 1) {
38
42
  line += 1;
39
43
  column = 0;
@@ -46,38 +50,51 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
46
50
  if (found !== null) {
47
51
  return;
48
52
  }
53
+
49
54
  const text = narrowToken(token, "text");
55
+
50
56
  if (text !== null) {
51
57
  advanceText(normalizeSoftLineEndings(text.text), href);
52
58
  continue;
53
59
  }
60
+
54
61
  const link = narrowToken(token, "link");
62
+
55
63
  if (link !== null) {
56
64
  visit(link.tokens, link.href);
57
65
  continue;
58
66
  }
67
+
59
68
  const strong = narrowToken(token, "strong");
69
+
60
70
  if (strong !== null) {
61
71
  visit(strong.tokens, href);
62
72
  continue;
63
73
  }
74
+
64
75
  const em = narrowToken(token, "em");
76
+
65
77
  if (em !== null) {
66
78
  visit(em.tokens, href);
67
79
  continue;
68
80
  }
81
+
69
82
  const del = narrowToken(token, "del");
83
+
70
84
  if (del !== null) {
71
85
  advanceText("~", href);
72
86
  visit(del.tokens, href);
73
87
  advanceText("~", href);
74
88
  continue;
75
89
  }
90
+
76
91
  const codespan = narrowToken(token, "codespan");
92
+
77
93
  if (codespan !== null) {
78
94
  advanceText(` ${codespan.text} `, href);
79
95
  continue;
80
96
  }
97
+
81
98
  if (narrowToken(token, "br") !== null) {
82
99
  advanceText("\n", href);
83
100
  } else if (narrowToken(token, "space") !== null) {
@@ -89,46 +106,73 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
89
106
  };
90
107
 
91
108
  visit(tokens);
109
+
92
110
  return found;
93
111
  };
94
112
 
95
- export const linkMouseHandler = function linkMouseHandler(
96
- tokens: readonly Token[],
97
- onLinkActivate: MarkdownLinkHandler | undefined,
98
- 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
99
147
  ): ((event: MouseEvent) => void) | undefined {
100
148
  if (onLinkActivate === undefined) {
101
149
  return undefined;
102
150
  }
151
+
103
152
  return (event: MouseEvent): void => {
104
153
  if (event.button !== 0) {
105
154
  return;
106
155
  }
107
- const { target } = event;
108
- if (target === null || target === undefined || !("lineInfo" in target)) {
109
- return;
110
- }
111
- // SAFETY: OpenTUI supplies a renderable target, and the lineInfo guard proves this text-buffer member.
112
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- OpenTUI supplies renderable mouse targets
113
- const text = target as TextBufferRenderable;
114
- const visualLine = event.y - text.y;
115
- const sourceLine = text.lineInfo.lineSources[visualLine];
116
- const startColumn = text.lineInfo.lineStartCols[visualLine];
117
- if (sourceLine === undefined || startColumn === undefined) {
118
- return;
119
- }
120
- const href = inlineLinkAtPosition(
121
- tokens,
122
- {
123
- column: startColumn + event.x - text.x,
124
- line: sourceLine,
125
- },
126
- initialColumn
127
- );
156
+
157
+ const position = textPositionAt(event);
158
+ const href = position === null ? null : hrefAt(position);
159
+
128
160
  if (href === null || onLinkActivate(href) !== true) {
129
161
  return;
130
162
  }
163
+
131
164
  event.preventDefault();
132
165
  event.stopPropagation();
133
166
  };
134
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
+ };
@@ -7,16 +7,21 @@ export const getPlainText = function getPlainText(tokens: readonly Token[]): str
7
7
  return tokens
8
8
  .map((token) => {
9
9
  const text = narrowToken(token, "text");
10
+
10
11
  if (text !== null) {
11
12
  return text.text;
12
13
  }
14
+
13
15
  const codespan = narrowToken(token, "codespan");
16
+
14
17
  if (codespan !== null) {
15
18
  return codespan.text;
16
19
  }
20
+
17
21
  if ("tokens" in token && Array.isArray(token.tokens)) {
18
22
  return getPlainText(token.tokens);
19
23
  }
24
+
20
25
  return hasMarkedText(token) ? token.text : "";
21
26
  })
22
27
  .join("");