|
2 | 2 |
|
3 | 3 | use crate::models::PostData; |
4 | 4 | use crate::utils::{clean_title, escape_toml_string}; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +/// Transform an image path to /posts/YYYY-MM-DD-filename.ext format |
| 8 | +/// |
| 9 | +/// # Arguments |
| 10 | +/// * `image_path` - The original image path from the HTML |
| 11 | +/// * `date_prefix` - The date prefix (YYYY-MM-DD) from the post filename |
| 12 | +/// |
| 13 | +/// # Returns |
| 14 | +/// The transformed path in the format /posts/YYYY-MM-DD-filename.ext |
| 15 | +fn transform_image_path(image_path: &str, date_prefix: &str) -> String { |
| 16 | + // Extract just the filename from the path |
| 17 | + let filename = Path::new(image_path) |
| 18 | + .file_name() |
| 19 | + .and_then(|name| name.to_str()) |
| 20 | + .unwrap_or(image_path); |
| 21 | + |
| 22 | + format!("/posts/{}-{}", date_prefix, filename) |
| 23 | +} |
5 | 24 |
|
6 | 25 | /// Generate markdown from post data |
7 | | -pub fn generate_markdown(post_data: &PostData) -> String { |
| 26 | +/// |
| 27 | +/// # Arguments |
| 28 | +/// * `post_data` - The post data to generate markdown from |
| 29 | +/// * `date_prefix` - The date prefix (YYYY-MM-DD) for image paths |
| 30 | +pub fn generate_markdown(post_data: &PostData, date_prefix: &str) -> String { |
8 | 31 | let mut markdown = String::new(); |
9 | 32 |
|
10 | 33 | // Generate TOML front matter |
@@ -101,7 +124,8 @@ pub fn generate_markdown(post_data: &PostData) -> String { |
101 | 124 | if !post_data.images.is_empty() { |
102 | 125 | markdown.push_str("## Images\n\n"); |
103 | 126 | for image_url in &post_data.images { |
104 | | - markdown.push_str(&format!("\n\n", image_url)); |
| 127 | + let transformed_path = transform_image_path(image_url, date_prefix); |
| 128 | + markdown.push_str(&format!("\n\n", transformed_path)); |
105 | 129 | } |
106 | 130 | } |
107 | 131 |
|
@@ -147,3 +171,102 @@ pub fn generate_markdown(post_data: &PostData) -> String { |
147 | 171 |
|
148 | 172 | format!("{}\n", markdown.trim_end()) |
149 | 173 | } |
| 174 | + |
| 175 | +#[cfg(test)] |
| 176 | +mod tests { |
| 177 | + use super::*; |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn test_transform_image_path_standard() { |
| 181 | + assert_eq!( |
| 182 | + transform_image_path("../Photos/Photos from posts/pretty/image.jpg", "2011-11-04"), |
| 183 | + "/posts/2011-11-04-image.jpg" |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + #[test] |
| 188 | + fn test_transform_image_path_complex_path() { |
| 189 | + assert_eq!( |
| 190 | + transform_image_path( |
| 191 | + "../Photos/Photos%20from%20posts/Vasquez%20Rocks/183zw3ui6c0yq.jpg", |
| 192 | + "2012-11-01" |
| 193 | + ), |
| 194 | + "/posts/2012-11-01-183zw3ui6c0yq.jpg" |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn test_transform_image_path_simple_filename() { |
| 200 | + assert_eq!( |
| 201 | + transform_image_path("image.jpg", "2013-07-13"), |
| 202 | + "/posts/2013-07-13-image.jpg" |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn test_transform_image_path_empty_date() { |
| 208 | + assert_eq!(transform_image_path("image.jpg", ""), "/posts/-image.jpg"); |
| 209 | + } |
| 210 | + |
| 211 | + #[test] |
| 212 | + fn test_transform_image_path_url() { |
| 213 | + assert_eq!( |
| 214 | + transform_image_path("http://example.com/path/image.png", "2013-07-13"), |
| 215 | + "/posts/2013-07-13-image.png" |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + #[test] |
| 220 | + fn test_transform_image_path_windows_path() { |
| 221 | + // On non-Windows systems, Path won't recognize Windows separators |
| 222 | + // and will treat the entire string as a filename |
| 223 | + #[cfg(windows)] |
| 224 | + { |
| 225 | + assert_eq!( |
| 226 | + transform_image_path("C:\\Photos\\image.jpg", "2015-01-01"), |
| 227 | + "/posts/2015-01-01-image.jpg" |
| 228 | + ); |
| 229 | + } |
| 230 | + #[cfg(not(windows))] |
| 231 | + { |
| 232 | + // On Unix systems, backslashes are valid filename characters, not separators |
| 233 | + assert_eq!( |
| 234 | + transform_image_path("C:\\Photos\\image.jpg", "2015-01-01"), |
| 235 | + "/posts/2015-01-01-C:\\Photos\\image.jpg" |
| 236 | + ); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + #[test] |
| 241 | + fn test_transform_image_path_absolute_path() { |
| 242 | + assert_eq!( |
| 243 | + transform_image_path("/var/data/photos/image.jpg", "2016-06-15"), |
| 244 | + "/posts/2016-06-15-image.jpg" |
| 245 | + ); |
| 246 | + } |
| 247 | + |
| 248 | + #[test] |
| 249 | + fn test_transform_image_path_special_chars() { |
| 250 | + assert_eq!( |
| 251 | + transform_image_path("../Photos/my photo (1).jpg", "2017-03-20"), |
| 252 | + "/posts/2017-03-20-my photo (1).jpg" |
| 253 | + ); |
| 254 | + } |
| 255 | + |
| 256 | + #[test] |
| 257 | + fn test_transform_image_path_no_extension() { |
| 258 | + assert_eq!( |
| 259 | + transform_image_path("../Photos/image", "2018-12-25"), |
| 260 | + "/posts/2018-12-25-image" |
| 261 | + ); |
| 262 | + } |
| 263 | + |
| 264 | + #[test] |
| 265 | + fn test_transform_image_path_fallback_when_no_filename() { |
| 266 | + // Edge case: if Path can't extract a filename, use original |
| 267 | + assert_eq!( |
| 268 | + transform_image_path("..", "2019-01-01"), |
| 269 | + "/posts/2019-01-01-.." |
| 270 | + ); |
| 271 | + } |
| 272 | +} |
0 commit comments