From 6df962b9fd0e0e2a69e664e6ede0f28b4305e11b Mon Sep 17 00:00:00 2001 From: Phil Schaf Date: Sat, 11 Jul 2026 19:49:48 +0200 Subject: [PATCH 1/7] feat: add option to emit semantic HTML for alerts --- fuzz/fuzz_targets/all_options.rs | 2 ++ fuzz/fuzz_targets/quadratic.rs | 2 ++ src/html.rs | 21 ++++++++++++++++----- src/nodes.rs | 21 +++++++++++++-------- src/parser/options.rs | 29 ++++++++++++++++++++++++++++- src/tests/alerts.rs | 14 ++++++++++++++ 6 files changed, 75 insertions(+), 14 deletions(-) diff --git a/fuzz/fuzz_targets/all_options.rs b/fuzz/fuzz_targets/all_options.rs index 3b740087..f6b608fa 100644 --- a/fuzz/fuzz_targets/all_options.rs +++ b/fuzz/fuzz_targets/all_options.rs @@ -187,6 +187,7 @@ struct FuzzRenderOptions { r#unsafe: bool, escape: bool, list_style: options::ListStyleType, + alert_style: options::AlertStyleType, sourcepos: bool, escaped_char_spans: bool, ignore_empty_links: bool, @@ -209,6 +210,7 @@ impl FuzzRenderOptions { r#unsafe: self.r#unsafe, escape: self.escape, list_style: self.list_style, + alert_style: self.alert_style, sourcepos: self.sourcepos, escaped_char_spans: self.escaped_char_spans, ignore_empty_links: self.ignore_empty_links, diff --git a/fuzz/fuzz_targets/quadratic.rs b/fuzz/fuzz_targets/quadratic.rs index 09178734..bf22ba8a 100644 --- a/fuzz/fuzz_targets/quadratic.rs +++ b/fuzz/fuzz_targets/quadratic.rs @@ -260,6 +260,7 @@ struct FuzzRenderOptions { r#unsafe: bool, escape: bool, list_style: options::ListStyleType, + alert_style: options::AlertStyleType, sourcepos: bool, escaped_char_spans: bool, } @@ -274,6 +275,7 @@ impl FuzzRenderOptions { r#unsafe: self.r#unsafe, escape: self.escape, list_style: self.list_style, + alert_style: self.alert_style, sourcepos: self.sourcepos, escaped_char_spans: self.escaped_char_spans, ..Default::default() diff --git a/src/html.rs b/src/html.rs index 3953efb7..5e73e0da 100644 --- a/src/html.rs +++ b/src/html.rs @@ -24,7 +24,7 @@ use crate::nodes::{ NodeValue, NodeWikiLink, TableAlignment, }; use crate::parser::options::{Options, Plugins}; -use crate::{node_matches, scanners}; +use crate::{node_matches, options, scanners}; #[doc(hidden)] pub use anchorizer::Anchorizer; @@ -1284,13 +1284,21 @@ fn render_alert( ) -> Result { if entering { context.cr()?; - context.write_str("
"
"
")?; + context.write_str(match context.options.render.alert_style { + options::AlertStyleType::Specific => "
", + options::AlertStyleType::Semantic => "", + })?; context.lf()?; } diff --git a/src/nodes.rs b/src/nodes.rs index 78ac3810..0074943f 100644 --- a/src/nodes.rs +++ b/src/nodes.rs @@ -4,7 +4,7 @@ use std::borrow::Cow; use std::cell::RefCell; use std::convert::TryFrom; -use crate::arena_tree; +use crate::{arena_tree, options::AlertStyleType}; #[cfg(feature = "phoenix_heex")] pub use crate::parser::phoenix_heex::{HeexNode, NodeHeexBlock}; #[cfg(feature = "shortcodes")] @@ -587,13 +587,18 @@ impl AlertType { } /// Returns the CSS class to use for an alert type - pub fn css_class(&self) -> &'static str { - match *self { - AlertType::Note => "markdown-alert-note", - AlertType::Tip => "markdown-alert-tip", - AlertType::Important => "markdown-alert-important", - AlertType::Warning => "markdown-alert-warning", - AlertType::Caution => "markdown-alert-caution", + pub fn css_class(&self, style: AlertStyleType) -> &'static str { + match (*self, style) { + (AlertType::Note, AlertStyleType::Specific) => "markdown-alert-note", + (AlertType::Note, AlertStyleType::Semantic) => "note", + (AlertType::Tip, AlertStyleType::Specific) => "markdown-alert-tip", + (AlertType::Tip, AlertStyleType::Semantic) => "tip", + (AlertType::Important, AlertStyleType::Specific) => "markdown-alert-important", + (AlertType::Important, AlertStyleType::Semantic) => "important", + (AlertType::Warning, AlertStyleType::Specific) => "markdown-alert-warning", + (AlertType::Warning, AlertStyleType::Semantic) => "warning", + (AlertType::Caution, AlertStyleType::Specific) => "markdown-alert-caution", + (AlertType::Caution, AlertStyleType::Semantic) => "caution", } } } diff --git a/src/parser/options.rs b/src/parser/options.rs index 5c5bdd2c..092f5037 100644 --- a/src/parser/options.rs +++ b/src/parser/options.rs @@ -1327,6 +1327,22 @@ pub struct Render { #[cfg_attr(feature = "bon", builder(default))] pub tasklist_classes: bool, + /// How to render alert blocks. Options are: + /// + /// * [`AlertStyleType::Specific`] to use `div`s with `markdown-` prefixed classes (default) + /// * [`AlertStyleType::Semantic`] to use `aside`s with an `admonition` class + /// + /// ```rust + /// # use comrak::{markdown_to_html, Options, options::AlertStyleType}; + /// let mut options = Options::default(); + /// options.extension.alerts = true; + /// options.render.alert_style = AlertStyleType::Semantic; + /// assert_eq!(markdown_to_html("> [!note]\n> Something of note", &options), + /// "", })?; context.lf()?; } From 7505f332b3140070487bcc7fd81e132b725b3a0f Mon Sep 17 00:00:00 2001 From: Phil Schaf Date: Sat, 11 Jul 2026 19:56:55 +0200 Subject: [PATCH 3/7] style --- src/parser/options.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/options.rs b/src/parser/options.rs index 092f5037..37f5c139 100644 --- a/src/parser/options.rs +++ b/src/parser/options.rs @@ -1415,7 +1415,7 @@ pub enum ListStyleType { #[derive(Debug, Clone, Copy, Default)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] -/// Options for alert rendering in markdown. See [`Render::alert_style`] for more details. +/// Options for alert rendering in markdown. See [`Render::alert_style`] for more details. pub enum AlertStyleType { /// `div`s with `class="markdown-alert markdown-alert-"` #[default] From ca583ebfbe593024d79ece901cc89809b110206f Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Sun, 12 Jul 2026 10:06:33 +1000 Subject: [PATCH 4/7] typo. --- src/html.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/html.rs b/src/html.rs index 2058b62b..4ff65417 100644 --- a/src/html.rs +++ b/src/html.rs @@ -1295,7 +1295,7 @@ fn render_alert( context.lf()?; context.write_str("

"markdown-alert-title>", + AlertStyleType::Specific => "markdown-alert-title", AlertStyleType::Semantic => "admonition-title", })?; context.write_str("\">")?; From 5ecc991aa214bf15785adfa9d6cee3fcc41d703f Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Sun, 12 Jul 2026 10:07:04 +1000 Subject: [PATCH 5/7] cargo fmt. --- src/html.rs | 8 ++++++-- src/nodes.rs | 2 +- src/tests/alerts.rs | 5 ++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/html.rs b/src/html.rs index 4ff65417..9f22f5fb 100644 --- a/src/html.rs +++ b/src/html.rs @@ -23,7 +23,7 @@ use crate::nodes::{ NodeFootnoteReference, NodeHeading, NodeHtmlBlock, NodeLink, NodeList, NodeMath, NodeTaskItem, NodeValue, NodeWikiLink, TableAlignment, }; -use crate::parser::options::{Options, Plugins, AlertStyleType}; +use crate::parser::options::{AlertStyleType, Options, Plugins}; use crate::{node_matches, scanners}; #[doc(hidden)] @@ -1288,7 +1288,11 @@ fn render_alert( AlertStyleType::Specific => "

"