Skip to main content

Module syntect

Module syntect 

Source
Expand description

Syntax highlighting for code blocks using syntect

This plugin will highlight indented code blocks and fenced code blocks. Fenced block will read the first token as language, for example ```rust. unknown language and indented code blocks will be rendered as plain text.

This plugin use InspiredGitHub theme and render inline styles by default. Use set_theme to select another built-in syntect theme. It will panic when get an unknown theme. Use available_themes to view all available themes.

Use set_to_classed or set_to_classed_with_prefix to switch classed mode. In this mode, you need to provide yourself styles. You can also use theme_css get the CSS for selected built-in theme. In inline mode, it will return None.

Fenced code blocks can mark highlighted lines with a {...} line spec in the info string, such as ```rust {1, 3-5}. Line number started with 1.

let mut md = markdown_it::MarkdownIt::new();
markdown_it::plugins::cmark::add(&mut md);
markdown_it::plugins::extra::syntect::add(&mut md);
markdown_it::plugins::extra::syntect::set_theme(&mut md, "base16-ocean.dark");

let html = md.parse("```rust\nfn main() {}\n```").render();
assert!(html.contains(r#"class="language-rust""#));

Structs§

SyntectRule
Replaces code blocks with syntect highlighted HTML.
SyntectSnippet
Rendered HTML produced by the syntect plugin.

Functions§

add
Add the syntect highlighting rule.
available_themes
Return the names of all built-in syntect themes available to this plugin.
set_prefix
Set the class prefix used for line highlighting and classed mode.
set_theme
Set the theme used for syntax highlighting.
set_to_classed
switch to stylesheet-based highlighting mode with default syntect- prefix.
set_to_classed_with_prefix
Switch to stylesheet-based highlighting with a custom class prefix.
theme_css
Generate CSS for selected built-in theme