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


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.


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