Skip to content

Syntax Highlighting with Shiki

The CodeHighlight component provides beautiful syntax highlighting powered by Shiki, supporting multiple programming languages, themes (light/dark/auto), line numbers, line highlighting, copy-to-clipboard, and various display options.

  • Multiple Languages: Support for TypeScript, JavaScript, Python, Rust, Go, JSON, YAML, Markdown, and more
  • Theme Support: Light, dark, and auto (system preference) themes
  • Line Numbers: Optional line numbering with customizable starting line
  • Line Highlighting: Highlight specific lines or ranges
  • Copy-to-Clipboard: Built-in copy functionality with visual feedback
  • Responsive Design: Mobile-friendly with proper overflow handling
  • Accessibility: Full keyboard navigation and screen reader support
  • Word Wrap: Optional word wrapping for long lines
Loading syntax highlighting...
Loading syntax highlighting...
Loading syntax highlighting...
Loading syntax highlighting...
Loading syntax highlighting...
Loading syntax highlighting...
Loading syntax highlighting...

Highlight specific lines or ranges using the highlightLines prop:

Loading syntax highlighting...

Start line numbers from any value:

Loading syntax highlighting...

Enable word wrapping for better readability on narrow screens:

Loading syntax highlighting...
Loading syntax highlighting...
Loading syntax highlighting...
Loading syntax highlighting...

The CodeHighlight component can be used anywhere in the documentation:

import CodeHighlight from '../../../components/interactive/CodeHighlight.astro'
<CodeHighlight
code={'your code here'}
language="typescript"
showLineNumbers
/>
PropTypeDefaultDescription
codestring-Code content to highlight (required)
languageBundledLanguage'typescript'Programming language
theme'light' | 'dark' | 'auto''auto'Syntax highlighting theme
showLineNumbersbooleanfalseDisplay line numbers
highlightLinesstring-Lines to highlight (e.g., “1,3-5,7”)
startLineNumbernumber1Starting line number
titlestring-Optional title/filename
showCopyButtonbooleantrueShow copy-to-clipboard button
copyButtonPosition'top-right' | 'top-left' | 'bottom-right' | 'bottom-left''top-right'Copy button position
maxHeightstring | number-Maximum height for scrolling
wordWrapbooleanfalseEnable word wrapping

The component supports all Shiki bundled languages, including:

Common Languages:

  • JavaScript, TypeScript, JSX, TSX
  • Python, Java, C, C++, C#, Go, Rust
  • HTML, CSS, SCSS, Less
  • JSON, YAML, TOML, XML
  • Markdown, MDX

Shell & Scripting:

  • Bash, Shell, PowerShell
  • Lua, Perl, Ruby, PHP

Data & Query:

  • SQL, GraphQL
  • Dockerfile, Makefile

And many more!

When to Use CodeHighlight vs Monaco Editor

Section titled “When to Use CodeHighlight vs Monaco Editor”
  • Displaying static code examples in documentation
  • Showing syntax-highlighted snippets without editing
  • Needing multiple language support in a single page
  • Prioritizing performance and bundle size
  • Building component showcases with code examples
  • Building interactive code playgrounds
  • Providing live code editing capabilities
  • Needing IntelliSense and type checking
  • Creating tutorial environments with editable code
  • Building online IDE features

The CodeHighlight component uses:

  • Shiki: Fast, beautiful syntax highlighter powered by the same engine as VS Code
  • Client-side Rendering: Lazy loading with client:load for optimal performance
  • Theme Detection: Automatic system theme detection in ‘auto’ mode
  • React Integration: Full React component with hooks for state management
  • Astro Wrapper: Seamless integration with Astro’s component system
  • Keyboard Navigation: Full keyboard support for scrolling and interaction
  • Screen Reader Support: Proper ARIA labels and semantic HTML
  • Color Contrast: High contrast themes for better readability
  • Focus Indicators: Visible focus states for all interactive elements
  • Responsive Design: Works on mobile, tablet, and desktop