File size: 8,614 Bytes
1851bae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | //! Heading rendering with theme-based styling.
use crate::inline::render_inline_content;
use crate::style::{HeadingStyler, InlineStyler};
use crate::utils::simple_wrap_preserving_spaces;
/// Render a heading with appropriate styling.
pub fn render_heading<S: InlineStyler + HeadingStyler>(
level: u8,
content: &str,
width: usize,
margin: &str,
styler: &S,
) -> Vec<String> {
// Create the heading prefix (e.g., "# ", "## ", etc.)
let prefix = "#".repeat(level as usize);
// For h1, uppercase the content before rendering inline elements
let content_to_render = if level == 1 {
content.to_uppercase()
} else {
content.to_string()
};
// First render inline elements (bold, italic, etc.) in the content
let rendered_content = render_inline_content(&content_to_render, styler);
// Adjust width to account for the prefix (e.g., "# " = 2 chars, "## " = 3
// chars, etc.)
let prefix_display_width = level as usize + 1;
let content_width = width.saturating_sub(prefix_display_width);
let lines = simple_wrap_preserving_spaces(&rendered_content, content_width);
let mut result = Vec::new();
for line in lines {
let formatted = match level {
1 => {
// H1: Bold, left-aligned, uppercase, with dimmed prefix
format!(
"{}\n{}{} {}",
margin,
margin,
styler.dimmed(&styler.h1(&prefix)),
styler.h1(&line)
)
}
2 => {
// H2: Bold, bright color, left-aligned, with dimmed prefix
format!(
"{}\n{}{} {}",
margin,
margin,
styler.dimmed(&styler.h2(&prefix)),
styler.h2(&line)
)
}
3 => {
format!(
"{}{} {}",
margin,
styler.dimmed(&styler.h3(&prefix)),
styler.h3(&line)
)
}
4 => {
format!(
"{}{} {}",
margin,
styler.dimmed(&styler.h4(&prefix)),
styler.h4(&line)
)
}
5 => {
format!(
"{}{} {}",
margin,
styler.dimmed(&styler.h5(&prefix)),
styler.h5(&line)
)
}
_ => {
format!(
"{}{} {}",
margin,
styler.dimmed(&styler.h6(&prefix)),
styler.h6(&line)
)
}
};
result.push(formatted);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
use crate::theme::TagStyler;
fn render(level: u8, content: &str) -> String {
render_heading(level, content, 80, " ", &TagStyler).join("\n")
}
fn render_with_width(level: u8, content: &str, width: usize) -> String {
render_heading(level, content, width, " ", &TagStyler).join("\n")
}
fn render_with_margin(level: u8, content: &str, margin: &str) -> String {
render_heading(level, content, 80, margin, &TagStyler).join("\n")
}
#[test]
fn test_h1_simple() {
insta::assert_snapshot!(render(1, "Hello World"), @"
<dim><h1>#</h1></dim> <h1>HELLO WORLD</h1>
");
}
#[test]
fn test_h2_simple() {
insta::assert_snapshot!(render(2, "Chapter One"), @"
<dim><h2>##</h2></dim> <h2>Chapter One</h2>
");
}
#[test]
fn test_h3_simple() {
insta::assert_snapshot!(render(3, "Section Title"), @" <dim><h3>###</h3></dim> <h3>Section Title</h3>");
}
#[test]
fn test_h4_simple() {
insta::assert_snapshot!(render(4, "Subsection"), @" <dim><h4>####</h4></dim> <h4>Subsection</h4>");
}
#[test]
fn test_h5_simple() {
insta::assert_snapshot!(render(5, "Minor Heading"), @" <dim><h5>#####</h5></dim> <h5>Minor Heading</h5>");
}
#[test]
fn test_h6_simple() {
insta::assert_snapshot!(render(6, "Smallest Heading"), @" <dim><h6>######</h6></dim> <h6>Smallest Heading</h6>");
}
#[test]
fn test_h1_with_inline_bold() {
insta::assert_snapshot!(render(1, "Hello **bold** world"), @"
<dim><h1>#</h1></dim> <h1>HELLO <b>BOLD</b> WORLD</h1>
");
}
#[test]
fn test_h2_with_inline_italic() {
insta::assert_snapshot!(render(2, "Hello *italic* text"), @"
<dim><h2>##</h2></dim> <h2>Hello <i>italic</i> text</h2>
");
}
#[test]
fn test_h3_with_code() {
insta::assert_snapshot!(render(3, "Using `code` here"), @" <dim><h3>###</h3></dim> <h3>Using <code>code</code> here</h3>");
}
#[test]
fn test_heading_level_beyond_6() {
// Level 7+ should fall through to h6 styling
insta::assert_snapshot!(render(7, "Level Seven"), @" <dim><h6>#######</h6></dim> <h6>Level Seven</h6>");
insta::assert_snapshot!(render(10, "Level Ten"), @" <dim><h6>##########</h6></dim> <h6>Level Ten</h6>");
}
#[test]
fn test_empty_content() {
insta::assert_snapshot!(render(1, ""), @"
<dim><h1>#</h1></dim> <h1></h1>
");
}
#[test]
fn test_custom_margin() {
insta::assert_snapshot!(render_with_margin(1, "Title", " "), @"
<dim><h1>#</h1></dim> <h1>TITLE</h1>
");
insta::assert_snapshot!(render_with_margin(3, "Section", ">>> "), @">>> <dim><h3>###</h3></dim> <h3>Section</h3>");
}
#[test]
fn test_no_margin() {
insta::assert_snapshot!(render_with_margin(1, "Title", ""), @"
<dim><h1>#</h1></dim> <h1>TITLE</h1>
");
insta::assert_snapshot!(render_with_margin(3, "Section", ""), @"<dim><h3>###</h3></dim> <h3>Section</h3>");
}
#[test]
fn test_wrapping_narrow_width() {
insta::assert_snapshot!(render_with_width(1, "This is a very long heading that should wrap", 20), @"
<dim><h1>#</h1></dim> <h1>THIS IS A VERY</h1>
<dim><h1>#</h1></dim> <h1>LONG HEADING THAT</h1>
<dim><h1>#</h1></dim> <h1>SHOULD WRAP</h1>
");
}
#[test]
fn test_h3_wrapping() {
insta::assert_snapshot!(render_with_width(3, "A long section title that wraps", 15), @"
<dim><h3>###</h3></dim> <h3>A long</h3>
<dim><h3>###</h3></dim> <h3>section</h3>
<dim><h3>###</h3></dim> <h3>title that</h3>
<dim><h3>###</h3></dim> <h3>wraps</h3>
");
}
#[test]
fn test_h3_wrapping_preserves_korean_word_spaces() {
let actual = render_with_width(3, "한글 공백 보존 확인", 12);
insta::assert_snapshot!(actual, @r"
<dim><h3>###</h3></dim> <h3>한글</h3>
<dim><h3>###</h3></dim> <h3>공백</h3>
<dim><h3>###</h3></dim> <h3>보존</h3>
<dim><h3>###</h3></dim> <h3>확인</h3>
");
}
#[test]
fn test_h3_wrapping_splits_long_tokens() {
let actual = render_with_width(3, "supercalifragilistic", 12);
insta::assert_snapshot!(actual, @r"
<dim><h3>###</h3></dim> <h3>supercal</h3>
<dim><h3>###</h3></dim> <h3>ifragili</h3>
<dim><h3>###</h3></dim> <h3>stic</h3>
");
}
#[test]
fn test_special_characters() {
insta::assert_snapshot!(render(2, "Hello & Goodbye < World >"), @"
<dim><h2>##</h2></dim> <h2>Hello & Goodbye < World ></h2>
");
}
#[test]
fn test_heading_with_link() {
insta::assert_snapshot!(render(3, "See [documentation](https://example.com)"), @r#" <dim><h3>###</h3></dim> <h3>See <a href="https://example.com">documentation</a></h3>"#);
}
#[test]
fn test_mixed_inline_styles() {
insta::assert_snapshot!(render(2, "**Bold** and *italic* and `code`"), @"
<dim><h2>##</h2></dim> <h2><b>Bold</b> and <i>italic</i> and <code>code</code></h2>
");
}
#[test]
fn test_all_levels_structure() {
// H1 and H2 have extra newline prefix
let h1 = render(1, "H1");
let h2 = render(2, "H2");
let h3 = render(3, "H3");
assert!(h1.contains("\n"), "H1 should have newline");
assert!(h2.contains("\n"), "H2 should have newline");
assert!(!h3.starts_with("\n"), "H3 should not start with newline");
}
}
|