Skip to content

Commit

Permalink
feat: support delete syntax in toc (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Nov 7, 2024
1 parent 78ef9f1 commit 00f645e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/plugin_toc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum NodeType {
Link,
Strong,
Emphasis,
Delete,
}

pub fn extract_text_from_node(node: &mdast::Node, node_type: NodeType) -> String {
Expand Down Expand Up @@ -58,6 +59,13 @@ pub fn extract_text_from_node(node: &mdast::Node, node_type: NodeType) -> String
}
}
}
NodeType::Delete => {
if let mdast::Node::Delete(delete) = node {
if let mdast::Node::Text(text) = &delete.children[0] {
return text.value.clone();
}
}
}
}
String::new()
}
Expand All @@ -79,6 +87,9 @@ pub fn collect_title_in_mdast(heading: &mut Heading) -> (String, String) {
mdast::Node::Emphasis(_) => {
title.push_str(format!("*{}*", extract_text_from_node(child, NodeType::Emphasis)).as_str())
}
mdast::Node::Delete(_) => {
title.push_str(format!("~~{}~~", extract_text_from_node(child, NodeType::Delete)).as_str())
}
mdast::Node::InlineCode(code) => title.push_str(format!("`{}`", code.value).as_str()),
mdast::Node::Link(_) => {
title.push_str(extract_text_from_node(child, NodeType::Link).as_str())
Expand Down Expand Up @@ -201,6 +212,23 @@ mod tests {
],
position: None,
};
let mut heading5 = mdast::Heading {
depth: 1,
children: vec![
mdast::Node::Text(mdast::Text {
value: "Hello".to_string(),
position: None,
}),
mdast::Node::Delete(mdast::Delete {
children: vec![mdast::Node::Text(mdast::Text {
value: "World".to_string(),
position: None,
})],
position: None,
}),
],
position: None,
};

assert_eq!(
collect_title_in_mdast(&mut heading),
Expand All @@ -218,6 +246,10 @@ mod tests {
collect_title_in_mdast(&mut heading4),
("Hello World".to_string(), "".to_string())
);
assert_eq!(
collect_title_in_mdast(&mut heading5),
("Hello~~World~~".to_string(), "".to_string())
);
}

#[test]
Expand Down

0 comments on commit 00f645e

Please sign in to comment.