forked from jaanli/jaan.io
/
katex_block.rb
65 lines (47 loc) · 1.78 KB
/
katex_block.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'execjs'
module Jekyll
module Tags
class KatexBlock < Liquid::Block
def initialize(tag, markup, tokens)
super
@tag = tag
@tokens = tokens
@markup = markup
@centred = markup.include? 'centred'
end
# Config defaults
PATH_TO_JS = "./public/js/katex.min.js"
def render(context)
# Take config from global `_config.yml', if it exists
katex_config = context.registers[:site].config['katex'] || {}
@config = {}
# Take path to KaTeX javascript from site config or default
@config['path_to_js'] = katex_config['path_to_js'] || PATH_TO_JS
import_js(@config['path_to_js'])
latex_source = super
if @centred
return katexify_centred(latex_source)
end
katexify_inline(latex_source)
end
def import_js(path)
katexsrc = open(path).read
@katex = ExecJS.compile(katexsrc)
end
def katexify_inline(content)
eqn_to_html(content)
end
def katexify_centred(content)
style = "text-align: center; margin-top: 0.5em; margin-bottom: 0.5em;"
div_open = "<div style='#{style}'>"
div_close = "</div>"
s = "\\displaystyle " + content
return div_open + eqn_to_html(s) + div_close
end
def eqn_to_html(string)
return @katex.call("katex.renderToString", string)
end
end
end
end
Liquid::Template.register_tag('latex', Jekyll::Tags::KatexBlock)