Skip to content

Commit faace6e

Browse files
sypharjyn514
authored andcommitted
upgrade comrak
1 parent 4e9c1fa commit faace6e

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ anyhow = { version = "1.0.42", features = ["backtrace"]}
4545
backtrace = "0.3.61"
4646
failure = "0.1.8"
4747
thiserror = "1.0.26"
48-
comrak = { version = "0.17.0", default-features = false }
48+
comrak = { version = "0.18.0", default-features = false }
4949
syntect = { version = "5.0.0", default-features = false, features = ["parsing", "html", "dump-load", "regex-onig"] }
5050
toml = "0.7.2"
5151
schemamama = "0.3"

src/web/markdown.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,42 @@ use comrak::{
33
adapters::SyntaxHighlighterAdapter, ComrakExtensionOptions, ComrakOptions, ComrakPlugins,
44
ComrakRenderPlugins,
55
};
6-
use std::{collections::HashMap, fmt::Write};
6+
use std::collections::HashMap;
77

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

1111
impl<F: Fn(Option<&str>, &str) -> String> SyntaxHighlighterAdapter for CodeAdapter<F> {
12-
fn highlight(&self, lang: Option<&str>, code: &str) -> String {
12+
fn write_highlighted(
13+
&self,
14+
output: &mut dyn std::io::Write,
15+
lang: Option<&str>,
16+
code: &str,
17+
) -> std::io::Result<()> {
1318
// comrak does not treat `,` as an info-string delimiter, so we do that here
1419
// TODO: https://github.com/kivikakk/comrak/issues/246
1520
let lang = lang.and_then(|lang| lang.split(',').next());
16-
(self.0)(lang, code)
21+
write!(output, "{}", (self.0)(lang, code))
1722
}
1823

19-
fn build_pre_tag(&self, attributes: &HashMap<String, String>) -> String {
20-
build_opening_tag("pre", attributes)
24+
fn write_pre_tag(
25+
&self,
26+
output: &mut dyn std::io::Write,
27+
attributes: HashMap<String, String>,
28+
) -> std::io::Result<()> {
29+
write_opening_tag(output, "pre", &attributes)
2130
}
2231

23-
fn build_code_tag(&self, attributes: &HashMap<String, String>) -> String {
32+
fn write_code_tag(
33+
&self,
34+
output: &mut dyn std::io::Write,
35+
attributes: HashMap<String, String>,
36+
) -> std::io::Result<()> {
2437
// similarly to above, since comrak does not treat `,` as an info-string delimiter it will
2538
// try to apply `class="language-rust,ignore"` for the info-string `rust,ignore`, so we
2639
// have to detect that case and fixup the class here
2740
// TODO: https://github.com/kivikakk/comrak/issues/246
28-
let mut attributes = attributes.clone();
41+
let mut attributes = attributes;
2942
if let Some(classes) = attributes.get_mut("class") {
3043
*classes = classes
3144
.split(' ')
@@ -35,17 +48,20 @@ impl<F: Fn(Option<&str>, &str) -> String> SyntaxHighlighterAdapter for CodeAdapt
3548
// TODO: https://github.com/rust-lang/rust/issues/79524 or itertools
3649
classes.pop();
3750
}
38-
build_opening_tag("code", &attributes)
51+
write_opening_tag(output, "code", &attributes)
3952
}
4053
}
4154

42-
fn build_opening_tag(tag: &str, attributes: &HashMap<String, String>) -> String {
43-
let mut tag_parts = format!("<{tag}");
55+
fn write_opening_tag(
56+
output: &mut dyn std::io::Write,
57+
tag: &str,
58+
attributes: &HashMap<String, String>,
59+
) -> std::io::Result<()> {
60+
write!(output, "<{tag}")?;
4461
for (attr, val) in attributes {
45-
write!(tag_parts, " {attr}=\"{val}\"").unwrap();
62+
write!(output, " {attr}=\"{val}\"")?;
4663
}
47-
tag_parts.push('>');
48-
tag_parts
64+
write!(output, ">")
4965
}
5066

5167
fn render_with_highlighter(

0 commit comments

Comments
 (0)