Debian Codegraph @

examples/sample.rs

raw
1 // Samples used in the README. Wanna make sure they work as advertised.
2
3 fn small() {
4 use comrak::{Options, markdown_to_html};
5
6 assert_eq!(
7 markdown_to_html("¡Olá, **世界**!", &Options::default()),
8 "<p>¡Olá, <strong>世界</strong>!</p>\n"
9 );
10 }
11
12 fn large() {
13 use comrak::nodes::NodeValue;
14 use comrak::{Arena, Options, format_html, parse_document};
15
16 fn replace_text(document: &str, orig_string: &str, replacement: &str) -> String {
17 // The returned nodes are created in the supplied Arena, and are bound by its lifetime.
18 let arena = Arena::new();
19
20 // Parse the document into a root `Node`
21 let root = parse_document(&arena, document, &Options::default());
22
23 // Iterate over all the descendants of root.
24 for node in root.descendants() {
25 if let NodeValue::Text(ref mut text) = node.data_mut().value {
26 // If the node is a text node, perform the string replacement.
27 *text = text.to_mut().replace(orig_string, replacement).into()
28 }
29 }
30
31 let mut html = String::new();
32 format_html(root, &Options::default(), &mut html).unwrap();
33
34 html
35 }
36
37 fn main() {
38 let doc = "Hello, pretty world!\n\n1. Do you like [pretty](#) paintings?\n2. Or *pretty* music?\n";
39 let orig = "pretty";
40 let repl = "beautiful";
41 let html = replace_text(doc, orig, repl);
42
43 println!("{}", html);
44 // Output:
45 //
46 // <p>Hello, beautiful world!</p>
47 // <ol>
48 // <li>Do you like <a href="#">beautiful</a> paintings?</li>
49 // <li>Or <em>beautiful</em> music?</li>
50 // </ol>
51 }
52
53 main()
54 }
55
56 fn main() {
57 small();
58 large();
59 }