pub fn handle_figure(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
if ctx.convert_as_inline {
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(child_handle, parser, output, options, ctx, depth, dom_ctx);
}
}
return;
}
if !output.is_empty() && !output.ends_with("\n\n") {
output.push_str("\n\n");
}
let mut figure_content = String::new();
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(child_handle, parser, &mut figure_content, options, ctx, depth, dom_ctx);
}
}
figure_content = figure_content.replace("\n![", "![");
figure_content = figure_content.replace(" ![", "![");
let trimmed = figure_content.trim_matches(|c| c == '\n' || c == ' ' || c == '\t');
if !trimmed.is_empty() {
output.push_str(trimmed);
if !output.ends_with('\n') {
output.push('\n');
}
if !output.ends_with("\n\n") {
output.push('\n');
}
}
}
}
pub fn handle_figcaption(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let mut text = String::new();
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(child_handle, parser, &mut text, options, ctx, depth + 1, dom_ctx);
}
}
let text = text.trim();
if !text.is_empty() {
if !output.is_empty() {
if output.ends_with("```\n") {
output.push('\n');
} else {
while output.ends_with(' ') || output.ends_with('\t') {
output.pop();
}
if output.ends_with('\n') && !output.ends_with("\n\n") {
output.push('\n');
} else if !output.ends_with('\n') {
output.push_str("\n\n");
}
}
}
output.push('*');
output.push_str(text);
output.push_str("*\n\n");
}
}
}
pub fn handle(
tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
match tag_name {
"figure" => handle_figure(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"figcaption" => handle_figcaption(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
_ => {}
}
}
#[cfg(test)]
mod tests {
#[test]
fn figure_caption_separated_from_image() {
let html = r#"<figure><img src="photo.jpg" alt="Photo"><figcaption>A nice photo</figcaption></figure>"#;
let result = crate::convert(html, None, None).unwrap();
let content = result.content.unwrap_or_default();
assert!(
content.contains(""),
"image should be present: {}",
content
);
assert!(
content.contains("A nice photo"),
"caption should be present: {}",
content
);
let lines: Vec<&str> = content.lines().filter(|l| !l.trim().is_empty()).collect();
let img_line = lines.iter().position(|l| l.contains("![")).unwrap_or(999);
let cap_line = lines.iter().position(|l| l.contains("A nice photo")).unwrap_or(999);
assert!(
cap_line > img_line,
"caption should be on a separate line after image, lines: {:?}",
lines
);
}
}