Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] should trim_end in collect_title_in_mdast when generate toc #41

Closed
Timeless0911 opened this issue May 16, 2024 · 0 comments · Fixed by #42 or #44
Closed

[Bug] should trim_end in collect_title_in_mdast when generate toc #41

Timeless0911 opened this issue May 16, 2024 · 0 comments · Fixed by #42 or #44
Assignees

Comments

@Timeless0911
Copy link
Collaborator

Timeless0911 commented May 16, 2024

If the heading contains some mdx component like

### `font-weight` <Version22 />

The mdx_plugin_toc will return a string contains whitespace at the end
image

pub fn collect_title_in_mdast(heading: &mut Heading) -> (String, String) {
let mut title = String::new();
let mut custom_id = String::new();
for child in &mut heading.children {
match child {
mdast::Node::Text(text) => {
// example: hello world {#custom-id}
// Then we extract the `hello world` as title and `custom-id` as id
let (title_part, id_part) = extract_title_and_id(&text.value);
title.push_str(&title_part);
custom_id = id_part;
}
mdast::Node::Strong(_) => {
title.push_str(format!("**{}**", extract_text_from_node(child, NodeType::Strong)).as_str())
}
mdast::Node::Emphasis(_) => {
title.push_str(format!("*{}*", extract_text_from_node(child, NodeType::Emphasis)).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())
}
_ => continue, // Continue if node is not Text or Code
}
}
(title, custom_id)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant