| 1 | import { Text } from "ink"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { |
| 5 | defaultTheme, |
| 6 | detectLanguage, |
| 7 | highlightCode, |
| 8 | SyntaxHighlighterTheme, |
| 9 | } from "./SyntaxHighlighter.js"; |
| 10 | |
| 11 | interface MarkdownRendererProps { |
| 12 | content: string | null | undefined; |
| 13 | theme?: SyntaxHighlighterTheme; |
| 14 | } |
| 15 | |
| 16 | interface MarkdownPattern { |
| 17 | regex: RegExp; |
| 18 | render: (content: string, key: string) => React.ReactNode; |
| 19 | } |
| 20 | |
| 21 | const MarkdownRenderer: React.FC<MarkdownRendererProps> = React.memo( |
| 22 | ({ content, theme = defaultTheme }) => { |
| 23 | const patterns: MarkdownPattern[] = [ |
| 24 | { |
| 25 | regex: /```(?:(\w+)\n)?([\s\S]*?)```/g, |
| 26 | render: (_content, _key) => { |
| 27 | // content here is the captured group, but we need to parse the full match |
| 28 | return null; // This will be handled separately |
| 29 | }, |
| 30 | }, |
| 31 | { |
| 32 | regex: /<think>([\s\S]*?)<\/think>/g, |
| 33 | render: (content, key) => ( |
| 34 | <Text key={key} color="dim"> |
| 35 | {content.trim()} |
| 36 | </Text> |
| 37 | ), |
| 38 | }, |
| 39 | { |
| 40 | regex: /^(#{1,6})\s+(.+)$/gm, |
| 41 | render: (content, key) => ( |
| 42 | <Text key={key} bold> |
| 43 | {content} |
| 44 | </Text> |
| 45 | ), |
| 46 | }, |
| 47 | { |
| 48 | regex: /\*\*(.+?)\*\*/g, |
| 49 | render: (content, key) => ( |
| 50 | <Text key={key} bold> |
| 51 | {content} |
| 52 | </Text> |
| 53 | ), |
| 54 | }, |
| 55 | { |
| 56 | regex: /\*([^\s*][^*]*[^\s*]|[^\s*])\*/g, |
| 57 | render: (content, key) => ( |
| 58 | <Text key={key} italic> |
| 59 | {content} |
| 60 | </Text> |
| 61 | ), |
| 62 | }, |
| 63 | { |
| 64 | regex: /_([^_]+)_/g, |
| 65 | render: (content, key) => ( |
| 66 | <Text key={key} italic> |
| 67 | {content} |
| 68 | </Text> |
| 69 | ), |
| 70 | }, |
| 71 | { |
| 72 | regex: /~~([^~]+)~~/g, |
| 73 | render: (content, key) => ( |
| 74 | <Text key={key} strikethrough> |
| 75 | {content} |
| 76 | </Text> |
| 77 | ), |
| 78 | }, |
| 79 | { |
| 80 | regex: /`([^`\n]+)`/g, |
| 81 | render: (content, key) => ( |
| 82 | <Text key={key} color="magentaBright"> |
| 83 | {content} |
| 84 | </Text> |
| 85 | ), |
| 86 | }, |
| 87 | ]; |
| 88 | |
| 89 | const renderMarkdown = (text: string | null | undefined) => { |
| 90 | const parts: React.ReactNode[] = []; |
| 91 | |
| 92 | // Handle null/undefined text |
| 93 | if (!text) { |
| 94 | return parts; |
| 95 | } |
| 96 | |
| 97 | let currentIndex = 0; |
| 98 | |
| 99 | // First, handle code blocks separately |
| 100 | const codeBlockRegex = /```(?:(\w+)\n)?([\s\S]*?)```/g; |
| 101 | const codeBlocks: Array<{ |
| 102 | index: number; |
| 103 | length: number; |
| 104 | language: string; |
| 105 | code: string; |
| 106 | }> = []; |
| 107 | |
| 108 | let codeMatch; |
| 109 | while ((codeMatch = codeBlockRegex.exec(text)) !== null) { |
| 110 | const language = codeMatch[1] || detectLanguage(codeMatch[2]); |
| 111 | codeBlocks.push({ |
| 112 | index: codeMatch.index, |
| 113 | length: codeMatch[0].length, |
| 114 | language, |
| 115 | code: codeMatch[2].trim(), |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | // Find all matches for other patterns (excluding code blocks) |
| 120 | const allMatches: Array<{ |
| 121 | index: number; |
| 122 | length: number; |
| 123 | content: string; |
| 124 | render: (content: string, key: string) => React.ReactNode; |
| 125 | }> = []; |
| 126 | |
| 127 | patterns.slice(1).forEach((pattern) => { |
| 128 | // Skip the first pattern (code blocks) |
| 129 | let match: RegExpExecArray | null; |
| 130 | const regex = new RegExp(pattern.regex.source, pattern.regex.flags); |
| 131 | |
| 132 | while ((match = regex.exec(text)) !== null) { |
| 133 | // Skip if this match is inside a code block |
| 134 | const isInCodeBlock = codeBlocks.some( |
| 135 | (block) => |
| 136 | match!.index >= block.index && |
| 137 | match!.index < block.index + block.length, |
| 138 | ); |
| 139 | |
| 140 | if (!isInCodeBlock) { |
| 141 | allMatches.push({ |
| 142 | index: match.index, |
| 143 | length: match[0].length, |
| 144 | content: match[2] || match[1], // Use second capture group for headings, first for others |
| 145 | render: pattern.render, |
| 146 | }); |
| 147 | } |
| 148 | } |
| 149 | }); |
| 150 | |
| 151 | // Combine code blocks and other matches |
| 152 | const combinedMatches: Array<{ |
| 153 | index: number; |
| 154 | length: number; |
| 155 | type: "code" | "other"; |
| 156 | content?: string; |
| 157 | render?: (content: string, key: string) => React.ReactNode; |
| 158 | language?: string; |
| 159 | code?: string; |
| 160 | }> = [ |
| 161 | ...codeBlocks.map((block) => ({ |
| 162 | index: block.index, |
| 163 | length: block.length, |
| 164 | type: "code" as const, |
| 165 | language: block.language, |
| 166 | code: block.code, |
| 167 | })), |
| 168 | ...allMatches.map((match) => ({ |
| 169 | index: match.index, |
| 170 | length: match.length, |
| 171 | type: "other" as const, |
| 172 | content: match.content, |
| 173 | render: match.render, |
| 174 | })), |
| 175 | ]; |
| 176 | |
| 177 | // Sort matches by index to process them in order |
| 178 | combinedMatches.sort((a, b) => a.index - b.index); |
| 179 | |
| 180 | // Process matches, avoiding overlaps |
| 181 | const processedMatches: typeof combinedMatches = []; |
| 182 | for (const match of combinedMatches) { |
| 183 | const overlaps = processedMatches.some( |
| 184 | (processed) => |
| 185 | (match.index >= processed.index && |
| 186 | match.index < processed.index + processed.length) || |
| 187 | (processed.index >= match.index && |
| 188 | processed.index < match.index + match.length), |
| 189 | ); |
| 190 | |
| 191 | if (!overlaps) { |
| 192 | processedMatches.push(match); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Render the text with formatting |
| 197 | processedMatches.forEach((match, idx) => { |
| 198 | // Add text before this match |
| 199 | if (match.index > currentIndex) { |
| 200 | parts.push(text.slice(currentIndex, match.index)); |
| 201 | } |
| 202 | |
| 203 | // Add the formatted match |
| 204 | if (match.type === "code") { |
| 205 | const highlightedCode = highlightCode( |
| 206 | match.code!, |
| 207 | match.language!, |
| 208 | theme, |
| 209 | ); |
| 210 | parts.push( |
| 211 | <Text key={`code-${idx}-${match.index}`}>{highlightedCode}</Text>, |
| 212 | ); |
| 213 | } else { |
| 214 | parts.push( |
| 215 | match.render!(match.content!, `format-${idx}-${match.index}`), |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | currentIndex = match.index + match.length; |
| 220 | }); |
| 221 | |
| 222 | // Add remaining text |
| 223 | if (currentIndex < text.length) { |
| 224 | parts.push(text.slice(currentIndex)); |
| 225 | } |
| 226 | |
| 227 | return parts; |
| 228 | }; |
| 229 | |
| 230 | return <Text>{renderMarkdown(content)}</Text>; |
| 231 | }, |
| 232 | ); |
| 233 | |
| 234 | MarkdownRenderer.displayName = "MarkdownRenderer"; |
| 235 | |
| 236 | export { defaultTheme, MarkdownRenderer }; |
| 237 | export type { SyntaxHighlighterTheme }; |