CodeHighlight.svelte

Declarations
#

view source

CodeHighlight
#

CodeHighlight.svelte view source

content

The source code to syntax highlight.

type string

lang?

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

type string | null
optional

mode?

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.

optional

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)

type SyntaxGrammar | undefined
optional

inline?

Whether to render as inline code or block code. Controls display via CSS classes.

type boolean
optional

wrap?

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).

type boolean
optional

syntax_styler?

Custom SyntaxStyler instance to use for highlighting. Allows using a different styler with custom grammars or configuration.

optional

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

type Snippet<[markup: string]>
optional

Depends on
#