Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade comrak to 0.19.0 #2240

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading