class Jekyll::Tags::KatexMathMode

Defines the custom Liquid tag for compile-time rendering of KaTeX math. This differs from the katex tag in that it allows use of `$` and `$$` fencing to mark math mode blocks similar to standard latex.

{% katexmm %}
This is a mixed environment where you can write text as normal but fence off latex math using `$`. Escape
using `\$`. For example.
$latex math with \$$
$$display mode latex$$
{% endkatexmm %}

Constants

LATEX_TOKEN_PATTERN
LOG_TOPIC

Public Class Methods

new(tag_name, markup, tokens) click to toggle source
Calls superclass method
# File lib/jekyll/tags/katex_math_mode.rb, line 22
def initialize(tag_name, markup, tokens)
  super
  @markup = markup
  @tokens = tokens
  @display_mode_rendering = Jekyll::Katex::Configuration.global_rendering_options.merge(displayMode: true)
  @inline_mode_rendering = Jekyll::Katex::Configuration.global_rendering_options.merge(displayMode: false)
end

Public Instance Methods

render(context) click to toggle source
Calls superclass method
# File lib/jekyll/tags/katex_math_mode.rb, line 30
def render(context)
  enclosed_block = super
  rendered_str = enclosed_block.to_s.gsub(LATEX_TOKEN_PATTERN) do |match|
    display_mode = match.to_s.start_with? '$$'
    rendering_options = display_mode ? @display_mode_rendering : @inline_mode_rendering
    Jekyll.logger.debug LOG_TOPIC, "Rendering matched block - #{match}"
    KATEX.call('katex.renderToString', Regexp.last_match(2), rendering_options)
  end
  # KaTeX should fix escaped `$` within fenced blocks, this addresses instances outside of math mode
  rendered_str.to_s.gsub(/\\[$]/, '$').to_s
end