xu-cheng / katex-rs

Rust bindings to KaTeX
https://docs.rs/katex
Apache License 2.0
111 stars 11 forks source link

Pass macros / Opts as references #3

Closed lzanini closed 3 years ago

lzanini commented 3 years ago

Hello,

Since the OptsBuilder takes ownership of the macros hashmap, the hashmap needs to be cloned every time a new equation is rendered.

I think it is a common use case to render multiple equations with the same set of macros / with the same opts. Wouldn't it be more convenient / faster to pass the macros as a &HashMap reference?

Or, maybe even better, to pass an &Opts reference to render_with_ops?

Right now I have to clone macros and create a new Opts object every time I render a new equation. It looks like this

for item in items_to_render {
    let cloned_macros = macros.clone();
    let ops = katex::Opts::builder()
         .display_mode(display)
         .output_type(katex::OutputType::Html)
         .macros(cloned_macros)
         .build()
         .unwrap();
    let result = katex::render_with_opts(&item, ops);
    ...
}

Which could be replaced by

let ops = katex::Opts::builder()
    .display_mode(display)
    .output_type(katex::OutputType::Html)
    .macros(macros)
    .build()
    .unwrap();
for item in items_to_render {
    let result = katex::render_with_opts(&item, &ops);
    ...
}

Let me know if I'm missing something!

xu-cheng commented 3 years ago

Thanks for the suggestion. I have now released a new version of this crate.