Class: Asciidoctor::Katex::OpalKatexAdapter

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/asciidoctor/katex/opal_katex_adapter.rb

Overview

Adapter for the KaTeX library for Opal/JS environment.

Instance Method Summary collapse

Methods included from Utils

hash_camelize

Constructor Details

#initialize(default_options = {}, katex_object = nil) ⇒ OpalKatexAdapter

Returns a new instance of OpalKatexAdapter.

Parameters:

  • default_options (Hash) (defaults to: {})

    the default options for the KaTeX renderer.

  • katex_object (defaults to: nil)

    the katex object to use under Opal (defaults to global variable ‘katex`).



14
15
16
17
# 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

Instance Method Details

#render(math, opts = {}) ⇒ String Also known as: call

Renders the given math expression to HTML using KaTeX.

Parameters:

  • math (String)

    the math (LaTeX) expression.

  • opts (Hash) (defaults to: {})

    options for ‘katex.renderToString`. Keys in under_score notation will be converted to camelCase. See <github.com/Khan/KaTeX#rendering-options>.

Returns:

  • (String)

    a rendered HTML fragment.



26
27
28
29
30
31
32
33
34
35
36
37
# 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