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
59#[cfg(feature = "gfm")]
60impl HtmlWriterOptions {
61    /// Enables GFM-specific HTML rendering behaviors.
62    pub fn enable_gfm(mut self, enable: bool) -> Self {
63        self.enable_gfm = enable;
64        self
65    }
66
67    /// A list of HTML tags that should be rendered as text when GFM is enabled.
68    pub fn gfm_disallowed_html_tags(mut self, tags: Vec<EcoString>) -> Self {
69        self.gfm_disallowed_html_tags = tags;
70        self
71    }
72}