Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: restore default heading behavior when not using components

Princesseuh d285962c 2ef8b941

+62 -21
+62 -21
crates/maudit/src/content/markdown.rs
··· 358 358 let mut events = Vec::new(); 359 359 360 360 // Do a first pass to collect body events 361 - for (event, _) in Parser::new_ext(content, parser_options).into_offset_iter() { 361 + for event in Parser::new_ext(content, parser_options) { 362 362 match event { 363 363 Event::Start(Tag::MetadataBlock(_)) => { 364 364 in_frontmatter = true; ··· 400 400 } 401 401 } 402 402 403 + // If we don't have a custom heading component, use default heading rendering 404 + if options.is_none_or(|o| o.components.heading.is_none()) { 405 + let headings = find_headings(&events); 406 + 407 + for heading in &headings { 408 + let heading_content = get_text_from_events(&events[heading.start..heading.end]); 409 + let slug: String = slugger.slugify(&heading_content); 410 + 411 + events[heading.start] = Event::Html( 412 + format!( 413 + "<h{} id=\"{}\" class=\"{}\">", 414 + heading.level, 415 + heading.id.clone().unwrap_or(slug), 416 + heading.classes.join(" ") 417 + ) 418 + .into(), 419 + ); 420 + } 421 + } 422 + 403 423 // Second pass: transform events with custom components only if needed 404 424 let final_events = match options { 405 425 Some(options) if options.components.has_any_components() => { ··· 431 451 Event::Start(Tag::Heading { 432 452 level, id, classes, .. 433 453 }) => { 434 - let heading_content = if let Some(end_index) = find_matching_heading_end(events, i) 435 - { 436 - get_text_from_events(&events[i + 1..end_index]) 437 - } else { 438 - String::new() 439 - }; 440 - let slug = slugger.slugify(&heading_content); 441 - let heading_id = id.as_ref().map(|s| s.as_ref()).unwrap_or(&slug); 442 - let classes_vec: Vec<&str> = classes.iter().map(|c| c.as_ref()).collect(); 454 + if let Some(component) = &components.heading { 455 + let heading_content = 456 + if let Some(end_index) = find_matching_heading_end(events, i) { 457 + get_text_from_events(&events[i + 1..end_index]) 458 + } else { 459 + String::new() 460 + }; 461 + let slug = slugger.slugify(&heading_content); 462 + let heading_id = id.as_ref().map(|s| s.as_ref()).unwrap_or(&slug); 463 + let classes_vec: Vec<&str> = classes.iter().map(|c| c.as_ref()).collect(); 443 464 444 - if let Some(component) = &components.heading { 445 465 let custom_html = 446 466 component.render_start(*level as u8, Some(heading_id), &classes_vec); 447 467 transformed.push(Event::Html(custom_html.into())); 448 468 } else { 449 - // Default behavior 450 - transformed.push(Event::Html( 451 - format!( 452 - "<h{} id=\"{}\" class=\"{}\">", 453 - level, 454 - heading_id, 455 - classes_vec.join(" ") 456 - ) 457 - .into(), 458 - )); 469 + transformed.push(event.clone()); 459 470 } 460 471 } 461 472 Event::End(TagEnd::Heading(level)) => { ··· 863 874 // Should be the same as default rendering when no custom components are provided 864 875 assert_eq!(html, default_html); 865 876 } 877 + 878 + #[test] 879 + fn test_default_heading_behavior_with_and_without_options() { 880 + let markdown = r#"# Main Title 881 + 882 + ## Subheading 883 + 884 + ### Another Level"#; 885 + 886 + // Render without any options 887 + let html_no_options = render_markdown(markdown, None); 888 + 889 + // Render with options but no custom heading component 890 + let options_no_heading = MarkdownOptions { 891 + components: MarkdownComponents::new(), 892 + }; 893 + let html_with_empty_options = render_markdown(markdown, Some(&options_no_heading)); 894 + 895 + // Both should produce identical output 896 + assert_eq!(html_no_options, html_with_empty_options); 897 + 898 + // Both should have default heading behavior (id attributes and proper HTML structure) 899 + assert!(html_no_options.contains("id=\"")); 900 + assert!(html_no_options.contains("<h1")); 901 + assert!(html_no_options.contains("<h2")); 902 + assert!(html_no_options.contains("<h3")); 903 + assert!(html_with_empty_options.contains("id=\"")); 904 + } 905 + 906 + 866 907 }