Skip to content
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
13 changes: 9 additions & 4 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 @@ -37,7 +37,7 @@ docsrs-metadata = { path = "crates/metadata" }
anyhow = { version = "1.0.42", features = ["backtrace"]}
backtrace = "0.3.61"
thiserror = "2.0.3"
comrak = { version = "0.44.0", default-features = false }
comrak = { version = "0.46.0", default-features = false }
syntect = { version = "5.0.0", default-features = false, features = ["parsing", "html", "dump-load", "regex-onig"] }
toml = "0.9.2"
prometheus = { version = "0.14.0", default-features = false }
Expand Down
35 changes: 18 additions & 17 deletions src/web/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::web::highlight;
use comrak::{
ExtensionOptions, Options, Plugins, RenderPlugins, adapters::SyntaxHighlighterAdapter,
};
use std::{collections::HashMap, fmt};
use comrak::{Options, adapters::SyntaxHighlighterAdapter, options};
use std::{borrow::Cow, collections::HashMap, fmt};

type Attributes<'s> = HashMap<&'static str, std::borrow::Cow<'s, str>>;

#[derive(Debug)]
struct CodeAdapter<F>(F, Option<&'static str>);
Expand All @@ -22,41 +22,42 @@ impl<F: Fn(Option<&str>, &str, Option<&str>) -> String + Send + Sync> SyntaxHigh
write!(output, "{}", (self.0)(lang, code, self.1))
}

fn write_pre_tag(
fn write_pre_tag<'s>(
&self,
output: &mut dyn fmt::Write,
attributes: HashMap<String, String>,
) -> Result<(), fmt::Error> {
attributes: Attributes<'s>,
) -> fmt::Result {
write_opening_tag(output, "pre", &attributes)
}

fn write_code_tag(
fn write_code_tag<'s>(
&self,
output: &mut dyn fmt::Write,
attributes: HashMap<String, String>,
) -> Result<(), fmt::Error> {
attributes: Attributes<'s>,
) -> fmt::Result {
// similarly to above, since comrak does not treat `,` as an info-string delimiter it will
// try to apply `class="language-rust,ignore"` for the info-string `rust,ignore`, so we
// have to detect that case and fixup the class here
// TODO: https://github.com/kivikakk/comrak/issues/246
let mut attributes = attributes;
if let Some(classes) = attributes.get_mut("class") {
*classes = classes
let mut updated: String = classes
.split(' ')
.flat_map(|class| [class.split(',').next().unwrap_or(class), " "])
.collect();
// remove trailing ' '
// TODO: https://github.com/rust-lang/rust/issues/79524 or itertools
classes.pop();
updated.pop();
*classes = Cow::Owned(updated);
}
write_opening_tag(output, "code", &attributes)
}
}

fn write_opening_tag(
fn write_opening_tag<'s>(
output: &mut dyn fmt::Write,
tag: &str,
attributes: &HashMap<String, String>,
attributes: &Attributes<'s>,
) -> Result<(), fmt::Error> {
write!(output, "<{tag}")?;
for (attr, val) in attributes {
Expand All @@ -75,7 +76,7 @@ fn render_with_highlighter(
comrak::markdown_to_html_with_plugins(
text,
&Options {
extension: ExtensionOptions {
extension: options::Extension {
superscript: true,
table: true,
autolink: true,
Expand All @@ -85,8 +86,8 @@ fn render_with_highlighter(
},
..Default::default()
},
&Plugins {
render: RenderPlugins {
&options::Plugins {
render: options::RenderPlugins {
codefence_syntax_highlighter: Some(&code_adapter),
..Default::default()
},
Expand Down
Loading