Skip to content

Commit ae73f70

Browse files
authored
fix: generate header anchor for strong/em/del syntax (#64)
1 parent 5e20bef commit ae73f70

File tree

1 file changed

+63
-1
lines changed
  • crates/plugin_header_anchor/src

1 file changed

+63
-1
lines changed

crates/plugin_header_anchor/src/lib.rs

+63-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ fn collect_title_in_hast(node: &mut hast::Element) -> (String, String) {
2929
}
3030
}
3131
hast::Node::Element(element) => {
32-
if element.tag_name == "code" || element.tag_name == "a" {
32+
if element.tag_name == "code"
33+
|| element.tag_name == "a"
34+
|| element.tag_name == "strong"
35+
|| element.tag_name == "em"
36+
|| element.tag_name == "del"
37+
{
3338
for child in &element.children {
3439
if let hast::Node::Text(text) = child {
3540
title.push_str(&text.value);
@@ -176,6 +181,51 @@ mod tests {
176181
position: None,
177182
};
178183

184+
let mut element3 = hast::Element {
185+
tag_name: "h3".to_string(),
186+
properties: vec![],
187+
children: vec![Node::Element(hast::Element {
188+
tag_name: "strong".to_string(),
189+
properties: vec![],
190+
children: vec![Node::Text(hast::Text {
191+
value: "Bold".to_string(),
192+
position: None,
193+
})],
194+
position: None,
195+
})],
196+
position: None,
197+
};
198+
199+
let mut element4 = hast::Element {
200+
tag_name: "h4".to_string(),
201+
properties: vec![],
202+
children: vec![Node::Element(hast::Element {
203+
tag_name: "em".to_string(),
204+
properties: vec![],
205+
children: vec![Node::Text(hast::Text {
206+
value: "Italic".to_string(),
207+
position: None,
208+
})],
209+
position: None,
210+
})],
211+
position: None,
212+
};
213+
214+
let mut element5 = hast::Element {
215+
tag_name: "h5".to_string(),
216+
properties: vec![],
217+
children: vec![Node::Element(hast::Element {
218+
tag_name: "del".to_string(),
219+
properties: vec![],
220+
children: vec![Node::Text(hast::Text {
221+
value: "Strikethrough".to_string(),
222+
position: None,
223+
})],
224+
position: None,
225+
})],
226+
position: None,
227+
};
228+
179229
assert_eq!(
180230
collect_title_in_hast(&mut element1),
181231
("HelloWorldWorld".to_string(), "".to_string())
@@ -184,6 +234,18 @@ mod tests {
184234
collect_title_in_hast(&mut element2),
185235
("Hello World".to_string(), "".to_string())
186236
);
237+
assert_eq!(
238+
collect_title_in_hast(&mut element3),
239+
("Bold".to_string(), "".to_string())
240+
);
241+
assert_eq!(
242+
collect_title_in_hast(&mut element4),
243+
("Italic".to_string(), "".to_string())
244+
);
245+
assert_eq!(
246+
collect_title_in_hast(&mut element5),
247+
("Strikethrough".to_string(), "".to_string())
248+
);
187249
}
188250

189251
#[test]

0 commit comments

Comments
 (0)