katex_wasmbind/options/
mod.rs

1use crate::render_to_string;
2use serde_derive::{Deserialize, Serialize};
3use std::collections::HashMap;
4use wasm_bindgen::JsValue;
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7/// Read <https://katex.org/docs/options.html> for more information.
8pub struct KaTeXOptions {
9    /// Whether to render the math in the display mode.
10    #[serde(rename = "displayMode")]
11    pub display_mode: bool,
12
13    /// If true, display math has `\tags` rendered on the left instead of the right.\
14    /// like `\usepackage[leqno]{amsmath}` in LaTeX.
15    #[serde(rename = "leqno")]
16    pub left_equation_numbers: bool,
17    /// If true, display math renders flush left with a `2em` left margin\
18    /// like `\documentclass[fleqn]` in LaTeX with the amsmath package.
19    #[serde(rename = "fleqn")]
20    pub flush_left_equations: bool,
21    /// Whether to let KaTeX throw a ParseError for invalid LaTeX.
22    #[serde(rename = "throwOnError")]
23    pub throw_on_error: bool,
24    /// Color used for invalid LaTeX.
25
26    /// Specifies a minimum thickness, in ems.
27    #[serde(rename = "minRuleThickness")]
28    pub min_rule_thickness: f64,
29    /// Max size for user-specified sizes.
30    /// If set to `None`, users can make elements and spaces arbitrarily large.
31    #[serde(rename = "maxSize")]
32    pub max_size: f64,
33    /// Limit the number of macro expansions to the specified number.
34    /// If set to `None`, the macro expander will try to fully expand as in LaTeX.
35    #[serde(rename = "maxExpand")]
36    pub max_expand: i32,
37    /// strict mode
38    pub strict: bool,
39    /// Whether to trust users' input.
40    /// Cannot be assigned at the same time with [`OptsBuilder::trust_callback`].
41    pub trust: bool,
42    /// group
43    #[serde(rename = "globalGroup")]
44    pub global_group: bool,
45
46    /// Output format, `html` or `mathml` or `htmlAndMathml`
47    output: String,
48    /// Collection of custom macros.
49    macros: HashMap<String, String>,
50    #[serde(rename = "errorColor")]
51    error_color: String,
52}
53
54impl Default for KaTeXOptions {
55    fn default() -> Self {
56        Self {
57            display_mode: false,
58            output: "html".to_string(),
59            flush_left_equations: false,
60            left_equation_numbers: false,
61            throw_on_error: false,
62            error_color: "#cc0000".to_string(),
63            macros: Default::default(),
64            min_rule_thickness: 0.04,
65            max_size: f64::INFINITY,
66            max_expand: 1000,
67            strict: false,
68            trust: false,
69            global_group: false,
70        }
71    }
72}
73
74impl KaTeXOptions {
75    /// Set output as `\displaystyle`
76    pub fn display_mode() -> KaTeXOptions {
77        KaTeXOptions { display_mode: true, ..KaTeXOptions::default() }
78    }
79    /// Set output as `\inlinestyle`
80    pub fn inline_mode() -> KaTeXOptions {
81        KaTeXOptions { display_mode: false, ..KaTeXOptions::default() }
82    }
83    /// Render formulas to html string.
84    pub fn render(&self, input: &str) -> String {
85        render_to_string(input, &JsValue::from_serde(self).unwrap())
86    }
87}
88
89impl KaTeXOptions {
90    /// Determines the markup language of the output. \
91    /// The valid choices are:
92    /// - `html`: Outputs KaTeX in HTML only.
93    /// - `mathml`: Outputs KaTeX in MathML only.
94    /// - `htmlAndMathml`: Outputs HTML for visual rendering and includes MathML for accessibility. This is the default.
95    pub fn set_output_format(&mut self, format: &str) -> bool {
96        let set = match format.to_ascii_lowercase().as_str() {
97            "html" => "html",
98            "mathml" => "mathml",
99            "htmlandmathml" => "htmlAndMathml",
100            _ => return false,
101        };
102        self.output = String::from(set);
103        return true;
104    }
105    /// Set the color of the error message.
106    pub fn set_error_color(&mut self) -> bool {
107        unimplemented!()
108    }
109    /// Insert a custom macro.
110    pub fn insert_macro_rule(&mut self) -> bool {
111        unimplemented!()
112    }
113}