Skip to main content

mdbook_plotly/preprocessor/handlers/
plotly_html_handler.rs

1use plotly::Plot;
2use pulldown_cmark::Event;
3
4pub fn handle(code: Plot) -> Event<'static> {
5    Event::Html(code.to_inline_html(None).into())
6}
7
8fn trim_leading_spaces(text: &mut String) {
9    let mut out = String::with_capacity(text.len());
10    for (idx, line) in text.lines().enumerate() {
11        if idx > 0 {
12            out.push('\n');
13        }
14        out.push_str(line.trim_start());
15    }
16    *text = out;
17}
18
19pub fn inject_header(offline_js_sources: bool) -> Event<'static> {
20    let mut html: String = if offline_js_sources {
21        Plot::offline_js_sources()
22    } else {
23        Plot::online_cdn_js()
24    };
25    trim_leading_spaces(&mut html);
26    Event::Html(html.into())
27}