Skip to main content

Highlighter

Struct Highlighter 

Source
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

Source

pub fn new() -> Self

Create a new highlighter with the default theme (base16-ocean.dark).

Source

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)”
Source

pub fn syntax_set(&self) -> &SyntaxSet

Get a reference to the syntax set.

Source

pub fn theme_set(&self) -> &ThemeSet

Get a reference to the theme set.

Source

pub fn set_theme(&mut self, theme_name: &str)

Set the current theme.

Source

pub fn theme_name(&self) -> &str

Get the current theme name.

Source

pub fn theme(&self) -> &Theme

Get the current theme.

Source

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)));
Source

pub fn background(&self) -> Option<(u8, u8, u8)>

Get the background override color.

Source

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.

Source

pub fn plain_text(&self) -> &SyntaxReference

Get the plain text syntax (for unknown languages).

Source

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.

Source

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

Source

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.

Source

pub fn highlight(&self, code: &str, language: Option<&str>) -> String

Simple highlight method (backward compatible).

Highlights code and returns ANSI-formatted string.

Source

pub fn themes(&self) -> Vec<&str>

List available theme names.

Source

pub fn languages(&self) -> Vec<&str>

List available language names.

Source

pub fn has_theme(&self, name: &str) -> bool

Check if a theme exists.

Source

pub fn has_language(&self, name: &str) -> bool

Check if a language is supported.

Trait Implementations§

Source§

impl Debug for Highlighter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Highlighter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.