module Katex
Provides a Ruby wrapper for KaTeX server-side rendering.
Constants
- KATEX_VERSION
- VERSION
Attributes
execjs_runtime[RW]
The ExecJS runtime factory, default: `-> { ExecJS.runtime }`. Set this before calling any other methods to use a different runtime.
This proc is guaranteed to be called at most once.
Public Class Methods
gem_path()
click to toggle source
# File lib/katex.rb, line 65 def gem_path @gem_path ||= File.expand_path(File.join(File.dirname(__FILE__), '..')) end
katex_context()
click to toggle source
# File lib/katex.rb, line 54 def katex_context @load_context_mutex.synchronize do @context ||= @execjs_runtime.call.compile File.read katex_js_path end end
katex_js_path()
click to toggle source
# File lib/katex.rb, line 60 def katex_js_path File.expand_path File.join('vendor', 'katex', 'javascripts', 'katex.js'), gem_path end
render(math, display_mode: false, throw_on_error: true, error_color: '
click to toggle source
Renders the given math expression to HTML via katex.renderToString.
@param math [String] The math (Latex) expression @param display_mode [Boolean] Whether to render in display mode. @param throw_on_error [Boolean] Whether to raise on error. If false,
renders the error message instead (even in case of ParseError, unlike the native katex).
@param error_color [String] Error text CSS color. @param render_options [Hash] Additional options for katex.renderToString.
See https://github.com/Khan/KaTeX#rendering-options.
@return [String] HTML. If strings respond to html_safe, the result will be
HTML-safe.
@note This method is thread-safe as long as your ExecJS runtime is
thread-safe. MiniRacer is the recommended runtime.
# File lib/katex.rb, line 30 def render(math, display_mode: false, throw_on_error: true, error_color: '#cc0000', macros: {}, **render_options) maybe_html_safe katex_context.call( 'katex.renderToString', math, displayMode: display_mode, throwOnError: throw_on_error, errorColor: error_color, macros: macros, **render_options ) rescue ExecJS::ProgramError => e raise e if throw_on_error render_exception e, error_color end
Private Class Methods
maybe_html_safe(html)
click to toggle source
# File lib/katex.rb, line 84 def maybe_html_safe(html) if html.respond_to?(:html_safe) html.html_safe else html end end
render_exception(exception, error_color)
click to toggle source
# File lib/katex.rb, line 72 def render_exception(exception, error_color) maybe_html_safe <<~HTML <span class='katex'> <span class='katex-html'> <span style='color: #{error_color}'> #{ERB::Util.h exception.message.sub(/^ParseError: /, '')} </span> </span> </span> HTML end