use crate::nesting::NESTING;
use markdown::mdast;
use std::mem;
pub(super) fn nests(node: &mdast::Node) -> bool {
matches!(
node,
mdast::Node::Blockquote(_) | mdast::Node::ListItem(_) | mdast::Node::FootnoteDefinition(_)
)
}
pub(super) fn too_deep(node: &mdast::Node, depth: usize) -> bool {
(nests(node) || matches!(node, mdast::Node::List(_))) && depth >= NESTING
}
pub(super) fn cut_deep(children: &mut Vec<mdast::Node>) -> Vec<usize> {
let mut cut = Vec::new();
let mut parents = vec![(children, 0)];
while let Some((nodes, depth)) = parents.pop() {
for node in nodes {
if too_deep(node, depth) {
cut.extend(node.position().map(|position| position.start.offset));
*node = plain(mem::replace(node, empty()));
} else {
let nested = depth + usize::from(nests(node));
if let Some(children) = node.children_mut() {
parents.push((children, nested));
}
}
}
}
cut.sort_unstable();
cut
}
pub(super) fn plain(node: mdast::Node) -> mdast::Node {
let position = node.position().cloned();
let mut text = String::new();
let mut pending = vec![node];
while let Some(mut node) = pending.pop() {
if let Some(value) = text_of(&node) {
text.push_str(value);
} else if matches!(node, mdast::Node::Paragraph(_) | mdast::Node::Heading(_))
&& !text.is_empty()
{
text.push(' ');
}
if let Some(children) = node.children_mut() {
pending.extend(mem::take(children).into_iter().rev());
}
}
mdast::Node::Paragraph(mdast::Paragraph {
children: vec![mdast::Node::Text(mdast::Text {
value: text.trim().to_string(),
position: position.clone(),
})],
position,
})
}
fn text_of(node: &mdast::Node) -> Option<&str> {
match node {
mdast::Node::Text(mdast::Text { value, .. })
| mdast::Node::InlineCode(mdast::InlineCode { value, .. })
| mdast::Node::Code(mdast::Code { value, .. })
| mdast::Node::Math(mdast::Math { value, .. })
| mdast::Node::InlineMath(mdast::InlineMath { value, .. })
| mdast::Node::Html(mdast::Html { value, .. })
| mdast::Node::CompmdRaw(mdast::CompmdRaw { value, .. }) => Some(value),
_ => None,
}
}
fn empty() -> mdast::Node {
mdast::Node::Break(mdast::Break { position: None })
}
#[cfg(test)]
mod tests {
use super::*;
fn parsed(text: &str) -> Vec<mdast::Node> {
match markdown::to_mdast(text, &markdown::ParseOptions::gfm()) {
Ok(mdast::Node::Root(root)) => root.children,
_ => Vec::new(),
}
}
fn depth(nodes: &[mdast::Node]) -> usize {
let mut deepest = 0;
let mut pending: Vec<(&mdast::Node, usize)> = nodes.iter().map(|node| (node, 0)).collect();
while let Some((node, depth)) = pending.pop() {
let nested = depth + usize::from(nests(node));
deepest = deepest.max(nested);
pending.extend(
node.children()
.into_iter()
.flatten()
.map(|child| (child, nested)),
);
}
deepest
}
#[test]
fn blocks_past_the_limit_become_their_text() {
let mut nodes = parsed(&format!(
"{}Deep *text*.\n\nAfter.\n",
"> ".repeat(NESTING + 2)
));
let cut = cut_deep(&mut nodes);
assert_eq!(cut.len(), 1, "one cut, at the block that crossed the limit");
assert_eq!(depth(&nodes), NESTING);
let mut innermost = &nodes[0];
while let Some([child]) = innermost.children().map(Vec::as_slice) {
innermost = child;
}
assert_eq!(innermost.to_string(), "Deep text.");
let mut nodes = parsed(&format!("{}At the limit.\n", "> ".repeat(NESTING)));
assert_eq!(cut_deep(&mut nodes), Vec::<usize>::new());
assert_eq!(depth(&nodes), NESTING);
}
#[test]
fn a_block_s_text_keeps_its_paragraphs_apart() {
let node = parsed("> One `code`.\n>\n> # Two\n").remove(0);
assert_eq!(plain(node).to_string(), "One code. Two");
}
}