-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
63 lines (57 loc) · 1.72 KB
/
Copy pathlib.rs
File metadata and controls
63 lines (57 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
mod image;
use crate::image::render_image;
use comrak::nodes::NodeValue;
use comrak::options::{Extension, Parse, Render};
use comrak::{Arena, Options, create_formatter, parse_document};
#[inline(always)]
fn options<'a>() -> Options<'a> {
Options {
extension: Extension {
strikethrough: true,
tagfilter: true,
table: true,
autolink: true,
superscript: true,
footnotes: true,
description_lists: true,
multiline_block_quotes: true,
shortcodes: true,
underline: true,
subscript: true,
spoiler: true,
greentext: true,
insert: true,
..Default::default()
},
parse: Parse {
smart: true,
..Default::default()
},
render: Render {
escape: true,
..Default::default()
},
}
}
create_formatter!(CustomFormatter, {
NodeValue::Image(ref nl) => |context, entering| {
return render_image(context, entering, nl);
},
});
/// The aemark parser and html generator.
pub fn markdown(s: &str) -> String {
let arena = Arena::new();
let options = options();
let root = parse_document(&arena, s, &options);
for node in root.descendants() {
if let NodeValue::Link(ref mut link) = node.data.borrow_mut().value
&& link.url.starts_with("page://")
{
link.url.replace_range(.."page://".len(), "/page/");
}
}
// html result will be at least as long as md input, so let's allocate that
let mut html = String::with_capacity(s.len());
CustomFormatter::format_document(root, &options, &mut html).unwrap();
html
}