HighlightSvelte Component

Specialized syntax highlighting for Svelte code using XML, JavaScript, and CSS parsers. Intelligently detects the best highlighting approach based on relevance scores. Features line numbers, language tags, and line highlighting.

Key Features

  • Specialized parser for Svelte's XML, JavaScript, and CSS syntax
  • Compares relevance scores to choose the best highlighting approach
  • Combines XML, JavaScript, and CSS highlighting
  • Optional line numbering with customizable starting number
  • Display "svelte" tag in top-right corner
  • Highlight specific lines or ranges with custom background
  • Validates line ranges and warns about invalid inputs
  • Line numbers stay visible while scrolling horizontally
  • Optional line wrapping for long lines
  • Gracefully falls back to plain code if highlighting fails
  • Configurable colors, borders, and backgrounds via CSS variables
  • Horizontal scroll for wide code blocks

Props

PropTypeDefaultDescription
codestring""Svelte code to highlight
langtagbooleanfalseShow "svelte" 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
classstringundefinedCSS class for the component

Types

HighlightSvelte component has the following types:

Style

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

Examples

Use the HighlightSvelte component to highlight your Svelte code. The HighlightSvelte component requires code props. langtag and --langtag-color are optional.

Using different --langtag-color.

Numberline for Svelte file

1
<script lang="ts">
2
  function greet() {
3
    alert('Welcome to Svelte!');
4
  }
5
</script>
6

7
<button onclick={greet}>click me</button>
8

9
<style>
10
  button {
11
    font-size: 2em;
12
  }
13
</style>
14

Using highlightedRanges

Use highlightedLines and/or highlightedRanges props to highlight lines as the following example.

1
<script lang="ts">
2
  function greet() {
3
    alert('Welcome to Svelte!');
4
  }
5
</script>
6

7
<button onclick={greet}>click me</button>
8

9
<style>
10
  button {
11
    font-size: 2em;
12
  }
13
</style>
14