@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.
- package/dist/chooser/choose-filter.d.ts.map +1 -1
- package/dist/chooser/choose-filter.js.map +1 -1
- package/dist/chooser/choose-highlighted-text.d.ts.map +1 -1
- package/dist/chooser/choose-highlighted-text.js.map +1 -1
- package/dist/chooser/choose-list.d.ts.map +1 -1
- package/dist/chooser/choose-list.js.map +1 -1
- package/dist/chooser/choose-store.d.ts +3 -3
- package/dist/chooser/choose-store.d.ts.map +1 -1
- package/dist/chooser/choose-store.js.map +1 -1
- package/dist/chooser/chooser.d.ts.map +1 -1
- package/dist/chooser/chooser.js.map +1 -1
- package/dist/chooser/source.d.ts.map +1 -1
- package/dist/chooser/source.js.map +1 -1
- package/dist/chooser/use-choose.d.ts.map +1 -1
- package/dist/chooser/use-choose.js.map +1 -1
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js.map +1 -1
- package/dist/command-palette.d.ts.map +1 -1
- package/dist/command-palette.js.map +1 -1
- package/dist/context-menu.d.ts.map +1 -1
- package/dist/context-menu.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/markdown/blocks/blockquote.d.ts.map +1 -1
- package/dist/markdown/blocks/blockquote.js.map +1 -1
- package/dist/markdown/blocks/flat-block.d.ts.map +1 -1
- package/dist/markdown/blocks/flat-block.js +38 -33
- package/dist/markdown/blocks/flat-block.js.map +1 -1
- package/dist/markdown/blocks/heading.d.ts.map +1 -1
- package/dist/markdown/blocks/heading.js.map +1 -1
- package/dist/markdown/blocks/list-line.d.ts +2 -2
- package/dist/markdown/blocks/list-line.d.ts.map +1 -1
- package/dist/markdown/blocks/list-line.js +2 -3
- package/dist/markdown/blocks/list-line.js.map +1 -1
- package/dist/markdown/blocks/paragraph.d.ts.map +1 -1
- package/dist/markdown/blocks/paragraph.js.map +1 -1
- package/dist/markdown/blocks/table.d.ts +17 -3
- package/dist/markdown/blocks/table.d.ts.map +1 -1
- package/dist/markdown/blocks/table.js +29 -29
- package/dist/markdown/blocks/table.js.map +1 -1
- package/dist/markdown/links.d.ts +10 -0
- package/dist/markdown/links.d.ts.map +1 -1
- package/dist/markdown/links.js +30 -18
- package/dist/markdown/links.js.map +1 -1
- package/dist/markdown/plain-text.d.ts.map +1 -1
- package/dist/markdown/plain-text.js.map +1 -1
- package/dist/markdown/table-layout.d.ts +28 -0
- package/dist/markdown/table-layout.d.ts.map +1 -0
- package/dist/markdown/table-layout.js +100 -0
- package/dist/markdown/table-layout.js.map +1 -0
- package/dist/markdown/table-links.d.ts +5 -4
- package/dist/markdown/table-links.d.ts.map +1 -1
- package/dist/markdown/table-links.js +6 -23
- package/dist/markdown/table-links.js.map +1 -1
- package/dist/markdown/tokens.d.ts +2 -0
- package/dist/markdown/tokens.d.ts.map +1 -1
- package/dist/markdown/tokens.js.map +1 -1
- package/dist/markdown/visit-inline.d.ts.map +1 -1
- package/dist/markdown/visit-inline.js.map +1 -1
- package/dist/markdown-blocks.d.ts +72 -14
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +137 -77
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/markdown-images.d.ts.map +1 -1
- package/dist/markdown-images.js +5 -8
- package/dist/markdown-images.js.map +1 -1
- package/dist/markdown-view.d.ts +0 -1
- package/dist/markdown-view.d.ts.map +1 -1
- package/dist/markdown-view.js +0 -1
- package/dist/markdown-view.js.map +1 -1
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +8 -4
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js.map +1 -1
- package/dist/row-document-renderable.d.ts.map +1 -1
- package/dist/row-document-renderable.js.map +1 -1
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js.map +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js.map +1 -1
- package/package.json +6 -6
- package/src/chooser/choose-filter.tsx +1 -0
- package/src/chooser/choose-highlighted-text.tsx +2 -0
- package/src/chooser/choose-list.tsx +6 -0
- package/src/chooser/choose-store.ts +20 -3
- package/src/chooser/chooser.tsx +2 -0
- package/src/chooser/source.ts +5 -0
- package/src/chooser/use-choose.ts +44 -0
- package/src/code-blocks.tsx +14 -0
- package/src/command-palette.tsx +4 -0
- package/src/context-menu.tsx +13 -0
- package/src/index.ts +56 -1
- package/src/markdown/blocks/blockquote.tsx +2 -0
- package/src/markdown/blocks/flat-block.tsx +113 -80
- package/src/markdown/blocks/heading.tsx +1 -0
- package/src/markdown/blocks/list-line.tsx +6 -6
- package/src/markdown/blocks/paragraph.tsx +1 -0
- package/src/markdown/blocks/table.tsx +84 -49
- package/src/markdown/links.ts +69 -25
- package/src/markdown/plain-text.ts +5 -0
- package/src/markdown/table-layout.ts +156 -0
- package/src/markdown/table-links.ts +17 -25
- package/src/markdown/tokens.ts +4 -0
- package/src/markdown/visit-inline.ts +20 -0
- package/src/markdown-blocks.ts +298 -100
- package/src/markdown-images.ts +30 -9
- package/src/markdown-view.tsx +4 -1
- package/src/mermaid.ts +21 -4
- package/src/parsers.ts +39 -0
- package/src/row-document-renderable.ts +53 -0
- package/src/source.ts +18 -0
- package/src/table.tsx +32 -0
- package/dist/text-table-internals.d.ts +0 -34
- package/dist/text-table-internals.d.ts.map +0 -1
- package/dist/text-table-internals.js +0 -68
- package/dist/text-table-internals.js.map +0 -1
- package/dist/text-table.d.ts +0 -7
- package/dist/text-table.d.ts.map +0 -1
- package/dist/text-table.js +0 -4
- package/dist/text-table.js.map +0 -1
- package/src/text-table-internals.ts +0 -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
|
|
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 {
|
|
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 {
|
|
72
|
+
const { indent } = block;
|
|
44
73
|
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
};
|
|
@@ -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 {
|
|
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:
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
const tokenText =
|
|
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) {
|
|
@@ -1,64 +1,99 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
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 {
|
|
11
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
indent
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
}
|
|
22
|
-
const { theme } = useTheme();
|
|
31
|
+
}
|
|
23
32
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
);
|
package/src/markdown/links.ts
CHANGED
|
@@ -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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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("");
|