Skip to main content

html_to_markdown_rs/options/
inline_image.rs

1//! Inline image configuration.
2//!
3//! This module provides configuration for controlling how images are rendered
4//! within specific HTML elements.
5
6/// Inline image configuration that specifies contexts where images remain as markdown links.
7///
8/// This is a wrapper type that provides semantic clarity for the vector of element
9/// names where inline images should be preserved.
10#[derive(Debug, Clone)]
11pub struct InlineImageConfig {
12    /// HTML elements where images should remain as markdown links (not converted to alt text)
13    pub keep_inline_images_in: Vec<String>,
14}
15
16impl InlineImageConfig {
17    /// Create a new inline image configuration with an empty list.
18    #[must_use]
19    pub const fn new() -> Self {
20        Self {
21            keep_inline_images_in: Vec::new(),
22        }
23    }
24
25    /// Create a new inline image configuration from a list of element names.
26    ///
27    /// # Arguments
28    ///
29    /// * `elements` - A vector of HTML element names where inline images should be kept
30    #[must_use]
31    pub const fn from_elements(elements: Vec<String>) -> Self {
32        Self {
33            keep_inline_images_in: elements,
34        }
35    }
36
37    /// Add an element name to the list of elements where images are kept inline.
38    ///
39    /// # Arguments
40    ///
41    /// * `element` - The HTML element name to add (e.g., "p", "div")
42    pub fn add_element(&mut self, element: String) {
43        self.keep_inline_images_in.push(element);
44    }
45
46    /// Check if a given element should keep images inline.
47    ///
48    /// # Arguments
49    ///
50    /// * `element` - The HTML element name to check
51    ///
52    /// # Returns
53    ///
54    /// `true` if the element is in the configured list, `false` otherwise
55    #[must_use]
56    pub fn should_keep_images(&self, element: &str) -> bool {
57        self.keep_inline_images_in.iter().any(|e| e == element)
58    }
59}
60
61impl Default for InlineImageConfig {
62    fn default() -> Self {
63        Self::new()
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_inline_image_config_new() {
73        let config = InlineImageConfig::new();
74        assert_eq!(config.keep_inline_images_in.len(), 0);
75    }
76
77    #[test]
78    fn test_inline_image_config_from_elements() {
79        let elements = vec!["p".to_string(), "div".to_string()];
80        let config = InlineImageConfig::from_elements(elements);
81        assert_eq!(config.keep_inline_images_in.len(), 2);
82        assert!(config.should_keep_images("p"));
83        assert!(config.should_keep_images("div"));
84        assert!(!config.should_keep_images("span"));
85    }
86
87    #[test]
88    fn test_inline_image_config_add_element() {
89        let mut config = InlineImageConfig::new();
90        config.add_element("p".to_string());
91        config.add_element("div".to_string());
92
93        assert_eq!(config.keep_inline_images_in.len(), 2);
94        assert!(config.should_keep_images("p"));
95        assert!(config.should_keep_images("div"));
96    }
97
98    #[test]
99    fn test_inline_image_config_should_keep_images() {
100        let config = InlineImageConfig::from_elements(vec!["figure".to_string()]);
101        assert!(config.should_keep_images("figure"));
102        assert!(!config.should_keep_images("p"));
103    }
104
105    #[test]
106    fn test_inline_image_config_default() {
107        let config = InlineImageConfig::default();
108        assert_eq!(config.keep_inline_images_in.len(), 0);
109        assert!(!config.should_keep_images("p"));
110    }
111}