Class: Asciidoctor::Katex::RubyKatexAdapter

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

Overview

Adapter for the KaTeX library for Ruby environment.

Instance Method Summary collapse

Methods included from Utils

hash_camelize

Constructor Details

#initialize(default_options = {}) ⇒ RubyKatexAdapter

Returns a new instance of RubyKatexAdapter.

Parameters:

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

    the default options for the KaTeX renderer.



13
14
15
# File 'lib/asciidoctor/katex/ruby_katex_adapter.rb', line 13

def initialize(default_options = {})
  @default_options = hash_camelize(default_options)
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.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/asciidoctor/katex/ruby_katex_adapter.rb', line 24

def render(math, opts = {})
  opts = @default_options.merge(hash_camelize(opts))

  opts[:throw_on_error] = opts[:throwOnError]
  opts[:error_color] = opts[:errorColor] || '#cc0000'

  begin
    ::Katex.render(math, **opts)
  rescue ::ExecJS::ProgramError => err
    raise ParseError.new(err, math) if err.to_s.start_with?('ParseError:')
    raise KatexError.new(err, math)
  end
end