cmark_writer/writer/html/options.rs
1/// Options for configuring the HTML rendering process.
2///
3/// `HtmlWriterOptions` allows customizing how HTML is generated when rendering
4/// CommonMark content. These options can be used directly with an `HtmlWriter` or
5/// can be derived from a `CommonMarkWriter`'s options when rendering HTML elements
6/// within CommonMark content.
7///
8/// # Example
9///
10/// ```rust
11/// use cmark_writer::{HtmlWriter, HtmlWriterOptions};
12///
13/// // Create custom HTML rendering options
14/// let options = HtmlWriterOptions {
15/// strict: true,
16/// code_block_language_class_prefix: Some("language-".to_string()),
17/// #[cfg(feature = "gfm")]
18/// enable_gfm: true,
19/// #[cfg(feature = "gfm")]
20/// gfm_disallowed_html_tags: vec!["script".to_string()],
21/// };
22///
23/// // Use the options with an HtmlWriter
24/// let mut writer = HtmlWriter::with_options(options);
25/// ```
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct HtmlWriterOptions {
28 /// A prefix for the class name applied to fenced code blocks.
29 /// For example, if set to "lang-", a Rust code block might get class "lang-rust".
30 /// If None, no language class is added.
31 pub code_block_language_class_prefix: Option<String>,
32
33 /// Enables GFM-specific HTML rendering behaviors.
34 #[cfg(feature = "gfm")]
35 pub enable_gfm: bool,
36 /// A list of HTML tags that should be rendered as text when GFM is enabled.
37 #[cfg(feature = "gfm")]
38 pub gfm_disallowed_html_tags: Vec<String>,
39
40 /// Determines if HTML parsing/rendering errors should be strict (panic/Err) or lenient (warn and attempt to recover/textualize).
41 pub strict: bool,
42}
43
44impl Default for HtmlWriterOptions {
45 fn default() -> Self {
46 Self {
47 code_block_language_class_prefix: Some("language-".to_string()),
48 #[cfg(feature = "gfm")]
49 enable_gfm: false, // Default to false, cmark.rs options should override
50 #[cfg(feature = "gfm")]
51 gfm_disallowed_html_tags: Vec::new(), // Default to empty
52 strict: true, // Default to strict for HTML, can be overridden by cmark.rs options
53 }
54 }
55}
56
57#[cfg(feature = "gfm")]
58impl HtmlWriterOptions {
59 /// Enables GFM-specific HTML rendering behaviors.
60 pub fn enable_gfm(mut self, enable: bool) -> Self {
61 self.enable_gfm = enable;
62 self
63 }
64
65 /// A list of HTML tags that should be rendered as text when GFM is enabled.
66 pub fn gfm_disallowed_html_tags(mut self, tags: Vec<String>) -> Self {
67 self.gfm_disallowed_html_tags = tags;
68 self
69 }
70}