interplane.Get Omega
Read in your language

omega-harness

Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories

Brought by drakestapleton

branch main Branches Tags History

omega-harness / cli / src / ui / Markdown.tsx

127 lines · 3077 bytes Raw Blame History
1import React from 'react';
2import { Box, Text } from 'ink';
3import { THEME } from '../brand.js';
4
5interface MarkdownProps {
6 text: string;
7}
8
9function renderInline(line: string): React.ReactNode[] {
10 const nodes: React.ReactNode[] = [];
11 // Split on inline code `...` and bold **...**
12 const pattern = /(`[^`]+`|\*\*[^*]+\*\*)/g;
13 let lastIndex = 0;
14 let key = 0;
15
16 for (const match of line.matchAll(pattern)) {
17 const idx = match.index ?? 0;
18 if (idx > lastIndex) {
19 nodes.push(<Text key={key++}>{line.slice(lastIndex, idx)}</Text>);
20 }
21 const token = match[0];
22 if (token.startsWith('`') && token.endsWith('`')) {
23 nodes.push(
24 <Text key={key++} color={THEME.code}>
25 {token.slice(1, -1)}
26 </Text>
27 );
28 } else if (token.startsWith('**') && token.endsWith('**')) {
29 nodes.push(
30 <Text key={key++} bold>
31 {token.slice(2, -2)}
32 </Text>
33 );
34 }
35 lastIndex = idx + token.length;
36 }
37
38 if (lastIndex < line.length) {
39 nodes.push(<Text key={key++}>{line.slice(lastIndex)}</Text>);
40 }
41 return nodes.length > 0 ? nodes : [<Text key={0}>{line}</Text>];
42}
43
44export const Markdown: React.FC<MarkdownProps> = ({ text }) => {
45 const lines = text.split(/\r?\n/);
46 const blocks: React.ReactNode[] = [];
47 let inCode = false;
48 let codeLang = '';
49 let codeLines: string[] = [];
50 let blockKey = 0;
51
52 const flushCode = () => {
53 blocks.push(
54 <Box
55 key={`code-${blockKey++}`}
56 flexDirection="column"
57 borderStyle="round"
58 borderColor={THEME.border}
59 paddingX={1}
60 marginY={0}
61 >
62 {codeLang ? (
63 <Text color={THEME.muted} dimColor>
64 {codeLang}
65 </Text>
66 ) : null}
67 <Text color={THEME.code}>{codeLines.join('\n')}</Text>
68 </Box>
69 );
70 codeLines = [];
71 codeLang = '';
72 };
73
74 for (let i = 0; i < lines.length; i++) {
75 const line = lines[i];
76 if (line.trimStart().startsWith('```')) {
77 if (!inCode) {
78 inCode = true;
79 codeLang = line.trimStart().slice(3).trim();
80 } else {
81 inCode = false;
82 flushCode();
83 }
84 continue;
85 }
86
87 if (inCode) {
88 codeLines.push(line);
89 continue;
90 }
91
92 const heading = line.match(/^(#{1,3})\s+(.*)$/);
93 if (heading) {
94 blocks.push(
95 <Text key={`h-${blockKey++}`} bold color={THEME.symbol}>
96 {heading[2]}
97 </Text>
98 );
99 continue;
100 }
101
102 const bullet = line.match(/^(\s*)[-*]\s+(.*)$/);
103 if (bullet) {
104 blocks.push(
105 <Text key={`b-${blockKey++}`}>
106 {bullet[1]}
107 <Text color={THEME.accent}>• </Text>
108 {renderInline(bullet[2])}
109 </Text>
110 );
111 continue;
112 }
113
114 if (line.trim() === '') {
115 blocks.push(<Text key={`sp-${blockKey++}`}> </Text>);
116 continue;
117 }
118
119 blocks.push(<Text key={`p-${blockKey++}`}>{renderInline(line)}</Text>);
120 }
121
122 if (inCode && codeLines.length > 0) {
123 flushCode();
124 }
125
126 return <Box flexDirection="column">{blocks}</Box>;
127};