Class: Jekyll::Spaceship::MathjaxProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/jekyll-spaceship/processors/mathjax-processor.rb

Constant Summary

Constants inherited from Processor

Processor::DEFAULT_PRIORITY, Processor::PRIORITY_MAP

Instance Attribute Summary

Attributes inherited from Processor

#config, #exclusions, #handled, #logger, #page, #priority, #registers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#converter, #dispatch, escape_html, exclude, #exclusion_regexs, #ext, fetch_img_data, #filename, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, make_img_tag, #name, #on_handle_html_block, #on_handled, #output_ext, #post_exclude, #pre_exclude, priority, register

Constructor Details

This class inherits a constructor from Jekyll::Spaceship::Processor

Class Method Details

.configObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/jekyll-spaceship/processors/mathjax-processor.rb', line 7

def self.config
  {
    'src' => [
      'https://polyfill.io/v3/polyfill.min.js?features=es6',
      'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js',
    ],
    'config' => {
      'tex' => { 'inlineMath' => [['$','$'], ['\\(','\\)']] },
      'svg': { 'fontCache': 'global' }
    }
  }
end

Instance Method Details

#has_mathjax_expression?(doc) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll-spaceship/processors/mathjax-processor.rb', line 47

def has_mathjax_expression?(doc)
  doc.css('*').each do |node|
    if node.content.match(/(?<!\\)\$.+(?<!\\)\$/)
      return true
    end
    if node.content.match(/(?<!\\)\\\(.+(?<!\\)\\\)/)
      return true
    end
  end

  doc.css('script').each do |node|
    type = node['type']
    if type and type.match(/math\/tex/)
      return true
    end
  end
  false
end

#on_handle_html(content) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jekyll-spaceship/processors/mathjax-processor.rb', line 24

def on_handle_html(content)
  # use nokogiri to parse html
  doc = Nokogiri::HTML(content)

  head = doc.at('head')
  return content if head.nil?
  return content if not self.has_mathjax_expression? doc

  self.handled = true

  # add mathjax config
  cfg = config['config'].to_json
  head.add_child("<script>MathJax=#{cfg}</script>")

  # add mathjax dependencies
  config['src'] = [config['src']] if config['src'].is_a? String
  config['src'].each do |src|
    head.add_child("<script src=\"#{src}\"></script>")
  end

  doc.to_html
end

#process?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/jekyll-spaceship/processors/mathjax-processor.rb', line 20

def process?
  return true if Type.html?(output_ext)
end