Add implementation summary for IDE-style code block rendering

This commit is contained in:
spencrmartin
2025-10-31 10:11:03 -04:00
parent 02b38f3ac8
commit 9d49e1fc3f
+195
View File
@@ -0,0 +1,195 @@
# 🎨 IDE-Style Code Block Rendering - Implementation Summary
## ✅ What We Built
Added beautiful IDE-style syntax highlighting for code blocks pasted into the Goose chat input!
## 🚀 Features
### 1. **Code Block Detection**
- Automatically detects triple backtick code fences: ` ```language\ncode\n``` `
- Regex pattern: `/```(\w+)?\n([\s\S]*?)```/g`
- Extracts language identifier (optional) and code content
### 2. **Syntax Highlighting**
- Uses `react-syntax-highlighter` with Prism
- VS Code Dark Plus theme (`vscDarkPlus`)
- Supports all major programming languages:
- JavaScript/TypeScript
- Python
- Java, C++, C#, Go, Rust
- HTML, CSS, JSON, YAML
- SQL, Bash, PowerShell
- And many more!
### 3. **Visual Design**
- **Dark IDE-like appearance** with `#1E1E1E` background
- **Language badge** in top-right corner (e.g., "typescript", "python")
- **Rounded corners** and subtle border
- **Monospace font** for code
- **Proper padding** and spacing
### 4. **Integration**
- Seamlessly integrated with existing features:
- ✅ Action pills `[Action]`
- ✅ Mention pills `@filename`
- ✅ Spell checking (code blocks excluded)
- ✅ Cursor positioning
- ✅ Height synchronization
## 📝 Usage
Simply paste code with triple backticks:
\`\`\`typescript
function greet(name: string): string {
return \`Hello, \${name}!\`;
}
\`\`\`
Or without language (defaults to plain text):
\`\`\`
Some plain text code
\`\`\`
## 🔧 Technical Implementation
### Files Modified
- `ui/desktop/src/components/RichChatInput.tsx`
### Key Changes
1. **Import Syntax Highlighter** (Line ~7):
```typescript
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';
```
2. **Code Block Detection** (Line ~587):
```typescript
const codeBlockRegex = /```(\w+)?\n([\s\S]*?)```/g;
let codeBlockMatch;
codeBlockRegex.lastIndex = 0;
while ((codeBlockMatch = codeBlockRegex.exec(value)) !== null) {
const language = codeBlockMatch[1] || 'text';
const code = codeBlockMatch[2];
allMatches.push({
type: 'codeblock',
match: codeBlockMatch,
index: codeBlockMatch.index,
length: codeBlockMatch[0].length,
content: code,
language: language
});
}
```
3. **Code Block Rendering** (Line ~730):
```typescript
} else if (type === 'codeblock') {
const language = (matchData as any).language || 'text';
const code = content;
parts.push(
<div key={`codeblock-${keyCounter++}`} className="block my-2" style={{ pointerEvents: 'auto' }}>
<div className="relative rounded-lg overflow-hidden bg-[#1E1E1E] border border-gray-700">
{/* Language badge */}
{language && language !== 'text' && (
<div className="absolute top-2 right-2 px-2 py-1 text-xs font-mono text-gray-300 bg-gray-800/80 rounded">
{language}
</div>
)}
{/* Syntax highlighted code */}
<SyntaxHighlighter
language={language}
style={vscDarkPlus}
customStyle={{
margin: 0,
padding: '16px',
background: 'transparent',
fontSize: '0.875rem',
lineHeight: '1.5',
}}
codeTagProps={{
style: {
fontFamily: 'Monaco, Menlo, "Ubuntu Mono", Consolas, source-code-pro, monospace',
}
}}
>
{code}
</SyntaxHighlighter>
</div>
</div>
);
}
```
## 🎯 Benefits
1. **Better Code Readability** - Syntax highlighting makes code much easier to read
2. **Professional Appearance** - IDE-like styling looks polished and modern
3. **Language Awareness** - Automatically detects and highlights based on language
4. **Seamless Integration** - Works perfectly with all existing chat input features
5. **No Breaking Changes** - All existing functionality preserved
## 🧪 Testing
To test, simply paste code into the chat input:
**Example 1 - TypeScript:**
```typescript
interface User {
name: string;
age: number;
}
const greet = (user: User) => {
console.log(`Hello, ${user.name}!`);
};
```
**Example 2 - Python:**
```python
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10))
```
**Example 3 - JavaScript:**
```javascript
const fetchData = async (url) => {
try {
const response = await fetch(url);
return await response.json();
} catch (error) {
console.error('Error:', error);
}
};
```
## 📊 Commit History
```
02b38f3 - Add IDE-style syntax highlighting for code blocks in chat input
```
## 🚀 Next Steps (Future Enhancements)
1. **Copy Button** - Add a copy-to-clipboard button in the top-right
2. **Line Numbers** - Optional line number display
3. **Theme Toggle** - Switch between light/dark code themes
4. **Collapsible Blocks** - For very long code snippets
5. **Code Execution** - Run code snippets directly (for safe languages)
## 🎉 Result
The chat input now provides a beautiful, IDE-quality code viewing experience that makes sharing and discussing code much more pleasant!
---
**Branch**: `spence/textinput-experiment`
**Status**: ✅ Complete and Ready for Testing