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
use gloo_utils::document;
pub use katex_wasmbind::KaTeXOptions;
use yew::{prelude::*, Component, Html};
#[derive(Properties, Clone, PartialEq)]
pub struct KaTeXProperties {
pub math: String,
#[prop_or(true)]
pub inline: bool,
#[prop_or(String::from("html"))]
pub output: String,
}
pub struct KaTeX;
impl Component for KaTeX {
type Message = ();
type Properties = KaTeXProperties;
fn create(_ctx: &yew::Context<Self>) -> Self {
Self
}
fn view(&self, ctx: &yew::Context<Self>) -> Html {
let renderer = self.create_renderer(ctx.props());
let t = document().create_element("div").unwrap();
t.set_inner_html(&renderer.render(ctx.props().math.as_str()));
Html::VRef(t.first_child().unwrap())
}
#[cfg(feature = "auto-cdn")]
fn rendered(&mut self, _ctx: &yew::Context<Self>, first_render: bool) {
if first_render {
self.load_cdn().unwrap_or_default()
}
}
}
impl KaTeX {
pub fn create_renderer(&self, props: &KaTeXProperties) -> KaTeXOptions {
let mut render = if props.inline { KaTeXOptions::inline_mode() } else { KaTeXOptions::display_mode() };
render.set_output_format(props.output.as_str());
render
}
pub fn load_cdn(&self) -> Result<(), std::io::Error> {
if document().get_element_by_id("cdn-katex").is_none() {
let head = document().query_selector("head").expect("").expect("");
let t = document().create_element("link").expect("");
t.set_attribute("id", "cdn-katex").expect("");
t.set_attribute("media", "none").expect("");
t.set_attribute("onload", "this.media='all'").expect("");
t.set_attribute("rel", "stylesheet").expect("");
t.set_attribute("href", "https://unpkg.com/katex@0.12.0/dist/katex.min.css").expect("");
head.append_child(&t).expect("");
}
Ok(())
}
}