Skip to content

Commit b3b0ae5

Browse files
committed
html::collect_text: deal in String, not Vec<u8>.
1 parent c5f78b3 commit b3b0ae5

File tree

3 files changed

+31
-40
lines changed

3 files changed

+31
-40
lines changed

changelog.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# [v0.42.0] - unreleased
2+
3+
Changed APIs:
4+
5+
* `html::collect_text` now returns a `String`. `html::collect_text_append` is
6+
added if you still want to start with your own (`String`) buffer.
7+
* There was no particular reason for this populating a `Vec<u8>` instead of a
8+
`String`; it was just old.
9+
10+
111
# [v0.41.1] - 2025-09-14
212

313
Bug fixes:

examples/headers.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Extract the document title by srching for a level-one header at the root level.
22

3-
use comrak::{
4-
nodes::{AstNode, NodeCode, NodeValue},
5-
parse_document, Arena, Options,
6-
};
3+
use comrak::{html::collect_text, nodes::NodeValue, parse_document, Arena, Options};
74

85
fn main() {
96
println!("{:?}", get_document_title("# Hello\n"));
@@ -25,27 +22,8 @@ fn get_document_title(document: &str) -> String {
2522
continue;
2623
}
2724

28-
let mut text = String::new();
29-
collect_text(node, &mut text);
30-
31-
// The input was already known good UTF-8 (document: &str) so comrak
32-
// guarantees the output will be too.
33-
return text;
25+
return collect_text(node);
3426
}
3527

3628
"Untitled Document".to_string()
3729
}
38-
39-
fn collect_text<'a>(node: &'a AstNode<'a>, output: &mut String) {
40-
match node.data.borrow().value {
41-
NodeValue::Text(ref literal) | NodeValue::Code(NodeCode { ref literal, .. }) => {
42-
output.push_str(literal)
43-
}
44-
NodeValue::LineBreak | NodeValue::SoftBreak => output.push(' '),
45-
_ => {
46-
for n in node.children() {
47-
collect_text(n, output);
48-
}
49-
}
50-
}
51-
}

src/html.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -632,11 +632,8 @@ fn render_heading<'a, T>(
632632
context.write_all(b">")?;
633633

634634
if let Some(ref prefix) = context.options.extension.header_ids {
635-
let mut text_content = Vec::with_capacity(20);
636-
collect_text(node, &mut text_content);
637-
638-
let mut id = String::from_utf8(text_content).unwrap();
639-
id = context.anchorizer.anchorize(id);
635+
let text_content = collect_text(node);
636+
let id = context.anchorizer.anchorize(text_content);
640637
write!(
641638
context,
642639
"<a href=\"#{}\" aria-hidden=\"true\" class=\"anchor\" id=\"{}{}\"></a>",
@@ -648,12 +645,10 @@ fn render_heading<'a, T>(
648645
}
649646
}
650647
Some(adapter) => {
651-
let mut text_content = Vec::with_capacity(20);
652-
collect_text(node, &mut text_content);
653-
let content = String::from_utf8(text_content).unwrap();
648+
let text_content = collect_text(node);
654649
let heading = HeadingMeta {
655650
level: nh.level,
656-
content,
651+
content: text_content,
657652
};
658653

659654
if entering {
@@ -1587,22 +1582,30 @@ fn render_wiki_link<'a, T>(
15871582

15881583
// Helpers
15891584

1585+
/// Recurses through a node and all of its children in depth-first (document)
1586+
/// order, returning the concatenated literal contents of text, code and math
1587+
/// blocks. Line breaks and soft breaks are represented as a single whitespace
1588+
/// character.
1589+
pub fn collect_text<'a>(node: &'a AstNode<'a>) -> String {
1590+
let mut text = String::with_capacity(20);
1591+
collect_text_append(node, &mut text);
1592+
text
1593+
}
1594+
15901595
/// Recurses through a node and all of its children in depth-first (document)
15911596
/// order, appending the literal contents of text, code and math blocks to
15921597
/// an output buffer. Line breaks and soft breaks are represented as a single
15931598
/// whitespace character.
1594-
pub fn collect_text<'a>(node: &'a AstNode<'a>, output: &mut Vec<u8>) {
1599+
pub fn collect_text_append<'a>(node: &'a AstNode<'a>, output: &mut String) {
15951600
match node.data.borrow().value {
15961601
NodeValue::Text(ref literal) | NodeValue::Code(NodeCode { ref literal, .. }) => {
1597-
output.extend_from_slice(literal.as_bytes())
1598-
}
1599-
NodeValue::LineBreak | NodeValue::SoftBreak => output.push(b' '),
1600-
NodeValue::Math(NodeMath { ref literal, .. }) => {
1601-
output.extend_from_slice(literal.as_bytes())
1602+
output.push_str(literal)
16021603
}
1604+
NodeValue::LineBreak | NodeValue::SoftBreak => output.push(' '),
1605+
NodeValue::Math(NodeMath { ref literal, .. }) => output.push_str(literal),
16031606
_ => {
16041607
for n in node.children() {
1605-
collect_text(n, output);
1608+
collect_text_append(n, output);
16061609
}
16071610
}
16081611
}

0 commit comments

Comments
 (0)