Skip to content

Commit

Permalink
feat: support nested container syntax in list and component (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jan 13, 2025
1 parent 276eb48 commit 6e704d7
Showing 1 changed file with 184 additions and 1 deletion.
185 changes: 184 additions & 1 deletion crates/plugin_container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,15 @@ fn traverse_children(root: &mut hast::Root) {
let mut container_content_end_index = 0;
let mut index = 0;
while index < root.children.len() {
let child = &root.children[index];
let child = &mut root.children[index];
if let hast::Node::Element(element) = child {
let mut element_as_root = hast::Root {
children: element.children.clone(),
position: element.position.clone(),
};
traverse_children(&mut element_as_root);
element.children = element_as_root.children;

// Meet the start of the container
if !container_content_start {
// e.g. :::tip
Expand Down Expand Up @@ -986,4 +993,180 @@ mod tests {
})
);
}

#[test]
fn test_container_plugin_with_nested_in_list() {
let mut root = hast::Node::Root(hast::Root {
children: vec![
hast::Node::Element(hast::Element {
tag_name: "ol".into(),
properties: vec![],
children: vec![
hast::Node::Element(hast::Element {
tag_name: "p".into(),
properties: vec![],
children: vec![hast::Node::Text(hast::Text {
value: ":::tip Note".into(),
position: None,
})],
position: None,
}),
hast::Node::Element(hast::Element {
tag_name: "p".into(),
properties: vec![],
children: vec![hast::Node::Text(hast::Text {
value: "This is a tip".into(),
position: None,
})],
position: None,
}),
hast::Node::Element(hast::Element {
tag_name: "p".into(),
properties: vec![],
children: vec![hast::Node::Text(hast::Text {
value: ":::".into(),
position: None,
})],
position: None,
}),
],
position: None,
}),
hast::Node::Element(hast::Element {
tag_name: "ul".into(),
properties: vec![],
children: vec![
hast::Node::Element(hast::Element {
tag_name: "p".into(),
properties: vec![],
children: vec![hast::Node::Text(hast::Text {
value: ":::tip Note".into(),
position: None,
})],
position: None,
}),
hast::Node::Element(hast::Element {
tag_name: "p".into(),
properties: vec![],
children: vec![hast::Node::Text(hast::Text {
value: "This is a tip".into(),
position: None,
})],
position: None,
}),
hast::Node::Element(hast::Element {
tag_name: "p".into(),
properties: vec![],
children: vec![hast::Node::Text(hast::Text {
value: ":::".into(),
position: None,
})],
position: None,
}),
],
position: None,
}),
],
position: None,
});

mdx_plugin_container(&mut root);

assert_eq!(
root,
hast::Node::Root(hast::Root {
children: vec![
hast::Node::Element(hast::Element {
tag_name: "ol".into(),
properties: vec![],
children: vec![hast::Node::Element(hast::Element {
tag_name: "div".into(),
properties: vec![(
"className".into(),
hast::PropertyValue::SpaceSeparated(vec!["rspress-directive".into(), "tip".into()])
),],
children: vec![
hast::Node::Element(hast::Element {
tag_name: "div".into(),
properties: vec![(
"className".into(),
hast::PropertyValue::SpaceSeparated(vec!["rspress-directive-title".into()])
)],
children: vec![hast::Node::Text(hast::Text {
value: "Note".into(),
position: None,
})],
position: None,
}),
hast::Node::Element(hast::Element {
tag_name: "div".into(),
properties: vec![(
"className".into(),
hast::PropertyValue::SpaceSeparated(vec!["rspress-directive-content".into()])
)],
children: vec![hast::Node::Element(hast::Element {
tag_name: "p".into(),
properties: vec![],
children: vec![hast::Node::Text(hast::Text {
value: "This is a tip".into(),
position: None,
})],
position: None,
})],
position: None,
})
],
position: None,
})],
position: None
}),
hast::Node::Element(hast::Element {
tag_name: "ul".into(),
properties: vec![],
children: vec![hast::Node::Element(hast::Element {
tag_name: "div".into(),
properties: vec![(
"className".into(),
hast::PropertyValue::SpaceSeparated(vec!["rspress-directive".into(), "tip".into()])
),],
children: vec![
hast::Node::Element(hast::Element {
tag_name: "div".into(),
properties: vec![(
"className".into(),
hast::PropertyValue::SpaceSeparated(vec!["rspress-directive-title".into()])
)],
children: vec![hast::Node::Text(hast::Text {
value: "Note".into(),
position: None,
})],
position: None,
}),
hast::Node::Element(hast::Element {
tag_name: "div".into(),
properties: vec![(
"className".into(),
hast::PropertyValue::SpaceSeparated(vec!["rspress-directive-content".into()])
)],
children: vec![hast::Node::Element(hast::Element {
tag_name: "p".into(),
properties: vec![],
children: vec![hast::Node::Text(hast::Text {
value: "This is a tip".into(),
position: None,
})],
position: None,
})],
position: None,
})
],
position: None,
})],
position: None
})
],
position: None,
})
);
}
}

0 comments on commit 6e704d7

Please sign in to comment.