#syntax-highlighting #markdown #markdown-syntax #highlighter #syntax-highlighter

highlight-pulldown

Process pulldown-cmark events to apply syntax highlighting to code blocks

5 releases

0.2.2 Jul 22, 2023
0.2.1 Jul 22, 2023
0.2.0 Jul 22, 2023
0.1.1 Jul 18, 2023
0.1.0 Jul 14, 2023

#585 in Text processing

Download history 7/week @ 2024-02-21 14/week @ 2024-02-28 5/week @ 2024-03-06 34/week @ 2024-03-13 16/week @ 2024-03-20 9/week @ 2024-03-27

64 downloads per month
Used in kobold-ssg

GPL-3.0-or-later

20KB
132 lines

pipeline status crates.io

Highlight Pulldown Code

A small library crate to apply syntax highlighting to markdown parsed with pulldown-cmark.

The implementation is based on the discussion at pulldown-cmark#167.

Usage

The crate exposes a single function, highlight. It takes an iterator over pulldown-cmark events and returns a corresponding Vec<pulldown_cmark::Event> where code blocks have been substituted by HTML blocks containing highlighted code.

use highlight_pulldown::highlight_with_theme;

let markdown = r#"
```rust
enum Hello {
    World,
    SyntaxHighlighting,
}
```"#;
let events = pulldown_cmark::Parser::new(markdown);

// apply a syntax highlighting pass to the pulldown_cmark events
let events = highlight_with_theme(events, "base16-ocean.dark").unwrap();

// emit HTML or further process the events as usual
let mut html = String::new();
pulldown_cmark::html::push_html(&mut html, events.into_iter());

For better efficiency, instead of invoking highlight or highlight_with_theme in a hot loop consider creating a PulldownHighlighter object once and use it many times.

Contributing

If you happen to use this package, any feedback is more than welcome.

Contributions in the form of issues or patches via the GitLab repo are even more appreciated.

Dependencies

~2.9–4MB
~67K SLoC