HighlightAuto Component

HighlightAuto automatically detects the programming language of the provided code using Highlight.js and applies syntax highlighting. Optionally, it can display a language tag overlay showing the detected language.

Key Features

  • Uses highlight.js auto-detection to identify code language
  • Optionally limit detection to specific languages for better accuracy
  • Optional line numbering with customizable starting number
  • Display detected language name in top-right corner
  • Highlight specific lines or ranges with custom background
  • Line numbers stay visible while scrolling horizontally
  • Optional line wrapping for long lines
  • Gracefully falls back to plaintext if detection fails
  • Handles empty or whitespace-only code strings
  • Configurable colors, borders, and backgrounds via CSS variables
  • Supports static, relative, absolute, or sticky positioning
  • Horizontal scroll for wide code blocks

Props

PropTypeDefaultDescription
codestring""Code to highlight with automatic language detection
langtagbooleanfalseShow detected language tag in top-right corner
numberLinebooleanundefinedShow line numbers
hideBorderbooleanundefinedHide border between line numbers and code
wrapLinesbooleanundefinedEnable line wrapping
startingLineNumbernumber1Starting line number for display
highlightedLinesnumber[][]Array of line numbers to highlight
highlightedRanges[number, number][][]Array of line ranges to highlight [start, end]
backgroundColorstringundefinedBackground color for line numbers column
position"static" | "relative" | "absolute" | "sticky""sticky"CSS position for line numbers column
languagesstring[]undefinedOptional subset of languages to restrict detection
classstringundefinedCSS class for the component

Language Tag: Caution - This may not be accurate.

Set langtag to true to display the language name in the top right corner of the code block.

Types

Style

Customize the language tag background, color, numberline style and border-radius using style props.

Examples

HTML

CSS

Javascript

Markdown

Typescript

Python

Rust

Other examples

1
function greet(name) {
2
  return `Hello, ${name}!`;
3
}
1
const App = () => {
2
  const [count, setCount] = useState(0);
3
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
4
};