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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use disposition_taffy_model::{MdHeadingLevel, MdStyle};
use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
/// A single block-level element from the markdown source.
pub(crate) struct MdBlock {
/// Heading level, or `None` for a paragraph.
pub(crate) heading_level: Option<MdHeadingLevel>,
/// Ordered inline tokens within this block.
pub(crate) tokens: Vec<MdTokenItem>,
}
/// An inline token within a block.
pub(crate) enum MdTokenItem {
/// A single word (no interior whitespace) with its active inline style.
Word {
/// A single word (no interior whitespace).
text: String,
/// Active inline style when this word was emitted.
md_style: MdStyle,
},
/// An inline image.
Image {
/// The image URL.
src: String,
/// Alt text with any trailing `{WxH}` annotation already stripped.
alt: String,
/// Width in pixels from a trailing `{WxH}` annotation in the alt text,
/// e.g. `` yields `explicit_width: Some(80.0)`.
explicit_width: Option<f32>,
/// Height in pixels from the same alt-text annotation.
explicit_height: Option<f32>,
},
/// A line break within a block, typically from a soft break in markdown.
LineBreak,
}
/// Parses a markdown string into an ordered list of [`MdBlock`] values.
pub(crate) struct MdBlocksParser;
struct ImageState {
src: String,
alt_buffer: String,
}
struct StyleStack {
bold_depth: u32,
italic_depth: u32,
strikethrough_depth: u32,
link_dest: Option<String>,
}
enum ListState {
/// Not currently in a list.
None,
/// Inside an ordered list, tracking the current item number.
Ordered { current_number: u64 },
/// Inside an unordered list.
Unordered,
}
impl MdBlocksParser {
/// Parses a markdown string into an ordered list of [`MdBlock`] values.
pub(crate) fn parse(markdown: &str) -> Vec<MdBlock> {
let options = Options::ENABLE_STRIKETHROUGH;
let parser = Parser::new_ext(markdown, options);
let mut blocks: Vec<MdBlock> = Vec::new();
let mut current_block: Option<MdBlock> = None;
let mut style_stack = StyleStack {
bold_depth: 0,
italic_depth: 0,
strikethrough_depth: 0,
link_dest: None,
};
let mut image_state: Option<ImageState> = None;
let mut heading_prefix_pending: Option<String> = None;
let mut list_state = ListState::None;
let mut list_item_prefix_pending: Option<String> = None;
for event in parser {
match event {
Event::Start(Tag::Heading { level, .. }) => {
let heading_level = Self::heading_level_from(level);
// Prepare the heading prefix (e.g., "# " for H1, "## " for H2)
let prefix_count = match level {
pulldown_cmark::HeadingLevel::H1 => 1,
pulldown_cmark::HeadingLevel::H2 => 2,
pulldown_cmark::HeadingLevel::H3 => 3,
pulldown_cmark::HeadingLevel::H4 => 4,
pulldown_cmark::HeadingLevel::H5 => 5,
pulldown_cmark::HeadingLevel::H6 => 6,
};
heading_prefix_pending = Some(format!("{} ", "#".repeat(prefix_count)));
current_block = Some(MdBlock {
heading_level: Some(heading_level),
tokens: vec![],
});
}
Event::End(TagEnd::Heading(_)) => {
if let Some(block) = current_block.take() {
blocks.push(block);
}
}
Event::Start(Tag::Paragraph) => {
current_block = Some(MdBlock {
heading_level: None,
tokens: vec![],
});
}
Event::End(TagEnd::Paragraph) => {
if let Some(block) = current_block.take() {
blocks.push(block);
}
}
Event::Start(Tag::List(first_item_number)) => {
list_state = if let Some(start_number) = first_item_number {
ListState::Ordered {
current_number: start_number,
}
} else {
ListState::Unordered
};
}
Event::End(TagEnd::List(_)) => {
list_state = ListState::None;
}
Event::Start(Tag::Item) => {
// Prepare the list item prefix based on current list state
let prefix = match &mut list_state {
ListState::Ordered { current_number } => {
let prefix = format!("{}. ", current_number);
*current_number += 1;
prefix
}
ListState::Unordered => "- ".to_string(),
ListState::None => String::new(),
};
list_item_prefix_pending = Some(prefix);
current_block = Some(MdBlock {
heading_level: None,
tokens: vec![],
});
}
Event::End(TagEnd::Item) => {
if let Some(block) = current_block.take() {
blocks.push(block);
}
}
Event::Start(Tag::Strong) => {
style_stack.bold_depth += 1;
}
Event::End(TagEnd::Strong) => {
style_stack.bold_depth -= 1;
}
Event::Start(Tag::Emphasis) => {
style_stack.italic_depth += 1;
}
Event::End(TagEnd::Emphasis) => {
style_stack.italic_depth -= 1;
}
Event::Start(Tag::Strikethrough) => {
style_stack.strikethrough_depth += 1;
}
Event::End(TagEnd::Strikethrough) => {
style_stack.strikethrough_depth -= 1;
}
Event::Start(Tag::Link { dest_url, .. }) => {
style_stack.link_dest = Some(String::from(dest_url));
}
Event::End(TagEnd::Link) => {
style_stack.link_dest = None;
}
Event::Code(text) => {
let heading_level = current_block
.as_ref()
.and_then(|current_block| current_block.heading_level);
let md_style = MdStyle {
code: true,
bold: style_stack.bold_depth > 0,
italic: style_stack.italic_depth > 0,
strikethrough: style_stack.strikethrough_depth > 0,
heading_level,
link_dest: style_stack.link_dest.clone(),
};
if let Some(block) = current_block.as_mut() {
// Prepend heading or list item prefix if pending
let code_text = if let Some(prefix) = heading_prefix_pending.take() {
format!("{}{}", prefix, text)
} else if let Some(prefix) = list_item_prefix_pending.take() {
format!("{}{}", prefix, text)
} else {
String::from(text)
};
block.tokens.push(MdTokenItem::Word {
text: code_text,
md_style,
});
}
}
Event::Text(text) => {
if let Some(state) = image_state.as_mut() {
state.alt_buffer.push_str(&text);
} else {
let heading_level = current_block
.as_ref()
.and_then(|current_block| current_block.heading_level);
let md_style = MdStyle {
bold: style_stack.bold_depth > 0,
italic: style_stack.italic_depth > 0,
strikethrough: style_stack.strikethrough_depth > 0,
code: false,
heading_level,
link_dest: style_stack.link_dest.clone(),
};
if let Some(block) = current_block.as_mut() {
let mut words: Vec<&str> = text.split_ascii_whitespace().collect();
// Prepend heading or list item prefix to the first word if pending
if let Some(prefix) = heading_prefix_pending
.take()
.or_else(|| list_item_prefix_pending.take())
{
if let Some(first_word) = words.first_mut() {
let prefixed_word = format!("{}{}", prefix, first_word);
block.tokens.push(MdTokenItem::Word {
text: prefixed_word,
md_style: md_style.clone(),
});
// Add remaining words
for word in &words[1..] {
block.tokens.push(MdTokenItem::Word {
text: word.to_string(),
md_style: md_style.clone(),
});
}
} else {
// No words in text, keep prefix pending
// Note: we can't distinguish which prefix it was, so we
// store it back in heading_prefix_pending as a fallback
heading_prefix_pending = Some(prefix);
}
} else {
// No prefix pending, add words normally
for word in words {
block.tokens.push(MdTokenItem::Word {
text: word.to_string(),
md_style: md_style.clone(),
});
}
}
}
}
}
Event::Start(Tag::Image { dest_url, .. }) => {
image_state = Some(ImageState {
src: String::from(dest_url),
alt_buffer: String::new(),
});
}
Event::End(TagEnd::Image) => {
if let Some(state) = image_state.take() {
let (alt, explicit_width, explicit_height) =
Self::parse_alt_annotation(&state.alt_buffer);
if let Some(block) = current_block.as_mut() {
block.tokens.push(MdTokenItem::Image {
src: state.src,
alt,
explicit_width,
explicit_height,
});
}
}
}
Event::HardBreak => {
if let Some(block) = current_block.as_mut() {
block.tokens.push(MdTokenItem::LineBreak);
}
}
_ => {}
}
}
blocks
}
fn heading_level_from(level: pulldown_cmark::HeadingLevel) -> MdHeadingLevel {
match level {
pulldown_cmark::HeadingLevel::H1 => MdHeadingLevel::H1,
pulldown_cmark::HeadingLevel::H2 => MdHeadingLevel::H2,
pulldown_cmark::HeadingLevel::H3 => MdHeadingLevel::H3,
pulldown_cmark::HeadingLevel::H4 => MdHeadingLevel::H4,
pulldown_cmark::HeadingLevel::H5 => MdHeadingLevel::H5,
pulldown_cmark::HeadingLevel::H6 => MdHeadingLevel::H6,
}
}
/// Strips a trailing `{WxH}` annotation from alt text.
///
/// Returns `(clean_alt, explicit_width, explicit_height)`. The annotation
/// is case-insensitive on the `x` separator, e.g. `{80x60}` or `{80X60}`.
///
/// # Examples
///
/// - `"Logo {80x60}"` yields `("Logo", Some(80.0), Some(60.0))`
/// - `"Logo"` yields `("Logo", None, None)`
fn parse_alt_annotation(alt: &str) -> (String, Option<f32>, Option<f32>) {
let trimmed = alt.trim_end();
if let Some(brace_start) = trimmed.rfind('{') {
let annotation = &trimmed[brace_start..];
if annotation.ends_with('}') {
let brace_content = &annotation[1..annotation.len() - 1];
let lower = brace_content.to_ascii_lowercase();
if let Some(x_pos) = lower.find('x') {
let w_str = &brace_content[..x_pos];
let h_str = &brace_content[x_pos + 1..];
if let (Ok(w), Ok(h)) =
(w_str.trim().parse::<f32>(), h_str.trim().parse::<f32>())
{
let clean_alt = alt[..brace_start].trim_end().to_string();
return (clean_alt, Some(w), Some(h));
}
}
}
}
(alt.to_string(), None, None)
}
}