Skip to content

Commit

Permalink
upgrade comrak to 0.19.0
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Oct 3, 2023
1 parent 5e035a3 commit 5dc3474
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 33 deletions.
77 changes: 75 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ anyhow = { version = "1.0.42", features = ["backtrace"]}
backtrace = "0.3.61"
failure = "0.1.8"
thiserror = "1.0.26"
comrak = { version = "0.18.0", default-features = false }
comrak = { version = "0.19.0", default-features = false }
syntect = { version = "5.0.0", default-features = false, features = ["parsing", "html", "dump-load", "regex-onig"] }
toml = "0.8.0"
schemamama = "0.3"
Expand Down
62 changes: 32 additions & 30 deletions src/web/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::web::highlight;
use comrak::{
adapters::SyntaxHighlighterAdapter, ComrakExtensionOptions, ComrakOptions, ComrakPlugins,
ComrakRenderPlugins,
adapters::SyntaxHighlighterAdapter, ExtensionOptions, Options, Plugins, RenderPlugins,
};
use std::collections::HashMap;

#[derive(Debug)]
struct CodeAdapter<F>(F);

impl<F: Fn(Option<&str>, &str) -> String> SyntaxHighlighterAdapter for CodeAdapter<F> {
impl<F: Fn(Option<&str>, &str) -> String + Send + Sync> SyntaxHighlighterAdapter
for CodeAdapter<F>
{
fn write_highlighted(
&self,
output: &mut dyn std::io::Write,
Expand Down Expand Up @@ -66,28 +67,29 @@ fn write_opening_tag(

fn render_with_highlighter(
text: &str,
highlighter: impl Fn(Option<&str>, &str) -> String,
highlighter: impl Fn(Option<&str>, &str) -> String + Send + Sync,
) -> String {
comrak::markdown_to_html_with_plugins(
text,
&ComrakOptions {
extension: ComrakExtensionOptions {
superscript: true,
table: true,
autolink: true,
tasklist: true,
strikethrough: true,
..ComrakExtensionOptions::default()
},
..ComrakOptions::default()
},
&ComrakPlugins {
render: ComrakRenderPlugins {
codefence_syntax_highlighter: Some(&CodeAdapter(highlighter)),
..Default::default()
},
},
)
let mut extension = ExtensionOptions::default();
extension.superscript = true;
extension.table = true;
extension.autolink = true;
extension.tasklist = true;
extension.strikethrough = true;

let options = Options {
extension,
..Default::default()
};

let code_adapter = CodeAdapter(highlighter);

let mut render = RenderPlugins::default();
render.codefence_syntax_highlighter = Some(&code_adapter);

let mut plugins = Plugins::default();
plugins.render = render;

comrak::markdown_to_html_with_plugins(text, &options, &plugins)
}

/// Wrapper around the Markdown parser and renderer to render markdown
Expand All @@ -99,11 +101,11 @@ pub fn render(text: &str) -> String {
mod test {
use super::render_with_highlighter;
use indoc::indoc;
use std::cell::RefCell;
use std::sync::Mutex;

#[test]
fn ignore_info_string_attributes() {
let highlighted = RefCell::new(vec![]);
let highlighted = Mutex::new(vec![]);

let output = render_with_highlighter(
indoc! {"
Expand All @@ -116,16 +118,16 @@ mod test {
```
"},
|lang, code| {
highlighted
.borrow_mut()
.push((lang.map(str::to_owned), code.to_owned()));
let mut highlighted = highlighted.lock().unwrap();
highlighted.push((lang.map(str::to_owned), code.to_owned()));
code.to_owned()
},
);

assert!(output.matches(r#"<code class="language-rust">"#).count() == 2);
let highlighted = highlighted.lock().unwrap();
assert_eq!(
highlighted.borrow().as_slice(),
highlighted.as_slice(),
[
(Some("rust".into()), "ignore::commas();\n".into()),
(Some("rust".into()), "ignore::spaces();\n".into())
Expand Down

0 comments on commit 5dc3474

Please sign in to comment.