CodeHighlight.svelte view source
content
The source code to syntax highlight.
stringlang?
Language identifier (e.g., 'ts', 'css', 'html', 'json', 'svelte', 'md').
Purpose:
- When grammar is not provided, used to look up the grammar via syntax_styler.get_lang(lang)
- Used for metadata: sets the data-lang attribute and determines language_supported
Special values:
- null - Explicitly disables syntax highlighting (content rendered as plain text)
- undefined - Falls back to default ('svelte')
Relationship with grammar:
- If both lang and grammar are provided, grammar takes precedence for tokenization
- However, lang is still used for the data-lang attribute and language detection
string | nullmode?
Highlighting mode for this component.
Options:
- 'auto' - Uses CSS Custom Highlight API if supported, falls back to HTML mode
- 'ranges' - Forces CSS Custom Highlight API (requires browser support)
- 'html' - Forces HTML generation with CSS classes
Note: CSS Custom Highlight API has limitations and limited browser support.
Requires importing theme_highlight.css instead of theme.css.
grammar?
Optional custom grammar object for syntax tokenization.
When to use:
- To provide a custom language definition not registered in syntax_styler.langs
- To use a modified/extended version of an existing grammar
- For one-off grammar variations without registering globally
Behavior:
- When provided, this grammar is used for tokenization instead of looking up via lang
- Enables highlighting even if lang is not in the registry (useful for custom languages)
- The lang parameter is still used for metadata (data-lang attribute)
- When undefined, the grammar is automatically looked up via syntax_styler.get_lang(lang)
SyntaxGrammar | undefinedinline?
Whether to render as inline code or block code. Controls display via CSS classes.
booleanwrap?
Whether to wrap long lines in block code.
Sets white-space: pre-wrap instead of white-space: pre.
Behavior:
- Wraps at whitespace (spaces, newlines)
- Long tokens without spaces (URLs, hashes) will still scroll horizontally
- Default false provides traditional code block behavior
Only affects block code (ignored for inline mode).
booleansyntax_styler?
Custom SyntaxStyler instance to use for highlighting. Allows using a different styler with custom grammars or configuration.
children?
Optional snippet to customize how the highlighted markup is rendered. - In HTML mode: receives the generated HTML string - In range mode: receives the plain text content
Snippet<[markup: string]>