");
output.push_str(&escape_html(&String::from_iter(
&characters[index + 1..end],
)));
output.push_str("");
index = end + 1;
} else {
output.push_str(&escape_html(&character.to_string()));
index += 1;
}
}
'!' if index + 1 < characters.len() && characters[index + 1] == '[' => {
if let Some(closing) = find(&characters, index + 2, ']') {
if closing + 1 < characters.len() && characters[closing + 1] == '(' {
if let Some(end) = find(&characters, closing + 2, ')') {
let alt = String::from_iter(&characters[index + 2..closing]);
let url = String::from_iter(&characters[closing + 2..end]);
output.push_str("");
body.push_str(¶graph.join("
"));
body.push_str("
");
body.push_str(&code.join("\n"));
body.push_str("\n");
continue;
}
if trimmed.is_empty() {
flush_paragraph(&mut paragraph, &mut body);
index += 1;
continue;
}
if is_rule(trimmed) {
flush_paragraph(&mut paragraph, &mut body);
body.push_str("| {cell} | ")); } body.push_str("
|---|
| {cell} | ")); } body.push_str("
"); body.push_str("e.join("\n"); continue; } let bullet = trimmed.starts_with("- ") || trimmed.starts_with("* ") || trimmed.starts_with("+ "); let ordered = trimmed .find('.') .map(|dot| dot > 0 && trimmed[..dot].chars().all(|c| c.is_ascii_digit())) .unwrap_or(false) && trimmed[trimmed.find('.').unwrap() + 1..].starts_with(' '); if bullet || ordered { flush_paragraph(&mut paragraph, &mut body); let tag = if ordered { "ol" } else { "ul" }; body.push_str(&format!("<{tag}>\n")); while index < lines.len() { let item = lines[index].trim(); let is_bullet = item.starts_with("- ") || item.starts_with("* ") || item.starts_with("+ "); let is_ordered = item .find('.') .map(|dot| dot > 0 && item[..dot].chars().all(|c| c.is_ascii_digit())) .unwrap_or(false) && item.len() > item.find('.').unwrap() + 1 && item.as_bytes()[item.find('.').unwrap() + 1] == b' '; if (ordered && !is_ordered) || (bullet && !is_bullet && !is_ordered) { break; } let content = match item.find(' ') { Some(space) => inline(&item[space + 1..]), None => String::new(), }; body.push_str(&format!("
")); body.push_str("
code"), "{html}");
assert!(
html.contains("a link"),
"{html}"
);
}
#[test]
fn renders_lists_code_and_tables() {
let html = to_html(
"- one\n- two\n\n1. first\n2. second\n\n```rust\nlet x = 1;\n```\n\n| A | B |\n| --- | --- |\n| 1 | 2 |\n",
);
assert!(html.contains("let x = 1;"),
"{html}"
);
assert!(html.contains("