Skip to content

Commit 9f6c018

Browse files
Add syntax highlighting.
1 parent 21b943c commit 9f6c018

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ pub fn generate_deck(
237237

238238
let md_content = md_content.replace("---", "## NO_HEADING");
239239
let slide_adapter = plugins::SlideAdapter::new();
240+
let syntax_highlighter = plugins::SyntaxHighlighter::new();
240241
let options = comrak::Options::default();
241242
let mut plugins = comrak::Plugins::default();
242243
plugins.render.heading_adapter = Some(&slide_adapter);
244+
plugins.render.codefence_syntax_highlighter = Some(&syntax_highlighter);
243245
let html = comrak::markdown_to_html_with_plugins(&md_content, &options, &plugins);
244246

245247
let generated = generated.replace("$CONTENT", &html);

src/plugins/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
//! comrak plugins we need
22
33
mod slides;
4+
mod syntax;
5+
46
pub use slides::SlideAdapter;
7+
pub use syntax::SyntaxHighlighter;

src/plugins/syntax.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! Our Syntax Highlighting code,
2+
//!
3+
//! A comrak plugin for adding syntax highlighting classes to code blocks.
4+
5+
pub struct SyntaxHighlighter();
6+
7+
impl SyntaxHighlighter {
8+
pub fn new() -> SyntaxHighlighter {
9+
SyntaxHighlighter()
10+
}
11+
}
12+
13+
impl comrak::adapters::SyntaxHighlighterAdapter for SyntaxHighlighter {
14+
fn write_pre_tag(
15+
&self,
16+
output: &mut dyn std::io::Write,
17+
attributes: std::collections::HashMap<String, String>,
18+
) -> std::io::Result<()> {
19+
println!("Pre Attr: {:?}", attributes);
20+
writeln!(output, "<pre>")?;
21+
Ok(())
22+
}
23+
24+
fn write_code_tag(
25+
&self,
26+
output: &mut dyn std::io::Write,
27+
attributes: std::collections::HashMap<String, String>,
28+
) -> std::io::Result<()> {
29+
println!("Code Attr: {:?}", attributes);
30+
let mut lang_class = "";
31+
if let Some(class) = attributes.get("class") {
32+
if let Some(lang) = class.strip_prefix("language-") {
33+
lang_class = lang;
34+
}
35+
}
36+
writeln!(
37+
output,
38+
r#"<code data-trim data-noescape class="{lang_class}">"#
39+
)?;
40+
Ok(())
41+
}
42+
43+
fn write_highlighted(
44+
&self,
45+
output: &mut dyn std::io::Write,
46+
lang: Option<&str>,
47+
code: &str,
48+
) -> std::io::Result<()> {
49+
println!("Got lang {:?}", lang);
50+
writeln!(output, "{}", code)?;
51+
Ok(())
52+
}
53+
}

0 commit comments

Comments
 (0)