pub struct Highlighter { /* private fields */ }Expand description
Syntax highlighter for code blocks.
Wraps syntect to provide a streaming-friendly API with language aliases and background color override support.
Implementations§
Source§impl Highlighter
impl Highlighter
Sourcepub fn with_theme(theme_name: &str) -> Self
pub fn with_theme(theme_name: &str) -> Self
Create a highlighter with a specific theme.
Available built-in themes:
- “base16-ocean.dark”
- “base16-ocean.light”
- “base16-eighties.dark”
- “base16-mocha.dark”
- “InspiredGitHub”
- “Solarized (dark)”
- “Solarized (light)”
Sourcepub fn syntax_set(&self) -> &SyntaxSet
pub fn syntax_set(&self) -> &SyntaxSet
Get a reference to the syntax set.
Sourcepub fn theme_name(&self) -> &str
pub fn theme_name(&self) -> &str
Get the current theme name.
Sourcepub fn set_background(&mut self, color: Option<(u8, u8, u8)>)
pub fn set_background(&mut self, color: Option<(u8, u8, u8)>)
Override the background color for highlighted output.
This removes all token background colors and uses the specified
color for the entire code block. Pass None to use theme defaults.
§Example
use streamdown_syntax::Highlighter;
let mut highlighter = Highlighter::new();
// Set a dark grey background
highlighter.set_background(Some((30, 30, 30)));Sourcepub fn syntax_for_language(&self, language: &str) -> Option<&SyntaxReference>
pub fn syntax_for_language(&self, language: &str) -> Option<&SyntaxReference>
Find syntax definition for a language name.
This first checks for common aliases (py→Python, js→JavaScript, etc.) and then falls back to syntect’s built-in matching.
Sourcepub fn plain_text(&self) -> &SyntaxReference
pub fn plain_text(&self) -> &SyntaxReference
Get the plain text syntax (for unknown languages).
Sourcepub fn new_highlight_state(&self, language: &str) -> HighlightState<'_>
pub fn new_highlight_state(&self, language: &str) -> HighlightState<'_>
Create a new highlight state for streaming.
This is the preferred way to do line-by-line highlighting.
Sourcepub fn highlight_line_with_state(
&self,
line: &str,
state: &mut HighlightState<'_>,
) -> String
pub fn highlight_line_with_state( &self, line: &str, state: &mut HighlightState<'_>, ) -> String
Highlight a single line with streaming state.
This is the preferred method for streaming use cases. It maintains parse state across calls to correctly handle multi-line tokens.
§Returns
The highlighted line as an ANSI-escaped string (without trailing newline).
Sourcepub fn highlight_block(&self, code: &str, language: &str) -> String
pub fn highlight_block(&self, code: &str, language: &str) -> String
Highlight a complete code block.
This is a convenience method for non-streaming use cases. Each line is highlighted and joined with newlines.
Sourcepub fn highlight(&self, code: &str, language: Option<&str>) -> String
pub fn highlight(&self, code: &str, language: Option<&str>) -> String
Simple highlight method (backward compatible).
Highlights code and returns ANSI-formatted string.
Sourcepub fn has_language(&self, name: &str) -> bool
pub fn has_language(&self, name: &str) -> bool
Check if a language is supported.