@tooee/renderers 0.2.4 → 0.3.1
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/code-blocks.d.ts +6 -6
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js +52 -42
- package/dist/code-blocks.js.map +1 -1
- package/dist/{CodeView.d.ts → code-view.d.ts} +3 -3
- package/dist/code-view.d.ts.map +1 -0
- package/dist/{CodeView.js → code-view.js} +5 -5
- package/dist/code-view.js.map +1 -0
- package/dist/{CommandPalette.d.ts → command-palette.d.ts} +2 -2
- package/dist/command-palette.d.ts.map +1 -0
- package/dist/{CommandPalette.js → command-palette.js} +27 -24
- package/dist/command-palette.js.map +1 -0
- package/dist/{ContextMenu.d.ts → context-menu.d.ts} +2 -2
- package/dist/context-menu.d.ts.map +1 -0
- package/dist/{ContextMenu.js → context-menu.js} +24 -14
- package/dist/context-menu.js.map +1 -0
- package/dist/{DecorationLayer.d.ts → decoration-layer.d.ts} +2 -2
- package/dist/decoration-layer.d.ts.map +1 -0
- package/dist/decoration-layer.js +2 -0
- package/dist/decoration-layer.js.map +1 -0
- package/dist/{DocumentBindings.d.ts → document-bindings.d.ts} +4 -4
- package/dist/document-bindings.d.ts.map +1 -0
- package/dist/document-bindings.js +2 -0
- package/dist/document-bindings.js.map +1 -0
- package/dist/index.d.ts +16 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -9
- package/dist/index.js.map +1 -1
- package/dist/markdown-blocks.d.ts +9 -13
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +98 -79
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/{MarkdownView.d.ts → markdown-view.d.ts} +13 -13
- package/dist/markdown-view.d.ts.map +1 -0
- package/dist/markdown-view.js +455 -0
- package/dist/markdown-view.js.map +1 -0
- package/dist/mermaid.d.ts +6 -6
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +56 -26
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts +5 -5
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js +64 -44
- package/dist/parsers.js.map +1 -1
- package/dist/{RowDocumentRenderable.d.ts → row-document-renderable.d.ts} +15 -11
- package/dist/row-document-renderable.d.ts.map +1 -0
- package/dist/{RowDocumentRenderable.js → row-document-renderable.js} +110 -62
- package/dist/row-document-renderable.js.map +1 -0
- package/dist/row-document.d.ts +1 -1
- package/dist/row-document.d.ts.map +1 -1
- package/dist/row-document.js +1 -1
- package/dist/row-document.js.map +1 -1
- package/dist/source.d.ts +6 -6
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +35 -26
- package/dist/source.js.map +1 -1
- package/dist/table-types.d.ts.map +1 -1
- package/dist/{Table.d.ts → table.d.ts} +6 -6
- package/dist/table.d.ts.map +1 -0
- package/dist/{Table.js → table.js} +65 -35
- package/dist/table.js.map +1 -0
- package/dist/text-table.d.ts.map +1 -1
- package/dist/text-table.js.map +1 -1
- package/dist/use-gutter-palette.d.ts +3 -0
- package/dist/use-gutter-palette.d.ts.map +1 -0
- package/dist/{useGutterPalette.js → use-gutter-palette.js} +4 -4
- package/dist/use-gutter-palette.js.map +1 -0
- package/package.json +5 -5
- package/src/code-blocks.tsx +122 -106
- package/src/{CodeView.tsx → code-view.tsx} +19 -14
- package/src/command-palette.tsx +163 -0
- package/src/context-menu.tsx +157 -0
- package/src/decoration-layer.ts +11 -0
- package/src/document-bindings.ts +16 -0
- package/src/index.ts +24 -23
- package/src/markdown-blocks.ts +178 -138
- package/src/markdown-view.tsx +853 -0
- package/src/mermaid.ts +122 -70
- package/src/parsers.ts +141 -103
- package/src/row-document-renderable.ts +672 -0
- package/src/row-document.ts +4 -4
- package/src/source.ts +92 -72
- package/src/table-types.ts +4 -4
- package/src/table.tsx +346 -0
- package/src/text-table.ts +4 -4
- package/src/use-gutter-palette.ts +15 -0
- package/dist/CodeView.d.ts.map +0 -1
- package/dist/CodeView.js.map +0 -1
- package/dist/CommandPalette.d.ts.map +0 -1
- package/dist/CommandPalette.js.map +0 -1
- package/dist/ContextMenu.d.ts.map +0 -1
- package/dist/ContextMenu.js.map +0 -1
- package/dist/DecorationLayer.d.ts.map +0 -1
- package/dist/DecorationLayer.js +0 -2
- package/dist/DecorationLayer.js.map +0 -1
- package/dist/DocumentBindings.d.ts.map +0 -1
- package/dist/DocumentBindings.js +0 -2
- package/dist/DocumentBindings.js.map +0 -1
- package/dist/MarkdownView.d.ts.map +0 -1
- package/dist/MarkdownView.js +0 -360
- package/dist/MarkdownView.js.map +0 -1
- package/dist/RowDocumentRenderable.d.ts.map +0 -1
- package/dist/RowDocumentRenderable.js.map +0 -1
- package/dist/Table.d.ts.map +0 -1
- package/dist/Table.js.map +0 -1
- package/dist/useGutterPalette.d.ts +0 -3
- package/dist/useGutterPalette.d.ts.map +0 -1
- package/dist/useGutterPalette.js.map +0 -1
- package/src/CommandPalette.tsx +0 -147
- package/src/ContextMenu.tsx +0 -132
- package/src/DecorationLayer.ts +0 -11
- package/src/DocumentBindings.ts +0 -16
- package/src/MarkdownView.tsx +0 -738
- package/src/RowDocumentRenderable.ts +0 -593
- package/src/Table.tsx +0 -300
- package/src/useGutterPalette.ts +0 -15
package/src/MarkdownView.tsx
DELETED
|
@@ -1,738 +0,0 @@
|
|
|
1
|
-
import { type Token, type Tokens } from "marked"
|
|
2
|
-
import { useMemo, type ReactNode, type RefObject } from "react"
|
|
3
|
-
import { useTheme, type ResolvedTheme } from "@tooee/themes"
|
|
4
|
-
import {
|
|
5
|
-
bold as boldChunk,
|
|
6
|
-
italic as italicChunk,
|
|
7
|
-
underline as underlineChunk,
|
|
8
|
-
parseColor,
|
|
9
|
-
} from "@opentui/core"
|
|
10
|
-
import type {
|
|
11
|
-
SyntaxStyle,
|
|
12
|
-
TextBufferRenderable,
|
|
13
|
-
TextTableContent,
|
|
14
|
-
TextTableCellContent,
|
|
15
|
-
TextChunk,
|
|
16
|
-
MouseEvent,
|
|
17
|
-
} from "@opentui/core"
|
|
18
|
-
import type { DocumentBindings } from "./DocumentBindings.js"
|
|
19
|
-
import { DEFAULT_SIGN_COLUMN_WIDTH } from "./RowDocumentRenderable.js"
|
|
20
|
-
import { useGutterPalette } from "./useGutterPalette.js"
|
|
21
|
-
import { CodeBlock, DEFAULT_CODE_BLOCK_RENDERERS, type CodeBlockRenderer } from "./code-blocks.js"
|
|
22
|
-
import { flattenMarkdown, type FlatBlock } from "./markdown-blocks.js"
|
|
23
|
-
import "./row-document.js"
|
|
24
|
-
import "./text-table.js"
|
|
25
|
-
|
|
26
|
-
// ---------------------------------------------------------------------------
|
|
27
|
-
// Types
|
|
28
|
-
// ---------------------------------------------------------------------------
|
|
29
|
-
|
|
30
|
-
export type MarkdownLinkHandler = (href: string) => boolean | void
|
|
31
|
-
|
|
32
|
-
interface MarkdownViewProps {
|
|
33
|
-
content: string
|
|
34
|
-
/**
|
|
35
|
-
* Pre-flattened blocks to render, normally the exact array a subview passed
|
|
36
|
-
* to its document controller. When supplied, `content` is *not* lexed again,
|
|
37
|
-
* so the rendered rows and the controller's navigation rows cannot drift.
|
|
38
|
-
* Omit it and `MarkdownView` flattens `content` itself for static callers.
|
|
39
|
-
*/
|
|
40
|
-
blocks?: readonly FlatBlock[]
|
|
41
|
-
showLineNumbers?: boolean
|
|
42
|
-
/**
|
|
43
|
-
* Binds the row document to a document controller: its ref, the decoration
|
|
44
|
-
* layers to paint, and the mouse handler. Rows are flattened *block* indices —
|
|
45
|
-
* the same unit `j`/`k` move between and copy operates on. Omit it to render
|
|
46
|
-
* a static, non-interactive document.
|
|
47
|
-
*
|
|
48
|
-
* Blocks are per-child renderables, but their markup varies across renderer
|
|
49
|
-
* branches (box/text/null with differing margins), so the controller maps
|
|
50
|
-
* clicks by screen-Y rather than per-child handlers. Clicks bubble up from
|
|
51
|
-
* block children (including custom code blocks) unless a custom renderer
|
|
52
|
-
* stops propagation.
|
|
53
|
-
*/
|
|
54
|
-
document?: DocumentBindings
|
|
55
|
-
/**
|
|
56
|
-
* Registry of horizontally scrollable blocks, keyed by block index. Blocks
|
|
57
|
-
* that can overflow horizontally (mermaid diagrams and code blocks)
|
|
58
|
-
* register their text-buffer renderable here so the owning subview can
|
|
59
|
-
* drive horizontal panning (`scrollX`) for the block under the nav cursor
|
|
60
|
-
* (h/l in cursor mode).
|
|
61
|
-
*/
|
|
62
|
-
hScrollableBlocksRef?: RefObject<Map<number, TextBufferRenderable>>
|
|
63
|
-
/**
|
|
64
|
-
* Custom renderers for fenced code blocks, keyed by fence type. A fence's
|
|
65
|
-
* type is the first whitespace-separated word of its info string, matched
|
|
66
|
-
* case-insensitively. Entries are merged over the built-in defaults
|
|
67
|
-
* (currently `mermaid`), so built-ins can be overridden. Unmatched types —
|
|
68
|
-
* and renderers that return `null` or throw — fall back to the default
|
|
69
|
-
* syntax-highlighted code block.
|
|
70
|
-
*/
|
|
71
|
-
codeBlockRenderers?: Record<string, CodeBlockRenderer>
|
|
72
|
-
/**
|
|
73
|
-
* Called when the primary mouse button activates inline Markdown link text.
|
|
74
|
-
* Return `true` when the host handled the href; handled clicks do not bubble
|
|
75
|
-
* to document row selection. Unhandled links retain their native OSC-8 href.
|
|
76
|
-
*/
|
|
77
|
-
onLinkActivate?: MarkdownLinkHandler
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// ---------------------------------------------------------------------------
|
|
81
|
-
// Component
|
|
82
|
-
// ---------------------------------------------------------------------------
|
|
83
|
-
|
|
84
|
-
export function MarkdownView({
|
|
85
|
-
content,
|
|
86
|
-
blocks: providedBlocks,
|
|
87
|
-
showLineNumbers = true,
|
|
88
|
-
document,
|
|
89
|
-
hScrollableBlocksRef,
|
|
90
|
-
codeBlockRenderers,
|
|
91
|
-
onLinkActivate,
|
|
92
|
-
}: MarkdownViewProps) {
|
|
93
|
-
const { theme, syntax } = useTheme()
|
|
94
|
-
const palette = useGutterPalette()
|
|
95
|
-
const blocks = useMemo(
|
|
96
|
-
() => providedBlocks ?? flattenMarkdown(content),
|
|
97
|
-
[providedBlocks, content],
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
// Merge user renderers over built-in defaults, normalizing keys to
|
|
101
|
-
// lowercase so registration matches fence types case-insensitively.
|
|
102
|
-
const mergedCodeBlockRenderers = useMemo(() => {
|
|
103
|
-
const merged: Record<string, CodeBlockRenderer> = { ...DEFAULT_CODE_BLOCK_RENDERERS }
|
|
104
|
-
for (const [key, renderer] of Object.entries(codeBlockRenderers ?? {})) {
|
|
105
|
-
merged[key.trim().toLowerCase()] = renderer
|
|
106
|
-
}
|
|
107
|
-
return merged
|
|
108
|
-
}, [codeBlockRenderers])
|
|
109
|
-
|
|
110
|
-
const blockElements = useMemo(
|
|
111
|
-
() =>
|
|
112
|
-
blocks.map((block, index) => (
|
|
113
|
-
<FlatBlockRenderer
|
|
114
|
-
key={index}
|
|
115
|
-
block={block}
|
|
116
|
-
blockIndex={index}
|
|
117
|
-
theme={theme}
|
|
118
|
-
syntax={syntax}
|
|
119
|
-
hScrollableBlocksRef={hScrollableBlocksRef}
|
|
120
|
-
codeBlockRenderers={mergedCodeBlockRenderers}
|
|
121
|
-
onLinkActivate={onLinkActivate}
|
|
122
|
-
/>
|
|
123
|
-
)),
|
|
124
|
-
[blocks, theme, syntax, hScrollableBlocksRef, mergedCodeBlockRenderers, onLinkActivate],
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
return (
|
|
128
|
-
<row-document
|
|
129
|
-
ref={document?.ref}
|
|
130
|
-
showLineNumbers={showLineNumbers}
|
|
131
|
-
palette={palette}
|
|
132
|
-
decorations={document?.decorations}
|
|
133
|
-
signColumnWidth={DEFAULT_SIGN_COLUMN_WIDTH}
|
|
134
|
-
style={{ flexGrow: 1 }}
|
|
135
|
-
onMouseDown={document?.onMouseDown}
|
|
136
|
-
>
|
|
137
|
-
{blockElements}
|
|
138
|
-
</row-document>
|
|
139
|
-
)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// ---------------------------------------------------------------------------
|
|
143
|
-
// Block renderer (flat)
|
|
144
|
-
// ---------------------------------------------------------------------------
|
|
145
|
-
|
|
146
|
-
function FlatBlockRenderer({
|
|
147
|
-
block,
|
|
148
|
-
blockIndex,
|
|
149
|
-
theme,
|
|
150
|
-
syntax,
|
|
151
|
-
hScrollableBlocksRef,
|
|
152
|
-
codeBlockRenderers,
|
|
153
|
-
onLinkActivate,
|
|
154
|
-
}: {
|
|
155
|
-
block: FlatBlock
|
|
156
|
-
blockIndex: number
|
|
157
|
-
theme: ResolvedTheme
|
|
158
|
-
syntax: SyntaxStyle
|
|
159
|
-
hScrollableBlocksRef?: RefObject<Map<number, TextBufferRenderable>>
|
|
160
|
-
codeBlockRenderers?: Record<string, CodeBlockRenderer>
|
|
161
|
-
/**
|
|
162
|
-
* Called when the primary mouse button activates inline Markdown link text.
|
|
163
|
-
* Return `true` when the host handled the href; handled clicks do not bubble
|
|
164
|
-
* to document row selection. Unhandled links retain their native OSC-8 href.
|
|
165
|
-
*/
|
|
166
|
-
onLinkActivate?: MarkdownLinkHandler
|
|
167
|
-
}): ReactNode {
|
|
168
|
-
const { token, indent, bullet } = block
|
|
169
|
-
|
|
170
|
-
// List item line (has bullet)
|
|
171
|
-
if (bullet !== undefined) {
|
|
172
|
-
return <ListLineRenderer block={block} theme={theme} onLinkActivate={onLinkActivate} />
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Regular block token
|
|
176
|
-
switch (token.type) {
|
|
177
|
-
case "heading":
|
|
178
|
-
return (
|
|
179
|
-
<HeadingRenderer
|
|
180
|
-
token={token as Tokens.Heading}
|
|
181
|
-
theme={theme}
|
|
182
|
-
indent={indent}
|
|
183
|
-
onLinkActivate={onLinkActivate}
|
|
184
|
-
/>
|
|
185
|
-
)
|
|
186
|
-
case "paragraph":
|
|
187
|
-
return (
|
|
188
|
-
<ParagraphRenderer
|
|
189
|
-
token={token as Tokens.Paragraph}
|
|
190
|
-
theme={theme}
|
|
191
|
-
indent={indent}
|
|
192
|
-
onLinkActivate={onLinkActivate}
|
|
193
|
-
/>
|
|
194
|
-
)
|
|
195
|
-
case "code":
|
|
196
|
-
return (
|
|
197
|
-
<CodeBlock
|
|
198
|
-
token={token as Tokens.Code}
|
|
199
|
-
blockIndex={blockIndex}
|
|
200
|
-
theme={theme}
|
|
201
|
-
syntax={syntax}
|
|
202
|
-
indent={indent}
|
|
203
|
-
hScrollableBlocksRef={hScrollableBlocksRef}
|
|
204
|
-
renderers={codeBlockRenderers}
|
|
205
|
-
/>
|
|
206
|
-
)
|
|
207
|
-
case "blockquote":
|
|
208
|
-
return (
|
|
209
|
-
<BlockquoteRenderer
|
|
210
|
-
token={token as Tokens.Blockquote}
|
|
211
|
-
theme={theme}
|
|
212
|
-
indent={indent}
|
|
213
|
-
onLinkActivate={onLinkActivate}
|
|
214
|
-
/>
|
|
215
|
-
)
|
|
216
|
-
case "table":
|
|
217
|
-
return <MarkdownTableRenderer token={token as Tokens.Table} indent={indent} />
|
|
218
|
-
case "hr":
|
|
219
|
-
return <HorizontalRule theme={theme} indent={indent} />
|
|
220
|
-
case "space":
|
|
221
|
-
case "html":
|
|
222
|
-
return null
|
|
223
|
-
default:
|
|
224
|
-
if ("text" in token && typeof token.text === "string") {
|
|
225
|
-
return (
|
|
226
|
-
<text
|
|
227
|
-
content={token.text}
|
|
228
|
-
style={{
|
|
229
|
-
fg: theme.markdownText,
|
|
230
|
-
marginBottom: 1,
|
|
231
|
-
marginTop: 0,
|
|
232
|
-
marginLeft: 1 + indent,
|
|
233
|
-
marginRight: 1,
|
|
234
|
-
}}
|
|
235
|
-
/>
|
|
236
|
-
)
|
|
237
|
-
}
|
|
238
|
-
return null
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// ---------------------------------------------------------------------------
|
|
243
|
-
// List line renderer
|
|
244
|
-
// ---------------------------------------------------------------------------
|
|
245
|
-
|
|
246
|
-
function ListLineRenderer({
|
|
247
|
-
block,
|
|
248
|
-
theme,
|
|
249
|
-
onLinkActivate,
|
|
250
|
-
}: {
|
|
251
|
-
block: FlatBlock
|
|
252
|
-
theme: ResolvedTheme
|
|
253
|
-
onLinkActivate?: MarkdownLinkHandler
|
|
254
|
-
}) {
|
|
255
|
-
const { token, indent, bullet, checked } = block
|
|
256
|
-
const checkboxPrefix = checked !== undefined ? (checked ? "[x] " : "[ ] ") : ""
|
|
257
|
-
|
|
258
|
-
// Get inline tokens from the text/paragraph token
|
|
259
|
-
const inlineTokens: Token[] = "tokens" in token && Array.isArray(token.tokens) ? token.tokens : []
|
|
260
|
-
|
|
261
|
-
const hasText = "text" in token && typeof token.text === "string" && token.text.length > 0
|
|
262
|
-
const hasContent = inlineTokens.length > 0 || hasText
|
|
263
|
-
|
|
264
|
-
return (
|
|
265
|
-
<box style={{ marginLeft: 1 + indent, marginRight: 1 }}>
|
|
266
|
-
<text
|
|
267
|
-
style={{ fg: theme.markdownText }}
|
|
268
|
-
onMouseDown={linkMouseHandler(
|
|
269
|
-
inlineTokens,
|
|
270
|
-
onLinkActivate,
|
|
271
|
-
Bun.stringWidth(`${bullet}${checkboxPrefix}`),
|
|
272
|
-
)}
|
|
273
|
-
>
|
|
274
|
-
<span fg={theme.markdownListItem}>{bullet}</span>
|
|
275
|
-
{checkboxPrefix !== "" && (
|
|
276
|
-
<span fg={checked ? theme.accent : theme.textMuted}>{checkboxPrefix}</span>
|
|
277
|
-
)}
|
|
278
|
-
{hasContent &&
|
|
279
|
-
(inlineTokens.length > 0 ? (
|
|
280
|
-
<InlineTokens onLinkActivate={onLinkActivate} tokens={inlineTokens} theme={theme} />
|
|
281
|
-
) : hasText ? (
|
|
282
|
-
"text" in token ? (
|
|
283
|
-
(token as { text: string }).text
|
|
284
|
-
) : (
|
|
285
|
-
""
|
|
286
|
-
)
|
|
287
|
-
) : null)}
|
|
288
|
-
</text>
|
|
289
|
-
</box>
|
|
290
|
-
)
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// ---------------------------------------------------------------------------
|
|
294
|
-
// Block renderers
|
|
295
|
-
// ---------------------------------------------------------------------------
|
|
296
|
-
|
|
297
|
-
function HeadingRenderer({
|
|
298
|
-
token,
|
|
299
|
-
theme,
|
|
300
|
-
indent,
|
|
301
|
-
onLinkActivate,
|
|
302
|
-
}: {
|
|
303
|
-
token: Tokens.Heading
|
|
304
|
-
theme: ResolvedTheme
|
|
305
|
-
indent: number
|
|
306
|
-
onLinkActivate?: MarkdownLinkHandler
|
|
307
|
-
}) {
|
|
308
|
-
const headingColors: Record<number, string> = {
|
|
309
|
-
1: theme.markdownHeading,
|
|
310
|
-
2: theme.secondary,
|
|
311
|
-
3: theme.accent,
|
|
312
|
-
4: theme.text,
|
|
313
|
-
5: theme.textMuted,
|
|
314
|
-
6: theme.textMuted,
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
const prefixes: Record<number, string> = {
|
|
318
|
-
1: "# ",
|
|
319
|
-
2: "## ",
|
|
320
|
-
3: "### ",
|
|
321
|
-
4: "#### ",
|
|
322
|
-
5: "##### ",
|
|
323
|
-
6: "###### ",
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
return (
|
|
327
|
-
<box style={{ marginTop: 1, marginBottom: 1, marginLeft: indent }}>
|
|
328
|
-
<text
|
|
329
|
-
style={{ fg: headingColors[token.depth] || theme.text }}
|
|
330
|
-
onMouseDown={linkMouseHandler(
|
|
331
|
-
token.tokens || [],
|
|
332
|
-
onLinkActivate,
|
|
333
|
-
Bun.stringWidth(prefixes[token.depth] || ""),
|
|
334
|
-
)}
|
|
335
|
-
>
|
|
336
|
-
<span fg={theme.textMuted}>{prefixes[token.depth]}</span>
|
|
337
|
-
<strong>
|
|
338
|
-
<InlineTokens onLinkActivate={onLinkActivate} tokens={token.tokens || []} theme={theme} />
|
|
339
|
-
</strong>
|
|
340
|
-
</text>
|
|
341
|
-
</box>
|
|
342
|
-
)
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
function ParagraphRenderer({
|
|
346
|
-
token,
|
|
347
|
-
theme,
|
|
348
|
-
indent,
|
|
349
|
-
onLinkActivate,
|
|
350
|
-
}: {
|
|
351
|
-
token: Tokens.Paragraph
|
|
352
|
-
theme: ResolvedTheme
|
|
353
|
-
indent: number
|
|
354
|
-
onLinkActivate?: MarkdownLinkHandler
|
|
355
|
-
}) {
|
|
356
|
-
return (
|
|
357
|
-
<box style={{ marginBottom: 1, marginLeft: 1 + indent, marginRight: 1 }}>
|
|
358
|
-
<text
|
|
359
|
-
style={{ fg: theme.markdownText }}
|
|
360
|
-
onMouseDown={linkMouseHandler(token.tokens || [], onLinkActivate)}
|
|
361
|
-
>
|
|
362
|
-
<InlineTokens onLinkActivate={onLinkActivate} tokens={token.tokens || []} theme={theme} />
|
|
363
|
-
</text>
|
|
364
|
-
</box>
|
|
365
|
-
)
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
function BlockquoteRenderer({
|
|
369
|
-
token,
|
|
370
|
-
theme,
|
|
371
|
-
indent,
|
|
372
|
-
onLinkActivate,
|
|
373
|
-
}: {
|
|
374
|
-
token: Tokens.Blockquote
|
|
375
|
-
theme: ResolvedTheme
|
|
376
|
-
indent: number
|
|
377
|
-
onLinkActivate?: MarkdownLinkHandler
|
|
378
|
-
}) {
|
|
379
|
-
// Collect inline tokens from blockquote's child paragraphs/text
|
|
380
|
-
const inlineTokens: Token[] = []
|
|
381
|
-
if (token.tokens) {
|
|
382
|
-
for (const child of token.tokens) {
|
|
383
|
-
if ("tokens" in child && Array.isArray(child.tokens)) {
|
|
384
|
-
if (inlineTokens.length > 0) {
|
|
385
|
-
inlineTokens.push({ type: "text", raw: "\n", text: "\n" } as Token)
|
|
386
|
-
}
|
|
387
|
-
inlineTokens.push(...(child.tokens as Token[]))
|
|
388
|
-
} else if ("text" in child && typeof child.text === "string") {
|
|
389
|
-
inlineTokens.push(child)
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
return (
|
|
395
|
-
<box
|
|
396
|
-
style={{
|
|
397
|
-
marginTop: 0,
|
|
398
|
-
marginBottom: 1,
|
|
399
|
-
marginLeft: 1 + indent,
|
|
400
|
-
marginRight: 1,
|
|
401
|
-
paddingLeft: 2,
|
|
402
|
-
}}
|
|
403
|
-
>
|
|
404
|
-
<text style={{ fg: theme.markdownBlockQuote }} content="│ " />
|
|
405
|
-
<text
|
|
406
|
-
style={{ fg: theme.textMuted }}
|
|
407
|
-
onMouseDown={linkMouseHandler(inlineTokens, onLinkActivate)}
|
|
408
|
-
>
|
|
409
|
-
<InlineTokens onLinkActivate={onLinkActivate} tokens={inlineTokens} theme={theme} />
|
|
410
|
-
</text>
|
|
411
|
-
</box>
|
|
412
|
-
)
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
function MarkdownTableRenderer({ token, indent }: { token: Tokens.Table; indent: number }) {
|
|
416
|
-
const { theme } = useTheme()
|
|
417
|
-
|
|
418
|
-
const content: TextTableContent = useMemo(() => {
|
|
419
|
-
const headerRow: TextTableCellContent[] = token.header.map((cell) => {
|
|
420
|
-
const chunks = inlineTokensToChunks(cell.tokens, theme)
|
|
421
|
-
// Wrap header chunks in bold
|
|
422
|
-
return chunks.length > 0
|
|
423
|
-
? chunks.map((c) => boldChunk(c))
|
|
424
|
-
: [boldChunk(getPlainText(cell.tokens).trim())]
|
|
425
|
-
})
|
|
426
|
-
const dataRows = token.rows.map((row) =>
|
|
427
|
-
row.map((cell) => {
|
|
428
|
-
const chunks = inlineTokensToChunks(cell.tokens, theme)
|
|
429
|
-
return chunks.length > 0
|
|
430
|
-
? chunks
|
|
431
|
-
: ([
|
|
432
|
-
{ __isChunk: true as const, text: getPlainText(cell.tokens) },
|
|
433
|
-
] as TextTableCellContent)
|
|
434
|
-
}),
|
|
435
|
-
)
|
|
436
|
-
return [headerRow, ...dataRows]
|
|
437
|
-
}, [token, theme])
|
|
438
|
-
|
|
439
|
-
return (
|
|
440
|
-
<box style={{ marginLeft: 1 + indent, marginRight: 1, marginBottom: 1 }}>
|
|
441
|
-
<text-table
|
|
442
|
-
content={content}
|
|
443
|
-
wrapMode="word"
|
|
444
|
-
columnWidthMode="content"
|
|
445
|
-
cellPadding={0}
|
|
446
|
-
border={true}
|
|
447
|
-
borderStyle="single"
|
|
448
|
-
borderColor={theme.border}
|
|
449
|
-
fg={theme.text}
|
|
450
|
-
/>
|
|
451
|
-
</box>
|
|
452
|
-
)
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
function HorizontalRule({ theme, indent }: { theme: ResolvedTheme; indent: number }) {
|
|
456
|
-
return (
|
|
457
|
-
<box style={{ marginTop: 0, marginBottom: 1, marginLeft: 1 + indent, marginRight: 1 }}>
|
|
458
|
-
<text style={{ fg: theme.markdownHorizontalRule }} content={"─".repeat(40)} />
|
|
459
|
-
</box>
|
|
460
|
-
)
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
export interface InlineLinkPosition {
|
|
464
|
-
line: number
|
|
465
|
-
column: number
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
/** Resolve a rendered text position to the Markdown link occupying that cell. */
|
|
469
|
-
export function inlineLinkAtPosition(
|
|
470
|
-
tokens: readonly Token[],
|
|
471
|
-
position: InlineLinkPosition,
|
|
472
|
-
initialColumn = 0,
|
|
473
|
-
): string | null {
|
|
474
|
-
let line = 0
|
|
475
|
-
let column = initialColumn
|
|
476
|
-
let found: string | null = null
|
|
477
|
-
|
|
478
|
-
const text = (value: string, href?: string) => {
|
|
479
|
-
for (const part of value.split("\n")) {
|
|
480
|
-
const width = Bun.stringWidth(part)
|
|
481
|
-
if (
|
|
482
|
-
href &&
|
|
483
|
-
line === position.line &&
|
|
484
|
-
position.column >= column &&
|
|
485
|
-
position.column < column + width
|
|
486
|
-
) {
|
|
487
|
-
found = href
|
|
488
|
-
}
|
|
489
|
-
column += width
|
|
490
|
-
if (part !== value.split("\n").at(-1)) {
|
|
491
|
-
line += 1
|
|
492
|
-
column = 0
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
const visit = (items: readonly Token[], href?: string) => {
|
|
497
|
-
for (const token of items) {
|
|
498
|
-
if (found) return
|
|
499
|
-
switch (token.type) {
|
|
500
|
-
case "link": {
|
|
501
|
-
const link = token as Tokens.Link
|
|
502
|
-
visit(link.tokens || [], link.href)
|
|
503
|
-
break
|
|
504
|
-
}
|
|
505
|
-
case "strong":
|
|
506
|
-
visit((token as Tokens.Strong).tokens || [], href)
|
|
507
|
-
break
|
|
508
|
-
case "em":
|
|
509
|
-
visit((token as Tokens.Em).tokens || [], href)
|
|
510
|
-
break
|
|
511
|
-
case "del":
|
|
512
|
-
text("~", href)
|
|
513
|
-
visit((token as Tokens.Del).tokens || [], href)
|
|
514
|
-
text("~", href)
|
|
515
|
-
break
|
|
516
|
-
case "codespan":
|
|
517
|
-
text(` ${(token as Tokens.Codespan).text} `, href)
|
|
518
|
-
break
|
|
519
|
-
case "br":
|
|
520
|
-
text("\n", href)
|
|
521
|
-
break
|
|
522
|
-
case "space":
|
|
523
|
-
text(" ", href)
|
|
524
|
-
break
|
|
525
|
-
default:
|
|
526
|
-
if ("text" in token && typeof token.text === "string") text(token.text, href)
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
visit(tokens)
|
|
531
|
-
return found
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
function linkMouseHandler(
|
|
535
|
-
tokens: readonly Token[],
|
|
536
|
-
onLinkActivate: MarkdownLinkHandler | undefined,
|
|
537
|
-
initialColumn = 0,
|
|
538
|
-
) {
|
|
539
|
-
if (!onLinkActivate) return undefined
|
|
540
|
-
return (event: MouseEvent) => {
|
|
541
|
-
if (event.button !== 0) return
|
|
542
|
-
const target = event.target
|
|
543
|
-
if (!target || !("lineInfo" in target)) return
|
|
544
|
-
const text = target as TextBufferRenderable
|
|
545
|
-
const visualLine = event.y - text.y
|
|
546
|
-
const sourceLine = text.lineInfo.lineSources[visualLine]
|
|
547
|
-
const startColumn = text.lineInfo.lineStartCols[visualLine]
|
|
548
|
-
if (sourceLine == null || startColumn == null) return
|
|
549
|
-
const href = inlineLinkAtPosition(
|
|
550
|
-
tokens,
|
|
551
|
-
{
|
|
552
|
-
line: sourceLine,
|
|
553
|
-
column: startColumn + event.x - text.x,
|
|
554
|
-
},
|
|
555
|
-
initialColumn,
|
|
556
|
-
)
|
|
557
|
-
if (!href || onLinkActivate(href) !== true) return
|
|
558
|
-
event.preventDefault()
|
|
559
|
-
event.stopPropagation()
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
// ---------------------------------------------------------------------------
|
|
564
|
-
// Inline token rendering (React elements)
|
|
565
|
-
// ---------------------------------------------------------------------------
|
|
566
|
-
|
|
567
|
-
function InlineTokens({
|
|
568
|
-
tokens,
|
|
569
|
-
theme,
|
|
570
|
-
onLinkActivate,
|
|
571
|
-
}: {
|
|
572
|
-
tokens: Token[]
|
|
573
|
-
theme: ResolvedTheme
|
|
574
|
-
onLinkActivate?: MarkdownLinkHandler
|
|
575
|
-
}): ReactNode {
|
|
576
|
-
const result: ReactNode[] = []
|
|
577
|
-
|
|
578
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
579
|
-
const token = tokens[i]
|
|
580
|
-
if (!token) continue
|
|
581
|
-
const key = i
|
|
582
|
-
|
|
583
|
-
switch (token.type) {
|
|
584
|
-
case "text":
|
|
585
|
-
result.push((token as Tokens.Text).text)
|
|
586
|
-
break
|
|
587
|
-
case "strong":
|
|
588
|
-
result.push(
|
|
589
|
-
<strong key={key}>
|
|
590
|
-
<InlineTokens
|
|
591
|
-
onLinkActivate={onLinkActivate}
|
|
592
|
-
tokens={(token as Tokens.Strong).tokens || []}
|
|
593
|
-
theme={theme}
|
|
594
|
-
/>
|
|
595
|
-
</strong>,
|
|
596
|
-
)
|
|
597
|
-
break
|
|
598
|
-
case "em":
|
|
599
|
-
result.push(
|
|
600
|
-
<em key={key}>
|
|
601
|
-
<InlineTokens
|
|
602
|
-
onLinkActivate={onLinkActivate}
|
|
603
|
-
tokens={(token as Tokens.Em).tokens || []}
|
|
604
|
-
theme={theme}
|
|
605
|
-
/>
|
|
606
|
-
</em>,
|
|
607
|
-
)
|
|
608
|
-
break
|
|
609
|
-
case "codespan":
|
|
610
|
-
result.push(
|
|
611
|
-
<span key={key} fg={theme.markdownCode} bg={theme.backgroundPanel}>
|
|
612
|
-
{` ${(token as Tokens.Codespan).text} `}
|
|
613
|
-
</span>,
|
|
614
|
-
)
|
|
615
|
-
break
|
|
616
|
-
case "link": {
|
|
617
|
-
const linkToken = token as Tokens.Link
|
|
618
|
-
result.push(
|
|
619
|
-
<u key={key}>
|
|
620
|
-
<a href={linkToken.href} fg={theme.markdownLink}>
|
|
621
|
-
<InlineTokens
|
|
622
|
-
onLinkActivate={onLinkActivate}
|
|
623
|
-
tokens={linkToken.tokens || []}
|
|
624
|
-
theme={theme}
|
|
625
|
-
/>
|
|
626
|
-
</a>
|
|
627
|
-
</u>,
|
|
628
|
-
)
|
|
629
|
-
break
|
|
630
|
-
}
|
|
631
|
-
case "del":
|
|
632
|
-
result.push(
|
|
633
|
-
<span key={key} fg={theme.textMuted}>
|
|
634
|
-
{"~"}
|
|
635
|
-
<InlineTokens
|
|
636
|
-
onLinkActivate={onLinkActivate}
|
|
637
|
-
tokens={(token as Tokens.Del).tokens || []}
|
|
638
|
-
theme={theme}
|
|
639
|
-
/>
|
|
640
|
-
{"~"}
|
|
641
|
-
</span>,
|
|
642
|
-
)
|
|
643
|
-
break
|
|
644
|
-
case "image": {
|
|
645
|
-
const imgToken = token as Tokens.Image
|
|
646
|
-
result.push(
|
|
647
|
-
<span key={key} fg={theme.textMuted}>
|
|
648
|
-
{imgToken.text || imgToken.href}
|
|
649
|
-
</span>,
|
|
650
|
-
)
|
|
651
|
-
break
|
|
652
|
-
}
|
|
653
|
-
case "br":
|
|
654
|
-
result.push("\n")
|
|
655
|
-
break
|
|
656
|
-
case "escape":
|
|
657
|
-
result.push((token as Tokens.Escape).text)
|
|
658
|
-
break
|
|
659
|
-
case "space":
|
|
660
|
-
result.push(" ")
|
|
661
|
-
break
|
|
662
|
-
default:
|
|
663
|
-
if ("text" in token && typeof (token as { text?: string }).text === "string") {
|
|
664
|
-
result.push((token as { text: string }).text)
|
|
665
|
-
}
|
|
666
|
-
break
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
return <>{result}</>
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
// ---------------------------------------------------------------------------
|
|
674
|
-
// Inline token rendering (TextChunks — for text-table cells)
|
|
675
|
-
// ---------------------------------------------------------------------------
|
|
676
|
-
|
|
677
|
-
function inlineTokensToChunks(tokens: Token[], theme: ResolvedTheme): TextChunk[] {
|
|
678
|
-
const chunks: TextChunk[] = []
|
|
679
|
-
|
|
680
|
-
for (const token of tokens) {
|
|
681
|
-
switch (token.type) {
|
|
682
|
-
case "text":
|
|
683
|
-
chunks.push({ __isChunk: true as const, text: (token as Tokens.Text).text })
|
|
684
|
-
break
|
|
685
|
-
case "strong":
|
|
686
|
-
for (const sub of inlineTokensToChunks((token as Tokens.Strong).tokens || [], theme)) {
|
|
687
|
-
chunks.push(boldChunk(sub))
|
|
688
|
-
}
|
|
689
|
-
break
|
|
690
|
-
case "em":
|
|
691
|
-
for (const sub of inlineTokensToChunks((token as Tokens.Em).tokens || [], theme)) {
|
|
692
|
-
chunks.push(italicChunk(sub))
|
|
693
|
-
}
|
|
694
|
-
break
|
|
695
|
-
case "codespan":
|
|
696
|
-
chunks.push({
|
|
697
|
-
__isChunk: true as const,
|
|
698
|
-
text: ` ${(token as Tokens.Codespan).text} `,
|
|
699
|
-
fg: parseColor(theme.markdownCode),
|
|
700
|
-
bg: parseColor(theme.backgroundPanel),
|
|
701
|
-
})
|
|
702
|
-
break
|
|
703
|
-
case "link": {
|
|
704
|
-
const linkToken = token as Tokens.Link
|
|
705
|
-
for (const sub of inlineTokensToChunks(linkToken.tokens || [], theme)) {
|
|
706
|
-
chunks.push(underlineChunk({ ...sub, fg: parseColor(theme.markdownLink) }))
|
|
707
|
-
}
|
|
708
|
-
break
|
|
709
|
-
}
|
|
710
|
-
case "escape":
|
|
711
|
-
chunks.push({ __isChunk: true as const, text: (token as Tokens.Escape).text })
|
|
712
|
-
break
|
|
713
|
-
default:
|
|
714
|
-
if ("text" in token && typeof (token as { text?: string }).text === "string") {
|
|
715
|
-
chunks.push({ __isChunk: true as const, text: (token as { text: string }).text })
|
|
716
|
-
}
|
|
717
|
-
break
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
return chunks
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
// ---------------------------------------------------------------------------
|
|
725
|
-
// Plain text extraction (only for width computation, not rendering)
|
|
726
|
-
// ---------------------------------------------------------------------------
|
|
727
|
-
|
|
728
|
-
function getPlainText(tokens: Token[]): string {
|
|
729
|
-
return tokens
|
|
730
|
-
.map((token) => {
|
|
731
|
-
if (token.type === "text") return token.text
|
|
732
|
-
if (token.type === "codespan") return (token as Tokens.Codespan).text
|
|
733
|
-
if ("tokens" in token && token.tokens) return getPlainText(token.tokens as Token[])
|
|
734
|
-
if ("text" in token) return (token as { text: string }).text
|
|
735
|
-
return ""
|
|
736
|
-
})
|
|
737
|
-
.join("")
|
|
738
|
-
}
|