class Asciidoctor::Katex::OpalKatexAdapter

Adapter for the KaTeX library for Opal/JS environment.

Public Class Methods

new(default_options = {}, katex_object = nil) click to toggle source

@param default_options [Hash] the default options for the KaTeX renderer. @param katex_object the katex object to use under Opal (defaults to

global variable `katex`).
# File lib/asciidoctor/katex/opal_katex_adapter.rb, line 14
def initialize(default_options = {}, katex_object = nil)
  @default_options = hash_camelize(default_options)
  @katex_object = katex_object || `katex`
end

Public Instance Methods

call(math, opts = {})
Alias for: render
render(math, opts = {}) click to toggle source

Renders the given math expression to HTML using KaTeX.

@param math [String] the math (LaTeX) expression. @param opts [Hash] options for `katex.renderToString`.

Keys in under_score notation will be converted to camelCase.
See <https://github.com/Khan/KaTeX#rendering-options>.

@return [String] a rendered HTML fragment.

# File lib/asciidoctor/katex/opal_katex_adapter.rb, line 26
def render(math, opts = {})
  opts = @default_options.merge(hash_camelize(opts))

  begin
    `#{@katex_object}.renderToString(#{math}, #{opts}.$$smap)`
  rescue ::JS::Error => err
    # "#{err}" is really needed for Opal/JS, #to_s returns a different string.
    # rubocop:disable UnneededInterpolation
    raise ParseError.new(err, math) if "#{err}".start_with?('ParseError:')
    raise KatexError.new(err, math)
  end
end
Also aliased as: call