Added an interactive toolbar to code blocks, similar to Prism.js, displaying the language name and a copy button.
- Displays the programming language in uppercase
- Positioned on the left side of the toolbar
- Styled with monospace font (Fira Code)
- Automatically detected from markdown code fence language identifier
- One-click copying of code to clipboard
- Visual feedback with icon changes:
- Default: 📋 Copy
- Success: ✅ Copied! (2 seconds)
- Error: ❌ Failed (2 seconds)
- Hover effect with blue color
- Uses Navigator Clipboard API
- Background: Solarized Light
--base2(#eee8d5) - Border: Matches code block border color
- Rounded top corners (bottom corners on code block)
- Clean, minimal design matching app theme
┌─────────────────────────────────────┐
│ JAVASCRIPT [📋 Copy] │ ← Toolbar
├─────────────────────────────────────┤
│ const x = 10; │
│ console.log(x); │ ← Code Block
└─────────────────────────────────────┘
enhanceCodeBlocks(container)- Scans for all
<pre>elements - Extracts language from
language-*class - Creates toolbar with language label and copy button
- Wraps code block in container for proper positioning
- Prevents duplicate enhancement
- View Snippet Modal: Enhanced after markdown rendering
- Live Preview: Enhanced after each update
- Debounced: Works seamlessly with live preview debouncing
The enhancement happens automatically when markdown is rendered:
renderMarkdown(content).then(html => {
container.innerHTML = html;
enhanceCodeBlocks(container); // Add toolbar
});All languages supported by Shiki (100+), including:
- JavaScript/TypeScript
- Python
- Rust
- Java
- C/C++
- Go
- Ruby
- PHP
- Shell/Bash
- HTML/CSS
- SQL
- And many more...
- Copy Function: Requires modern browsers with Clipboard API
- Fallback: Shows error message if clipboard access fails
- Font Awesome Icons: Already included in the app
All styles use Solarized Light variables for consistency:
--base2: Toolbar background--base1: Borders--base01: Language text--blue: Hover states--base00: Button text
Try creating a snippet with:
```javascript const greet = (name) => { console.log(`Hello, ${name}!`); }; greet('World'); ```
You should see:
- "JAVASCRIPT" label on the left
- "Copy" button on the right
- Clicking copy shows "Copied!" feedback
- Code is copied to clipboard
Possible additions:
- Line numbers toggle button
- Expand/collapse for long code blocks
- Direct link to code block
- Download code as file
- Syntax theme switcher