@tooee/renderers 0.10.1 → 0.10.3
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 +5 -2
- package/dist/chooser/choose-list.js.map +1 -1
- 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/fuzzy.d.ts.map +1 -1
- package/dist/chooser/fuzzy.js.map +1 -1
- package/dist/chooser/use-choose.d.ts.map +1 -1
- package/dist/chooser/use-choose.js +2 -2
- package/dist/chooser/use-choose.js.map +1 -1
- package/dist/code-blocks.d.ts +2 -2
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js.map +1 -1
- package/dist/code-view.d.ts +1 -1
- package/dist/code-view.d.ts.map +1 -1
- package/dist/code-view.js +1 -1
- package/dist/code-view.js.map +1 -1
- package/dist/command-palette.d.ts.map +1 -1
- package/dist/command-palette.js +2 -2
- package/dist/command-palette.js.map +1 -1
- package/dist/context-menu.d.ts.map +1 -1
- package/dist/context-menu.js +1 -1
- package/dist/context-menu.js.map +1 -1
- package/dist/document-bindings.d.ts +1 -1
- package/dist/document-bindings.d.ts.map +1 -1
- package/dist/markdown/blocks/blockquote.d.ts +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 +1 -1
- package/dist/markdown/blocks/flat-block.d.ts.map +1 -1
- package/dist/markdown/blocks/flat-block.js +2 -2
- package/dist/markdown/blocks/flat-block.js.map +1 -1
- package/dist/markdown/blocks/heading.d.ts +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/horizontal-rule.d.ts +1 -1
- package/dist/markdown/blocks/horizontal-rule.d.ts.map +1 -1
- package/dist/markdown/blocks/list-line.d.ts +1 -1
- package/dist/markdown/blocks/list-line.d.ts.map +1 -1
- package/dist/markdown/blocks/list-line.js.map +1 -1
- package/dist/markdown/blocks/paragraph.d.ts +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 +3 -1
- package/dist/markdown/blocks/table.d.ts.map +1 -1
- package/dist/markdown/blocks/table.js +5 -3
- package/dist/markdown/blocks/table.js.map +1 -1
- package/dist/markdown/chunks.d.ts +1 -1
- package/dist/markdown/chunks.d.ts.map +1 -1
- package/dist/markdown/chunks.js +1 -1
- package/dist/markdown/chunks.js.map +1 -1
- package/dist/markdown/inline.d.ts +1 -1
- package/dist/markdown/inline.d.ts.map +1 -1
- package/dist/markdown/inline.js.map +1 -1
- package/dist/markdown/links.d.ts +1 -1
- package/dist/markdown/links.d.ts.map +1 -1
- 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-links.d.ts +10 -0
- package/dist/markdown/table-links.d.ts.map +1 -0
- package/dist/markdown/table-links.js +47 -0
- package/dist/markdown/table-links.js.map +1 -0
- 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 +2 -0
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +3 -1
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/markdown-images.d.ts.map +1 -1
- package/dist/markdown-images.js.map +1 -1
- package/dist/markdown-view.d.ts +1 -1
- package/dist/markdown-view.d.ts.map +1 -1
- package/dist/markdown-view.js +1 -1
- package/dist/markdown-view.js.map +1 -1
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +1 -1
- package/dist/mermaid.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/row-document.d.ts.map +1 -1
- package/dist/row-document.js.map +1 -1
- package/dist/table.d.ts +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js +1 -1
- package/dist/table.js.map +1 -1
- package/dist/text-table-internals.d.ts +34 -0
- package/dist/text-table-internals.d.ts.map +1 -0
- package/dist/text-table-internals.js +68 -0
- package/dist/text-table-internals.js.map +1 -0
- package/dist/text-table.d.ts.map +1 -1
- package/dist/text-table.js +1 -1
- package/dist/text-table.js.map +1 -1
- package/dist/use-gutter-palette.d.ts.map +1 -1
- package/dist/use-gutter-palette.js +1 -1
- package/dist/use-gutter-palette.js.map +1 -1
- package/package.json +15 -15
- package/src/chooser/choose-filter.tsx +2 -1
- package/src/chooser/choose-highlighted-text.tsx +5 -7
- package/src/chooser/choose-list.tsx +7 -4
- package/src/chooser/choose-store.ts +2 -1
- package/src/chooser/chooser.tsx +2 -1
- package/src/chooser/fuzzy.ts +1 -0
- package/src/chooser/source.ts +1 -1
- package/src/chooser/use-choose.ts +11 -10
- package/src/code-blocks.tsx +6 -5
- package/src/code-view.tsx +3 -2
- package/src/command-palette.tsx +5 -4
- package/src/context-menu.tsx +29 -31
- package/src/document-bindings.ts +2 -1
- package/src/markdown/blocks/blockquote.tsx +2 -1
- package/src/markdown/blocks/flat-block.tsx +5 -4
- package/src/markdown/blocks/heading.tsx +3 -2
- package/src/markdown/blocks/horizontal-rule.tsx +1 -1
- package/src/markdown/blocks/list-line.tsx +3 -2
- package/src/markdown/blocks/paragraph.tsx +14 -14
- package/src/markdown/blocks/table.tsx +14 -4
- package/src/markdown/chunks.ts +7 -4
- package/src/markdown/inline.tsx +2 -1
- package/src/markdown/links.ts +5 -4
- package/src/markdown/plain-text.ts +1 -0
- package/src/markdown/table-links.ts +58 -0
- package/src/markdown/tokens.ts +1 -1
- package/src/markdown/visit-inline.ts +2 -1
- package/src/markdown-blocks.ts +8 -5
- package/src/markdown-images.ts +5 -4
- package/src/markdown-view.tsx +20 -21
- package/src/mermaid.ts +9 -9
- package/src/parsers.ts +1 -1
- package/src/row-document-renderable.ts +8 -7
- package/src/row-document.ts +1 -0
- package/src/source.ts +1 -1
- package/src/table.tsx +54 -59
- package/src/text-table-internals.ts +124 -0
- package/src/text-table.ts +1 -1
- package/src/use-gutter-palette.ts +3 -2
package/src/context-menu.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { useState, useCallback } from "react";
|
|
2
1
|
import { useTerminalDimensions } from "@opentui/react";
|
|
3
2
|
import { useCommand } from "@tooee/commands";
|
|
4
3
|
import { useTheme } from "@tooee/themes";
|
|
4
|
+
import { useState, useCallback } from "react";
|
|
5
5
|
import type { ReactNode } from "react";
|
|
6
6
|
|
|
7
7
|
export interface ContextMenuEntry {
|
|
@@ -49,7 +49,7 @@ export const ContextMenu = function ContextMenu({
|
|
|
49
49
|
onSelect(entry.id);
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
|
-
[entries, onSelect]
|
|
52
|
+
[entries, onSelect]
|
|
53
53
|
);
|
|
54
54
|
|
|
55
55
|
const moveUp = useCallback(() => {
|
|
@@ -160,35 +160,33 @@ export const ContextMenu = function ContextMenu({
|
|
|
160
160
|
<text content="No actions" fg={theme.textMuted} />
|
|
161
161
|
</box>
|
|
162
162
|
) : (
|
|
163
|
-
entries.map(
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
{
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
),
|
|
191
|
-
)
|
|
163
|
+
entries.map((entry, i): ReactNode => (
|
|
164
|
+
<box
|
|
165
|
+
key={entry.id}
|
|
166
|
+
flexDirection="row"
|
|
167
|
+
height={1}
|
|
168
|
+
paddingLeft={1}
|
|
169
|
+
paddingRight={1}
|
|
170
|
+
backgroundColor={i === activeIndex ? theme.backgroundElement : undefined}
|
|
171
|
+
onMouseDown={(event) => {
|
|
172
|
+
if (event.button !== 0) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
event.preventDefault();
|
|
176
|
+
event.stopPropagation();
|
|
177
|
+
onSelect(entry.id);
|
|
178
|
+
}}
|
|
179
|
+
>
|
|
180
|
+
<text
|
|
181
|
+
content={entry.title}
|
|
182
|
+
fg={i === activeIndex ? theme.primary : theme.text}
|
|
183
|
+
style={{ flexGrow: 1 }}
|
|
184
|
+
/>
|
|
185
|
+
{(entry.hotkey?.length ?? 0) > 0 && (
|
|
186
|
+
<text content={` ${entry.hotkey}`} fg={theme.textMuted} />
|
|
187
|
+
)}
|
|
188
|
+
</box>
|
|
189
|
+
))
|
|
192
190
|
)}
|
|
193
191
|
</box>
|
|
194
192
|
</box>
|
package/src/document-bindings.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { RefObject } from "react";
|
|
2
1
|
import type { MouseEvent } from "@opentui/core";
|
|
2
|
+
import type { RefObject } from "react";
|
|
3
|
+
|
|
3
4
|
import type { DecorationLayer } from "./decoration-layer.js";
|
|
4
5
|
import type { RowDocumentRenderable } from "./row-document-renderable.js";
|
|
5
6
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { ResolvedTheme } from "@tooee/themes";
|
|
1
2
|
import type { Token, Tokens } from "marked";
|
|
2
3
|
import type { ReactNode } from "react";
|
|
3
|
-
|
|
4
|
+
|
|
4
5
|
import { InlineTokens } from "../inline.js";
|
|
5
6
|
import { linkMouseHandler } from "../links.js";
|
|
6
7
|
import type { MarkdownLinkHandler } from "../links.js";
|
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import type { ReactNode, RefObject } from "react";
|
|
2
1
|
import type { SyntaxStyle, TextBufferRenderable } from "@opentui/core";
|
|
3
2
|
import type { ResolvedTheme } from "@tooee/themes";
|
|
3
|
+
import type { ReactNode, RefObject } from "react";
|
|
4
|
+
|
|
4
5
|
import { CodeBlock } from "../../code-blocks.js";
|
|
5
6
|
import type { CodeBlockRenderer } from "../../code-blocks.js";
|
|
6
7
|
import type { FlatBlock } from "../../markdown-blocks.js";
|
|
8
|
+
import type { MarkdownLinkHandler } from "../links.js";
|
|
9
|
+
import { hasMarkedText, narrowToken } from "../tokens.js";
|
|
7
10
|
import { BlockquoteRenderer } from "./blockquote.js";
|
|
8
11
|
import { HeadingRenderer } from "./heading.js";
|
|
9
12
|
import { HorizontalRule } from "./horizontal-rule.js";
|
|
10
13
|
import { ListLineRenderer } from "./list-line.js";
|
|
11
14
|
import { ParagraphRenderer } from "./paragraph.js";
|
|
12
15
|
import { MarkdownTableRenderer } from "./table.js";
|
|
13
|
-
import type { MarkdownLinkHandler } from "../links.js";
|
|
14
|
-
import { hasMarkedText, narrowToken } from "../tokens.js";
|
|
15
16
|
|
|
16
17
|
type CodeBlockRendererRegistry = Record<string, CodeBlockRenderer>;
|
|
17
18
|
|
|
@@ -93,7 +94,7 @@ export const FlatBlockRenderer = function FlatBlockRenderer({
|
|
|
93
94
|
}
|
|
94
95
|
const table = narrowToken(token, "table");
|
|
95
96
|
if (table !== null) {
|
|
96
|
-
return <MarkdownTableRenderer token={table} indent={indent} />;
|
|
97
|
+
return <MarkdownTableRenderer token={table} indent={indent} onLinkActivate={onLinkActivate} />;
|
|
97
98
|
}
|
|
98
99
|
if (token.type === "hr") {
|
|
99
100
|
return <HorizontalRule theme={theme} indent={indent} />;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { ResolvedTheme } from "@tooee/themes";
|
|
1
2
|
import type { Tokens } from "marked";
|
|
2
3
|
import type { ReactNode } from "react";
|
|
3
|
-
|
|
4
|
+
|
|
4
5
|
import { InlineTokens } from "../inline.js";
|
|
5
6
|
import { linkMouseHandler } from "../links.js";
|
|
6
7
|
import type { MarkdownLinkHandler } from "../links.js";
|
|
@@ -40,7 +41,7 @@ export const HeadingRenderer = function HeadingRenderer({
|
|
|
40
41
|
onMouseDown={linkMouseHandler(
|
|
41
42
|
token.tokens,
|
|
42
43
|
onLinkActivate,
|
|
43
|
-
Bun.stringWidth(prefixes.get(token.depth) ?? "")
|
|
44
|
+
Bun.stringWidth(prefixes.get(token.depth) ?? "")
|
|
44
45
|
)}
|
|
45
46
|
>
|
|
46
47
|
<span fg={theme.textMuted}>{prefixes.get(token.depth)}</span>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { ResolvedTheme } from "@tooee/themes";
|
|
1
2
|
import type { Token } from "marked";
|
|
2
3
|
import type { ReactNode } from "react";
|
|
3
|
-
|
|
4
|
+
|
|
4
5
|
import { checkboxMarker } from "../../markdown-blocks.js";
|
|
5
6
|
import type { FlatBlock } from "../../markdown-blocks.js";
|
|
6
7
|
import { InlineTokens } from "../inline.js";
|
|
@@ -38,7 +39,7 @@ export const ListLineRenderer = function ListLineRenderer({
|
|
|
38
39
|
onMouseDown={linkMouseHandler(
|
|
39
40
|
inlineTokens,
|
|
40
41
|
onLinkActivate,
|
|
41
|
-
Bun.stringWidth(`${bullet}${checkboxPrefix}`)
|
|
42
|
+
Bun.stringWidth(`${bullet}${checkboxPrefix}`)
|
|
42
43
|
)}
|
|
43
44
|
>
|
|
44
45
|
<span fg={theme.markdownListItem}>{bullet}</span>
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { ResolvedTheme } from "@tooee/themes";
|
|
1
2
|
import type { Tokens } from "marked";
|
|
2
3
|
import { useEffect, useState } from "react";
|
|
3
4
|
import type { ReactNode } from "react";
|
|
4
|
-
|
|
5
|
+
|
|
5
6
|
import { resolveMarkdownImageSource, splitMarkdownImages } from "../../markdown-images.js";
|
|
6
7
|
import type { MarkdownImageEmbed } from "../../markdown-images.js";
|
|
7
8
|
import { InlineTokens } from "../inline.js";
|
|
@@ -71,19 +72,18 @@ export const ParagraphRenderer = function ParagraphRenderer({
|
|
|
71
72
|
marginRight: 1,
|
|
72
73
|
}}
|
|
73
74
|
>
|
|
74
|
-
{segments.map(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
),
|
|
75
|
+
{segments.map((segment, index): ReactNode =>
|
|
76
|
+
segment.type === "image" ? (
|
|
77
|
+
<MarkdownImage key={index} image={segment} basePath={imageBasePath} theme={theme} />
|
|
78
|
+
) : (
|
|
79
|
+
<text
|
|
80
|
+
key={index}
|
|
81
|
+
style={{ fg: theme.markdownText }}
|
|
82
|
+
onMouseDown={linkMouseHandler(segment.tokens, onLinkActivate)}
|
|
83
|
+
>
|
|
84
|
+
<InlineTokens tokens={segment.tokens} theme={theme} />
|
|
85
|
+
</text>
|
|
86
|
+
)
|
|
87
87
|
)}
|
|
88
88
|
</box>
|
|
89
89
|
);
|
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
import type { Tokens } from "marked";
|
|
2
|
-
import { useMemo } from "react";
|
|
3
|
-
import type { ReactNode } from "react";
|
|
4
1
|
import { bold as boldChunk } from "@opentui/core";
|
|
5
2
|
import type { TextTableCellContent, TextTableContent } from "@opentui/core";
|
|
6
3
|
import { useTheme } from "@tooee/themes";
|
|
4
|
+
import type { Tokens } from "marked";
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
import type { ReactNode } from "react";
|
|
7
|
+
|
|
7
8
|
import { inlineTokensToChunks } from "../chunks.js";
|
|
9
|
+
import type { MarkdownLinkHandler } from "../links.js";
|
|
8
10
|
import { getPlainText } from "../plain-text.js";
|
|
11
|
+
import { tableLinkMouseHandler } from "../table-links.js";
|
|
9
12
|
|
|
10
13
|
export const MarkdownTableRenderer = function MarkdownTableRenderer({
|
|
11
14
|
token,
|
|
12
15
|
indent,
|
|
16
|
+
onLinkActivate,
|
|
13
17
|
}: {
|
|
14
18
|
token: Tokens.Table;
|
|
15
19
|
indent: number;
|
|
20
|
+
onLinkActivate?: MarkdownLinkHandler;
|
|
16
21
|
}): ReactNode {
|
|
17
22
|
const { theme } = useTheme();
|
|
18
23
|
|
|
@@ -33,10 +38,14 @@ export const MarkdownTableRenderer = function MarkdownTableRenderer({
|
|
|
33
38
|
{ __isChunk: true, text: getPlainText(cell.tokens) },
|
|
34
39
|
];
|
|
35
40
|
return fallbackCell;
|
|
36
|
-
})
|
|
41
|
+
})
|
|
37
42
|
);
|
|
38
43
|
return [headerRow, ...dataRows];
|
|
39
44
|
}, [token, theme]);
|
|
45
|
+
const onMouseDown = useMemo(
|
|
46
|
+
() => tableLinkMouseHandler(content, onLinkActivate),
|
|
47
|
+
[content, onLinkActivate]
|
|
48
|
+
);
|
|
40
49
|
|
|
41
50
|
return (
|
|
42
51
|
<box style={{ marginBottom: 1, marginLeft: 1 + indent, marginRight: 1 }}>
|
|
@@ -49,6 +58,7 @@ export const MarkdownTableRenderer = function MarkdownTableRenderer({
|
|
|
49
58
|
borderStyle="single"
|
|
50
59
|
borderColor={theme.border}
|
|
51
60
|
fg={theme.text}
|
|
61
|
+
onMouseDown={onMouseDown}
|
|
52
62
|
/>
|
|
53
63
|
</box>
|
|
54
64
|
);
|
package/src/markdown/chunks.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Token } from "marked";
|
|
2
1
|
import {
|
|
3
2
|
bold as boldChunk,
|
|
4
3
|
italic as italicChunk,
|
|
@@ -7,6 +6,8 @@ import {
|
|
|
7
6
|
} from "@opentui/core";
|
|
8
7
|
import type { TextChunk } from "@opentui/core";
|
|
9
8
|
import type { ResolvedTheme } from "@tooee/themes";
|
|
9
|
+
import type { Token } from "marked";
|
|
10
|
+
|
|
10
11
|
import { hasMarkedText } from "./tokens.js";
|
|
11
12
|
import { visitInline } from "./visit-inline.js";
|
|
12
13
|
|
|
@@ -16,7 +17,7 @@ const chunk = function chunk(text: string): TextChunk {
|
|
|
16
17
|
|
|
17
18
|
export const inlineTokensToChunks = function inlineTokensToChunks(
|
|
18
19
|
tokens: readonly Token[],
|
|
19
|
-
theme: ResolvedTheme
|
|
20
|
+
theme: ResolvedTheme
|
|
20
21
|
): TextChunk[] {
|
|
21
22
|
return visitInline<TextChunk>(tokens, {
|
|
22
23
|
br: () => [],
|
|
@@ -33,8 +34,10 @@ export const inlineTokensToChunks = function inlineTokensToChunks(
|
|
|
33
34
|
escape: (token) => [chunk(token.text)],
|
|
34
35
|
fallback: (token) => (hasMarkedText(token) ? [chunk(token.text)] : []),
|
|
35
36
|
image: (token) => [chunk(token.text)],
|
|
36
|
-
link: (
|
|
37
|
-
children.map((child) =>
|
|
37
|
+
link: (token, children) =>
|
|
38
|
+
children.map((child) =>
|
|
39
|
+
underlineChunk({ ...child, fg: parseColor(theme.markdownLink), link: { url: token.href } })
|
|
40
|
+
),
|
|
38
41
|
space: () => [],
|
|
39
42
|
strong: (_token, children) => children.map((child) => boldChunk(child)),
|
|
40
43
|
text: (token) => [chunk(token.text)],
|
package/src/markdown/inline.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { ResolvedTheme } from "@tooee/themes";
|
|
1
2
|
import type { Token } from "marked";
|
|
2
3
|
import type { ReactNode } from "react";
|
|
3
|
-
|
|
4
|
+
|
|
4
5
|
import { hasMarkedText, normalizeSoftLineEndings } from "./tokens.js";
|
|
5
6
|
import { visitInline } from "./visit-inline.js";
|
|
6
7
|
|
package/src/markdown/links.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { Token } from "marked";
|
|
2
1
|
import type { MouseEvent, TextBufferRenderable } from "@opentui/core";
|
|
2
|
+
import type { Token } from "marked";
|
|
3
|
+
|
|
3
4
|
import { hasMarkedText, narrowToken, normalizeSoftLineEndings } from "./tokens.js";
|
|
4
5
|
|
|
5
6
|
// oxlint-disable-next-line anti-slop/no-unknown-returns -- public host callback accepts any ignored result; only literal true marks a handled link
|
|
@@ -14,7 +15,7 @@ export interface InlineLinkPosition {
|
|
|
14
15
|
export const inlineLinkAtPosition = function inlineLinkAtPosition(
|
|
15
16
|
tokens: readonly Token[],
|
|
16
17
|
position: InlineLinkPosition,
|
|
17
|
-
initialColumn = 0
|
|
18
|
+
initialColumn = 0
|
|
18
19
|
): string | null {
|
|
19
20
|
let line = 0;
|
|
20
21
|
let column = initialColumn;
|
|
@@ -94,7 +95,7 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
|
|
|
94
95
|
export const linkMouseHandler = function linkMouseHandler(
|
|
95
96
|
tokens: readonly Token[],
|
|
96
97
|
onLinkActivate: MarkdownLinkHandler | undefined,
|
|
97
|
-
initialColumn = 0
|
|
98
|
+
initialColumn = 0
|
|
98
99
|
): ((event: MouseEvent) => void) | undefined {
|
|
99
100
|
if (onLinkActivate === undefined) {
|
|
100
101
|
return undefined;
|
|
@@ -122,7 +123,7 @@ export const linkMouseHandler = function linkMouseHandler(
|
|
|
122
123
|
column: startColumn + event.x - text.x,
|
|
123
124
|
line: sourceLine,
|
|
124
125
|
},
|
|
125
|
-
initialColumn
|
|
126
|
+
initialColumn
|
|
126
127
|
);
|
|
127
128
|
if (href === null || onLinkActivate(href) !== true) {
|
|
128
129
|
return;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { MouseEvent, TextChunk, TextTableContent } from "@opentui/core";
|
|
2
|
+
|
|
3
|
+
import { textTableCellAt } from "../text-table-internals.js";
|
|
4
|
+
import type { MarkdownLinkHandler } from "./links.js";
|
|
5
|
+
|
|
6
|
+
/** The OSC 8 link URL of the chunk at a text position in one table cell. */
|
|
7
|
+
export const chunkLinkAt = function chunkLinkAt(
|
|
8
|
+
chunks: readonly TextChunk[],
|
|
9
|
+
line: number,
|
|
10
|
+
offset: number
|
|
11
|
+
): string | null {
|
|
12
|
+
let currentLine = 0;
|
|
13
|
+
let column = 0;
|
|
14
|
+
for (const chunk of chunks) {
|
|
15
|
+
const parts = chunk.text.split("\n");
|
|
16
|
+
for (const [index, part] of parts.entries()) {
|
|
17
|
+
const width = Bun.stringWidth(part);
|
|
18
|
+
if (currentLine === line && offset >= column && offset < column + width) {
|
|
19
|
+
return chunk.link?.url ?? null;
|
|
20
|
+
}
|
|
21
|
+
column += width;
|
|
22
|
+
if (index < parts.length - 1) {
|
|
23
|
+
currentLine += 1;
|
|
24
|
+
column = 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Primary-click handler for a Markdown table. It activates the link under the pointer and
|
|
33
|
+
* consumes the event only when the host handler returns literal true.
|
|
34
|
+
*/
|
|
35
|
+
export const tableLinkMouseHandler = function tableLinkMouseHandler(
|
|
36
|
+
content: TextTableContent,
|
|
37
|
+
onLinkActivate?: MarkdownLinkHandler
|
|
38
|
+
): ((event: MouseEvent) => void) | undefined {
|
|
39
|
+
if (onLinkActivate === undefined) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
return (event: MouseEvent): void => {
|
|
43
|
+
if (event.button !== 0 || event.target === null) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const hit = textTableCellAt(event.target, event.x, event.y);
|
|
47
|
+
const chunks = hit === null ? null : content[hit.row]?.[hit.column];
|
|
48
|
+
if (hit === null || chunks === null || chunks === undefined) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const href = chunkLinkAt(chunks, hit.line, hit.offset);
|
|
52
|
+
if (href === null || onLinkActivate(href) !== true) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
event.preventDefault();
|
|
56
|
+
event.stopPropagation();
|
|
57
|
+
};
|
|
58
|
+
};
|
package/src/markdown/tokens.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Token, Tokens } from "marked";
|
|
2
|
+
|
|
2
3
|
import { narrowToken } from "./tokens.js";
|
|
3
4
|
|
|
4
5
|
export interface InlineVisitorSink<Result> {
|
|
@@ -19,7 +20,7 @@ export interface InlineVisitorSink<Result> {
|
|
|
19
20
|
export const visitInline = function visitInline<Result>(
|
|
20
21
|
tokens: readonly Token[],
|
|
21
22
|
sink: InlineVisitorSink<Result>,
|
|
22
|
-
keyPrefix = ""
|
|
23
|
+
keyPrefix = ""
|
|
23
24
|
): Result[] {
|
|
24
25
|
const result: Result[] = [];
|
|
25
26
|
|
package/src/markdown-blocks.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { marked } from "marked";
|
|
2
2
|
import type { Token, Tokens } from "marked";
|
|
3
|
+
|
|
3
4
|
import { SourceIndex } from "./source.js";
|
|
4
5
|
import type { DocumentRowSource } from "./source.js";
|
|
5
6
|
|
|
@@ -206,7 +207,7 @@ const flattenList = function flattenList(
|
|
|
206
207
|
indent: number,
|
|
207
208
|
out: FlatBlock[],
|
|
208
209
|
res: MarkdownResolver | null,
|
|
209
|
-
bound: number
|
|
210
|
+
bound: number
|
|
210
211
|
): void {
|
|
211
212
|
for (let i = 0; i < list.items.length; i += 1) {
|
|
212
213
|
const item = list.items[i];
|
|
@@ -239,7 +240,7 @@ const flattenListItem = function flattenListItem(
|
|
|
239
240
|
out: FlatBlock[],
|
|
240
241
|
res: MarkdownResolver | null,
|
|
241
242
|
itemStart: number | null,
|
|
242
|
-
itemEnd: number
|
|
243
|
+
itemEnd: number
|
|
243
244
|
): void {
|
|
244
245
|
const checked = item.checked === null ? undefined : item.checked;
|
|
245
246
|
const childTokens = item.tokens;
|
|
@@ -302,7 +303,7 @@ const flattenWalk = function flattenWalk(
|
|
|
302
303
|
indent: number,
|
|
303
304
|
out: FlatBlock[],
|
|
304
305
|
res: MarkdownResolver | null,
|
|
305
|
-
bound: number
|
|
306
|
+
bound: number
|
|
306
307
|
): void {
|
|
307
308
|
for (const token of tokens) {
|
|
308
309
|
if (token.type === "space") {
|
|
@@ -326,13 +327,15 @@ const flattenWalk = function flattenWalk(
|
|
|
326
327
|
* Lex, flatten, and source-map Markdown in one operation. Every returned block
|
|
327
328
|
* carries a `source` anchor (or `null` when a marked edge case leaves a token
|
|
328
329
|
* genuinely unresolvable), in the exact order used for navigation/rendering.
|
|
330
|
+
* Block HTML is not rendered, so it does not occupy a navigation row. Resolve
|
|
331
|
+
* it before filtering so later blocks retain their sequential source anchors.
|
|
329
332
|
*/
|
|
330
333
|
export const flattenMarkdown = function flattenMarkdown(
|
|
331
334
|
markdown: string,
|
|
332
|
-
options?: FlattenMarkdownOptions
|
|
335
|
+
options?: FlattenMarkdownOptions
|
|
333
336
|
): FlatBlock[] {
|
|
334
337
|
const res = new MarkdownResolver(markdown, options?.sourceId);
|
|
335
338
|
const out: FlatBlock[] = [];
|
|
336
339
|
flattenWalk(marked.lexer(markdown), 0, out, res, markdown.length);
|
|
337
|
-
return out;
|
|
340
|
+
return out.filter((block) => block.token.type !== "html");
|
|
338
341
|
};
|
package/src/markdown-images.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
+
|
|
2
3
|
import type { Token, Tokens } from "marked";
|
|
3
4
|
|
|
4
5
|
const OBSIDIAN_IMAGE_EMBED = /!\[\[(?<target>[^\]|]+)(?:\|(?<display>[^\]]+))?\]\]/gu;
|
|
@@ -18,7 +19,7 @@ export type MarkdownInlineSegment =
|
|
|
18
19
|
|
|
19
20
|
const obsidianEmbed = function obsidianEmbed(
|
|
20
21
|
target: string,
|
|
21
|
-
display: string | undefined
|
|
22
|
+
display: string | undefined
|
|
22
23
|
): MarkdownImageEmbed {
|
|
23
24
|
const source = target.trim();
|
|
24
25
|
const dimensions = display?.trim().match(IMAGE_DIMENSIONS);
|
|
@@ -35,7 +36,7 @@ const obsidianEmbed = function obsidianEmbed(
|
|
|
35
36
|
|
|
36
37
|
/** Parse a complete Obsidian image embed such as `![[cover.png|40x12]]`. */
|
|
37
38
|
export const parseObsidianImageEmbed = function parseObsidianImageEmbed(
|
|
38
|
-
value: string
|
|
39
|
+
value: string
|
|
39
40
|
): MarkdownImageEmbed | null {
|
|
40
41
|
const match = /^!\[\[(?<target>[^\]|]+)(?:\|(?<display>[^\]]+))?\]\]$/u.exec(value.trim());
|
|
41
42
|
if (match?.groups?.target === undefined) {
|
|
@@ -80,7 +81,7 @@ const splitTextToken = function splitTextToken(text: string): MarkdownInlineSegm
|
|
|
80
81
|
|
|
81
82
|
/** Split paragraph tokens into text and native-image render segments. */
|
|
82
83
|
export const splitMarkdownImages = function splitMarkdownImages(
|
|
83
|
-
tokens: readonly Token[]
|
|
84
|
+
tokens: readonly Token[]
|
|
84
85
|
): MarkdownInlineSegment[] {
|
|
85
86
|
const result: MarkdownInlineSegment[] = [];
|
|
86
87
|
let pendingText: Token[] = [];
|
|
@@ -129,7 +130,7 @@ export const splitMarkdownImages = function splitMarkdownImages(
|
|
|
129
130
|
/** Resolve relative image links against the Markdown document directory. */
|
|
130
131
|
export const resolveMarkdownImageSource = function resolveMarkdownImageSource(
|
|
131
132
|
source: string,
|
|
132
|
-
basePath?: string
|
|
133
|
+
basePath?: string
|
|
133
134
|
): string {
|
|
134
135
|
if (path.isAbsolute(source) || URL_SCHEME.test(source)) {
|
|
135
136
|
return source;
|
package/src/markdown-view.tsx
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import type { ReactNode, RefObject } from "react";
|
|
3
1
|
import type { TextBufferRenderable } from "@opentui/core";
|
|
4
2
|
import { useTerminalDimensions } from "@opentui/react";
|
|
5
3
|
import { useTheme } from "@tooee/themes";
|
|
4
|
+
import { useMemo } from "react";
|
|
5
|
+
import type { ReactNode, RefObject } from "react";
|
|
6
|
+
|
|
6
7
|
import { DEFAULT_CODE_BLOCK_RENDERERS } from "./code-blocks.js";
|
|
7
8
|
import type { CodeBlockRenderer } from "./code-blocks.js";
|
|
8
9
|
import type { DocumentBindings } from "./document-bindings.js";
|
|
@@ -74,7 +75,7 @@ export const MarkdownView = function MarkdownView({
|
|
|
74
75
|
const { width: terminalWidth } = useTerminalDimensions();
|
|
75
76
|
const blocks = useMemo(
|
|
76
77
|
() => providedBlocks ?? flattenMarkdown(content),
|
|
77
|
-
[providedBlocks, content]
|
|
78
|
+
[providedBlocks, content]
|
|
78
79
|
);
|
|
79
80
|
|
|
80
81
|
const contentWidth = Math.max(
|
|
@@ -85,7 +86,7 @@ export const MarkdownView = function MarkdownView({
|
|
|
85
86
|
showLineNumbers,
|
|
86
87
|
signColumnWidth: DEFAULT_SIGN_COLUMN_WIDTH,
|
|
87
88
|
}) -
|
|
88
|
-
MARKDOWN_SCROLLBAR_RESERVE
|
|
89
|
+
MARKDOWN_SCROLLBAR_RESERVE
|
|
89
90
|
);
|
|
90
91
|
|
|
91
92
|
const mergedCodeBlockRenderers = useMemo(() => {
|
|
@@ -98,22 +99,20 @@ export const MarkdownView = function MarkdownView({
|
|
|
98
99
|
|
|
99
100
|
const blockElements = useMemo(
|
|
100
101
|
() =>
|
|
101
|
-
blocks.map(
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
),
|
|
116
|
-
),
|
|
102
|
+
blocks.map((block, index): ReactNode => (
|
|
103
|
+
<FlatBlockRenderer
|
|
104
|
+
key={index}
|
|
105
|
+
block={block}
|
|
106
|
+
blockIndex={index}
|
|
107
|
+
theme={theme}
|
|
108
|
+
syntax={syntax}
|
|
109
|
+
contentWidth={contentWidth}
|
|
110
|
+
hScrollableBlocksRef={hScrollableBlocksRef}
|
|
111
|
+
codeBlockRenderers={mergedCodeBlockRenderers}
|
|
112
|
+
onLinkActivate={onLinkActivate}
|
|
113
|
+
imageBasePath={imageBasePath}
|
|
114
|
+
/>
|
|
115
|
+
)),
|
|
117
116
|
[
|
|
118
117
|
blocks,
|
|
119
118
|
theme,
|
|
@@ -123,7 +122,7 @@ export const MarkdownView = function MarkdownView({
|
|
|
123
122
|
mergedCodeBlockRenderers,
|
|
124
123
|
onLinkActivate,
|
|
125
124
|
imageBasePath,
|
|
126
|
-
]
|
|
125
|
+
]
|
|
127
126
|
);
|
|
128
127
|
|
|
129
128
|
return (
|