Skip to main content
The CodeHighlight 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 data
  • javascript - JavaScript code
  • typescript - TypeScript code
  • bash - Shell scripts and commands
  • yaml - YAML configuration files

Basic Usage

Props

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

  1. Format code before passing - Use JSON.stringify(data, null, 2) for JSON
  2. Choose the correct language - Ensures proper syntax highlighting
  3. Keep code snippets reasonable - Very large code blocks may impact performance
  4. Use template literals for multi-line code strings in TypeScript