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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! This crate offers Rust bindings to [KaTeX](https://katex.org).
//! This allows you to render LaTeX equations to HTML.
//!
//! # Usage
//!
//! Add this to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! katex = "0.2"
//! ```
//!
//! # Examples
//!
//! ```
//! let html = katex::render("E = mc^2").unwrap();
//!
//! let opts = katex::Opts::builder().display_mode(true).build().unwrap();
//! let html_in_display_mode = katex::render_with_opts("E = mc^2", opts).unwrap();
//! ```

#[macro_use]
extern crate derive_builder;

use core::convert::TryFrom;
use core::fmt;
use quick_js::{self, Context as JsContext, JsValue};
use std::collections::HashMap;
use std::panic::RefUnwindSafe;
use std::sync::Arc;

const KATEX_SRC: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/katex.min.js"));

thread_local! {
    static KATEX: Result<JsContext> = init_katex();
}

/// Error type for this crate.
#[non_exhaustive]
#[derive(thiserror::Error, Clone, Debug)]
pub enum Error {
    /// Error on js context creation. See [`quick_js::ContextError`].
    #[error("failed to initialize js environment (detail: {0})")]
    JsInitError(String),
    /// Error on js execution. See [`quick_js::ExecutionError`].
    #[error("failed to execute js (detail: {0})")]
    JsExecError(String),
    /// Error on js value conversion. See [`quick_js::ValueError`].
    #[error("failed to convert js value (detail: {0})")]
    JsValueError(String),
}

impl From<quick_js::ContextError> for Error {
    fn from(e: quick_js::ContextError) -> Self {
        Self::JsInitError(format!("{}", e))
    }
}

impl From<quick_js::ExecutionError> for Error {
    fn from(e: quick_js::ExecutionError) -> Self {
        Self::JsExecError(format!("{}", e))
    }
}

impl From<quick_js::ValueError> for Error {
    fn from(e: quick_js::ValueError) -> Self {
        Self::JsValueError(format!("{}", e))
    }
}

/// Alias to `core::result::Result<T, katex::Error>`
pub type Result<T> = core::result::Result<T, Error>;

/// Initialize KaTeX js environment.
fn init_katex() -> Result<JsContext> {
    let ctx = JsContext::new()?;
    let _ = ctx.eval(KATEX_SRC)?;
    let _ = ctx.eval(
        r#"
    function renderToString(input, opts) {
        if (opts.trust === "USE_TRUST_CALLBACK") {
            opts.trust = trustCallback;
        }
        return katex.renderToString(input, opts);
    }
    "#,
    )?;
    Ok(ctx)
}

/// The input used by the [`TrustCallback`].
/// See [`OptsBuilder::trust_callback`].
#[derive(Debug)]
pub struct TrustContext<'a> {
    pub command: &'a str,
    pub url: &'a str,
    pub protocol: &'a str,
}

impl<'a> TryFrom<&'a JsValue> for TrustContext<'a> {
    type Error = quick_js::ValueError;

    fn try_from(input: &'a JsValue) -> core::result::Result<Self, Self::Error> {
        match input {
            JsValue::Object(obj) => {
                let command = obj
                    .get("command")
                    .ok_or_else(|| quick_js::ValueError::UnexpectedType)?
                    .as_str()
                    .ok_or_else(|| quick_js::ValueError::UnexpectedType)?;
                let url = obj
                    .get("url")
                    .ok_or_else(|| quick_js::ValueError::UnexpectedType)?
                    .as_str()
                    .ok_or_else(|| quick_js::ValueError::UnexpectedType)?;
                let protocol = obj
                    .get("protocol")
                    .ok_or_else(|| quick_js::ValueError::UnexpectedType)?
                    .as_str()
                    .ok_or_else(|| quick_js::ValueError::UnexpectedType)?;
                Ok(Self {
                    command,
                    url,
                    protocol,
                })
            }
            _ => Err(quick_js::ValueError::UnexpectedType),
        }
    }
}

/// A callback function to determine whether to trust users' input.
/// It accepts [`TrustContext`] and returns a [`bool`].
/// See [`OptsBuilder::trust_callback`].
#[derive(Clone)]
pub struct TrustCallback(Arc<dyn Fn(TrustContext) -> bool + RefUnwindSafe>);

impl fmt::Debug for TrustCallback {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Fn(TrustContext) -> bool")
    }
}

impl<F: Fn(TrustContext) -> bool + RefUnwindSafe + 'static> From<F> for TrustCallback {
    fn from(f: F) -> Self {
        Self(Arc::from(f))
    }
}

impl quick_js::Callback<TrustCallback> for TrustCallback {
    fn argument_count(&self) -> usize {
        1
    }

    fn call(
        &self,
        args: Vec<JsValue>,
    ) -> core::result::Result<core::result::Result<JsValue, String>, quick_js::ValueError> {
        let arg = args
            .get(0)
            .ok_or_else(|| quick_js::ValueError::UnexpectedType)?;
        let ctx = TrustContext::try_from(arg)?;
        let result = self.0(ctx);
        Ok(Ok(JsValue::from(result)))
    }
}

/// Options to be passed to KaTeX.
///
/// Read <https://katex.org/docs/options.html> for more information.
#[non_exhaustive]
#[derive(Clone, Builder, Debug, Default)]
#[builder(default)]
#[builder(setter(into, strip_option))]
#[builder(build_fn(validate = "Self::validate"))]
pub struct Opts {
    /// Whether to render the math in the display mode.
    display_mode: Option<bool>,
    /// KaTeX output type.
    output_type: Option<OutputType>,
    /// Whether to have `\tags` rendered on the left instead of the right.
    leqno: Option<bool>,
    /// Whether to make display math flush left.
    fleqn: Option<bool>,
    /// Whether to let KaTeX throw a ParseError for invalid LaTeX.
    throw_on_error: Option<bool>,
    /// Color used for invalid LaTeX.
    error_color: Option<String>,
    /// Collection of custom macros.
    /// Read <https://katex.org/docs/options.html> for more information.
    macros: HashMap<String, String>,
    /// Specifies a minimum thickness, in ems.
    /// Read <https://katex.org/docs/options.html> for more information.
    min_rule_thickness: Option<f64>,
    /// Max size for user-specified sizes.
    /// If set to `None`, users can make elements and spaces arbitrarily large.
    /// Read <https://katex.org/docs/options.html> for more information.
    #[allow(clippy::option_option)]
    max_size: Option<Option<f64>>,
    /// Limit the number of macro expansions to the specified number.
    /// If set to `None`, the macro expander will try to fully expand as in LaTeX.
    /// Read <https://katex.org/docs/options.html> for more information.
    #[allow(clippy::option_option)]
    max_expand: Option<Option<i32>>,
    /// Whether to trust users' input.
    /// Cannot be assigned at the same time with [`OptsBuilder::trust_callback`].
    /// Read <https://katex.org/docs/options.html> for more information.
    trust: Option<bool>,
    /// A callback function to determine whether to trust users' input.
    /// Cannot be assigned at the same time with [`OptsBuilder::trust`].
    /// Read <https://katex.org/docs/options.html> for more information.
    ///
    /// # Examples
    ///
    /// ```
    /// let opts = katex::Opts::builder()
    ///     .trust_callback(|ctx: katex::TrustContext| -> bool {
    ///         ctx.command == r#"\url"#
    ///     })
    ///     .build()
    ///     .unwrap();
    /// ```
    trust_callback: Option<TrustCallback>,
}

impl Opts {
    /// Return [`OptsBuilder`].
    pub fn builder() -> OptsBuilder {
        OptsBuilder::default()
    }
}

impl Into<JsValue> for Opts {
    fn into(self) -> JsValue {
        let mut opt: HashMap<String, JsValue> = HashMap::new();
        if let Some(display_mode) = self.display_mode {
            opt.insert("displayMode".to_owned(), display_mode.into());
        }
        if let Some(output_type) = self.output_type {
            opt.insert(
                "output".to_owned(),
                match output_type {
                    OutputType::Html => "html",
                    OutputType::Mathml => "mathml",
                    OutputType::HtmlAndMathml => "htmlAndMathml",
                }
                .into(),
            );
        }
        if let Some(leqno) = self.leqno {
            opt.insert("leqno".to_owned(), leqno.into());
        }
        if let Some(fleqn) = self.fleqn {
            opt.insert("fleqn".to_owned(), fleqn.into());
        }
        if let Some(throw_on_error) = self.throw_on_error {
            opt.insert("throwOnError".to_owned(), throw_on_error.into());
        }
        if let Some(error_color) = self.error_color {
            opt.insert("errorColor".to_owned(), error_color.into());
        }
        opt.insert("macros".to_owned(), self.macros.into());
        if let Some(min_rule_thickness) = self.min_rule_thickness {
            opt.insert("minRuleThickness".to_owned(), min_rule_thickness.into());
        }
        if let Some(max_size) = self.max_size {
            if let Some(max_size) = max_size {
                opt.insert("maxSize".to_owned(), max_size.into());
            }
        }
        if let Some(max_expand) = self.max_expand {
            match max_expand {
                Some(max_expand) => {
                    opt.insert("maxExpand".to_owned(), max_expand.into());
                }
                None => {
                    opt.insert("maxExpand".to_owned(), i32::max_value().into());
                }
            }
        }
        if let Some(trust) = self.trust {
            opt.insert("trust".to_owned(), trust.into());
        }
        if self.trust_callback.is_some() {
            opt.insert("trust".to_owned(), "USE_TRUST_CALLBACK".into());
        }
        JsValue::Object(opt)
    }
}

impl OptsBuilder {
    /// Add an entry to [`macros`](OptsBuilder::macros).
    ///
    /// # Examples
    ///
    /// ```
    /// let opts = katex::Opts::builder()
    ///     .add_macro(r#"\RR"#.to_owned(), r#"\mathbb{R}"#.to_owned())
    ///     .build()
    ///     .unwrap();
    /// let html = katex::render_with_opts(r#"\RR"#, opts).unwrap();
    /// ```
    pub fn add_macro(mut self, entry_name: String, entry_data: String) -> Self {
        match self.macros.as_mut() {
            Some(macros) => {
                macros.insert(entry_name, entry_data);
            }
            None => {
                let mut macros = HashMap::new();
                macros.insert(entry_name, entry_data);
                self.macros = Some(macros);
            }
        }
        self
    }

    /// Check that `Opts` is valid.
    fn validate(&self) -> core::result::Result<(), String> {
        if self.trust.is_some() && self.trust_callback.is_some() {
            return Err("cannot set `trust` and `trust_callback` at the same time".to_owned());
        }

        Ok(())
    }
}

/// Output type from KaTeX.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum OutputType {
    /// Outputs KaTeX in HTML only.
    Html,
    /// Outputs KaTeX in MathML only.
    Mathml,
    /// Outputs HTML for visual rendering and includes MathML for accessibility.
    HtmlAndMathml,
}

/// Render LaTeX equation to HTML with additional [options](`Opts`).
pub fn render_with_opts(input: &str, opts: Opts) -> Result<String> {
    KATEX.with(|ctx| {
        let ctx = match ctx.as_ref() {
            Ok(ctx) => ctx,
            Err(e) => return Err(e.clone()),
        };
        if let Some(trust_callback) = opts.trust_callback.clone() {
            ctx.add_callback("trustCallback", trust_callback)?;
        }
        let args: Vec<JsValue> = vec![input.into(), opts.into()];
        let result = ctx
            .call_function("renderToString", args)?
            .into_string()
            .ok_or_else(|| quick_js::ValueError::UnexpectedType)?;
        Ok(result)
    })
}

/// Render LaTeX equation to HTML.
#[inline]
pub fn render(input: &str) -> Result<String> {
    render_with_opts(input, Default::default())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_render() {
        let html = render("a = b + c").unwrap();
        assert!(!html.contains(r#"span class="katex-display""#));
        assert!(html.contains(r#"span class="katex""#));
        assert!(html.contains(r#"span class="katex-mathml""#));
        assert!(html.contains(r#"span class="katex-html""#));
    }

    #[test]
    fn test_display_mode() {
        let opts = Opts::builder().display_mode(true).build().unwrap();
        let html = render_with_opts("a = b + c", opts).unwrap();
        assert!(html.contains(r#"span class="katex-display""#));
    }

    #[test]
    fn test_output_html_only() {
        let opts = Opts::builder()
            .output_type(OutputType::Html)
            .build()
            .unwrap();
        let html = render_with_opts("a = b + c", opts).unwrap();
        assert!(!html.contains(r#"span class="katex-mathml""#));
        assert!(html.contains(r#"span class="katex-html""#));
    }

    #[test]
    fn test_output_mathml_only() {
        let opts = Opts::builder()
            .output_type(OutputType::Mathml)
            .build()
            .unwrap();
        let html = render_with_opts("a = b + c", opts).unwrap();
        assert!(html.contains(r#"MathML"#));
        assert!(!html.contains(r#"span class="katex-html""#));
    }

    #[test]
    fn test_leqno() {
        let opts = Opts::builder()
            .display_mode(true)
            .leqno(true)
            .build()
            .unwrap();
        let html = render_with_opts("a = b + c", opts).unwrap();
        assert!(html.contains(r#"span class="katex-display leqno""#));
    }

    #[test]
    fn test_fleqn() {
        let opts = Opts::builder()
            .display_mode(true)
            .fleqn(true)
            .build()
            .unwrap();
        let html = render_with_opts("a = b + c", opts).unwrap();
        assert!(html.contains(r#"span class="katex-display fleqn""#));
    }

    #[test]
    fn test_throw_on_error() {
        let err_msg = match render(r#"\"#) {
            Ok(_) => unreachable!(),
            Err(e) => match e {
                Error::JsExecError(msg) => msg,
                _ => unreachable!(),
            },
        };
        assert!(err_msg.contains("ParseError"));
    }

    #[test]
    fn test_error_color() {
        let opts = Opts::builder()
            .throw_on_error(false)
            .error_color("#ff0000")
            .build()
            .unwrap();
        let html = render_with_opts(r#"\"#, opts).unwrap();
        assert!(html.contains(r#"span class="katex-error""#));
        assert!(html.contains("color:#ff0000"));
    }

    #[test]
    fn test_macros() {
        let opts = Opts::builder()
            .add_macro(r#"\RR"#.to_owned(), r#"\mathbb{R}"#.to_owned())
            .build()
            .unwrap();
        let html = render_with_opts(r#"\RR"#, opts).unwrap();
        assert!(html.contains("mathbb"));
    }

    #[test]
    fn test_trust() {
        let opts = Opts::builder().error_color("#ff0000").build().unwrap();
        let html = render_with_opts(r#"\url{https://www.google.com}"#, opts).unwrap();
        assert!(html.contains(r#"color:#ff0000"#));
        assert!(!html.contains(r#"a href="https://www.google.com""#));

        let opts = Opts::builder()
            .error_color("#ff0000")
            .trust(true)
            .build()
            .unwrap();
        let html = render_with_opts(r#"\url{https://www.google.com}"#, opts).unwrap();
        assert!(!html.contains(r#"color:#ff0000"#));
        assert!(html.contains(r#"a href="https://www.google.com""#));
    }

    #[test]
    fn test_set_both_trust_and_trust_callback() {
        let opts = Opts::builder()
            .trust(true)
            .trust_callback(|_ctx: TrustContext| -> bool { true })
            .build();
        assert!(opts.is_err());
        assert_eq!(
            opts.unwrap_err(),
            "cannot set `trust` and `trust_callback` at the same time"
        );
    }

    #[test]
    fn test_trust_callback_using_closure() {
        let opts = Opts::builder()
            .error_color("#ff0000")
            .trust_callback(|ctx: TrustContext| -> bool {
                ctx.command == r#"\url"#
                    && ctx.protocol == "https"
                    && ctx.url == "https://www.google.com"
            })
            .build()
            .unwrap();
        let html = render_with_opts(r#"\url{https://www.google.com}"#, opts).unwrap();
        assert!(!html.contains(r#"color:#ff0000"#));
        assert!(html.contains(r#"a href="https://www.google.com""#));
    }

    #[test]
    fn test_trust_callback_using_fn() {
        fn callback(ctx: TrustContext) -> bool {
            ctx.command == r#"\url"#
                && ctx.protocol == "https"
                && ctx.url == "https://www.google.com"
        }
        let opts = Opts::builder()
            .error_color("#ff0000")
            .trust_callback(callback)
            .build()
            .unwrap();
        let html = render_with_opts(r#"\url{https://www.google.com}"#, opts).unwrap();
        assert!(!html.contains(r#"color:#ff0000"#));
        assert!(html.contains(r#"a href="https://www.google.com""#));
    }
}