Syntax Highlighting
TheCodeHighlight
component provides syntax highlighting for code blocks using Prism.js. It’s a reusable component that handles the highlighting automatically without requiring manual Prism.js imports.
Component Location
Features
- Automatic syntax highlighting using Prism.js
- Multiple language support (JSON, JavaScript, TypeScript, Bash, YAML)
- Clean default theme with proper contrast
- Error handling for invalid code or unsupported languages
- Zero setup - just import and use
Supported Languages
The component comes pre-configured with these languages:json
- JSON datajavascript
- JavaScript codetypescript
- TypeScript codebash
- Shell scripts and commandsyaml
- YAML configuration files
Basic Usage
Props
Prop | Type | Default | Description |
---|---|---|---|
code | string | required | The code to highlight |
language | string | 'javascript' | Language for syntax highlighting |
Examples
JSON Data
JavaScript Code
TypeScript Code
Adding More Languages
To add support for additional languages, edit the component and import the required Prism.js language component:Theme Customization
The component uses the default Prism.js theme (prism.css
). To use a different theme, change the import in CodeHighlight.vue
:
Real-World Example
Here’s how the component is used in the job details page to display JSON payloads:Error Handling
The component handles errors gracefully:- If the specified language is not available, it displays the code without highlighting
- If the code cannot be highlighted (syntax errors), it displays the raw code
- No errors are thrown to the console
Best Practices
- Format code before passing - Use
JSON.stringify(data, null, 2)
for JSON - Choose the correct language - Ensures proper syntax highlighting
- Keep code snippets reasonable - Very large code blocks may impact performance
- Use template literals for multi-line code strings in TypeScript
Related Documentation
- UI Design System - Overall design patterns