Skip to content

Commit

Permalink
fix: trim_end when collect title in hast (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed May 16, 2024
1 parent 59b71b7 commit 219617b
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions crates/plugin_header_anchor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn collect_title_in_hast(node: &mut hast::Element) -> (String, String) {
node.children.remove(index);
}

title = title.trim_end().to_string();
(title, id)
}

Expand Down Expand Up @@ -117,7 +118,7 @@ mod tests {

#[test]
fn test_collect_title_in_hast() {
let mut element = hast::Element {
let mut element1 = hast::Element {
tag_name: "h1".to_string(),
properties: vec![],
children: vec![
Expand Down Expand Up @@ -150,10 +151,39 @@ mod tests {
position: None,
};

let mut element2 = hast::Element {
tag_name: "h2".to_string(),
properties: vec![],
children: vec![
Node::Text(hast::Text {
value: "Hello World ".to_string(),
position: None,
}),
Node::MdxJsxElement(hast::MdxJsxElement {
name: Some("foo".to_string()),
attributes: vec![],
children: vec![Node::Text(hast::Text {
value: "bar".to_string(),
position: None,
})],
position: None,
}),
Node::Text(hast::Text {
value: " ".to_string(),
position: None,
}),
],
position: None,
};

assert_eq!(
collect_title_in_hast(&mut element),
collect_title_in_hast(&mut element1),
("HelloWorldWorld".to_string(), "".to_string())
);
assert_eq!(
collect_title_in_hast(&mut element2),
("Hello World".to_string(), "".to_string())
);
}

#[test]
Expand Down

0 comments on commit 219617b

Please sign in to comment.