cmark_writer/writer/html/
options.rs

1use ecow::EcoString;
2
3/// Options for configuring the HTML rendering process.
4///
5/// `HtmlWriterOptions` allows customizing how HTML is generated when rendering
6/// CommonMark content. These options can be used directly with an `HtmlWriter` or
7/// can be derived from a `CommonMarkWriter`'s options when rendering HTML elements
8/// within CommonMark content.
9///
10/// # Example
11///
12/// ```rust
13/// use cmark_writer::{HtmlWriter, HtmlWriterOptions};
14///
15/// // Create custom HTML rendering options
16/// let options = HtmlWriterOptions {
17///     strict: true,
18///     code_block_language_class_prefix: Some("language-".into()),
19///     #[cfg(feature = "gfm")]
20///     enable_gfm: true,
21///     #[cfg(feature = "gfm")]
22///     gfm_disallowed_html_tags: vec!["script".into()],
23/// };
24///
25/// // Use the options with an HtmlWriter
26/// let mut writer = HtmlWriter::with_options(options);
27/// ```
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct HtmlWriterOptions {
30    /// A prefix for the class name applied to fenced code blocks.
31    /// For example, if set to "lang-", a Rust code block might get class "lang-rust".
32    /// If None, no language class is added.
33    pub code_block_language_class_prefix: Option<EcoString>,
34
35    /// Enables GFM-specific HTML rendering behaviors.
36    #[cfg(feature = "gfm")]
37    pub enable_gfm: bool,
38    /// A list of HTML tags that should be rendered as text when GFM is enabled.
39    #[cfg(feature = "gfm")]
40    pub gfm_disallowed_html_tags: Vec<EcoString>,
41
42    /// Determines if HTML parsing/rendering errors should be strict (panic/Err) or lenient (warn and attempt to recover/textualize).
43    pub strict: bool,
44}
45
46impl Default for HtmlWriterOptions {
47    fn default() -> Self {
48        Self {
49            code_block_language_class_prefix: Some("language-".into()),
50            #[cfg(feature = "gfm")]
51            enable_gfm: false, // Default to false, cmark.rs options should override
52            #[cfg(feature = "gfm")]
53            gfm_disallowed_html_tags: Vec::new(), // Default to empty
54            strict: true, // Default to strict for HTML, can be overridden by cmark.rs options
55        }
56    }
57}
58
59impl HtmlWriterOptions {
60    /// Sets the code block language class prefix.
61    pub fn with_code_block_prefix<S: Into<EcoString>>(mut self, prefix: Option<S>) -> Self {
62        self.code_block_language_class_prefix = prefix.map(|p| p.into());
63        self
64    }
65
66    /// Sets strict mode.
67    pub fn with_strict(mut self, strict: bool) -> Self {
68        self.strict = strict;
69        self
70    }
71
72    /// Sets the code block language class prefix.
73    pub fn set_code_block_prefix<S: Into<EcoString>>(&mut self, prefix: Option<S>) {
74        self.code_block_language_class_prefix = prefix.map(|p| p.into());
75    }
76
77    /// Sets strict mode.
78    pub fn set_strict(&mut self, strict: bool) {
79        self.strict = strict;
80    }
81
82    /// Enables GFM-specific HTML rendering behaviors.
83    #[cfg(feature = "gfm")]
84    pub fn with_gfm_enabled(mut self, enable: bool) -> Self {
85        self.enable_gfm = enable;
86        self
87    }
88
89    /// Sets GFM-specific HTML rendering behaviors.
90    #[cfg(feature = "gfm")]
91    pub fn set_gfm_enabled(&mut self, enable: bool) {
92        self.enable_gfm = enable;
93    }
94
95    /// Sets the list of HTML tags that should be rendered as text when GFM is enabled.
96    #[cfg(feature = "gfm")]
97    pub fn with_gfm_disallowed_tags(mut self, tags: Vec<EcoString>) -> Self {
98        self.gfm_disallowed_html_tags = tags;
99        self
100    }
101
102    /// Sets the list of HTML tags that should be rendered as text when GFM is enabled.
103    #[cfg(feature = "gfm")]
104    pub fn set_gfm_disallowed_tags(&mut self, tags: Vec<EcoString>) {
105        self.gfm_disallowed_html_tags = tags;
106    }
107}