diff --git a/Gemfile b/Gemfile index 11b2b7959..64d71a4d1 100644 --- a/Gemfile +++ b/Gemfile @@ -58,6 +58,7 @@ gem "inline_svg", "~> 1.7" # Embed SVG documents in your Rails views and style t gem "jbuilder", "~> 2.10" # Create JSON structures via a Builder-style DSL gem "jquery-rails", "~> 4.3" # A gem to automate using jQuery with Rails gem "kaminari", "~> 1.2" # A Scope & Engine based, clean, powerful, customizable and sophisticated paginator +gem "katex", "~> 0.6.0" # This rubygem enables you to render TeX math to HTML using KaTeX. It uses ExecJS under the hood gem "libhoney", "~> 1.14" # Ruby gem for sending data to Honeycomb gem "liquid", "~> 4.0" # A secure, non-evaling end user template engine with aesthetic markup gem "nokogiri", "~> 1.10" # HTML, XML, SAX, and Reader parser diff --git a/Gemfile.lock b/Gemfile.lock index abf3cc945..e1cafd6da 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -453,6 +453,8 @@ GEM activerecord kaminari-core (= 1.2.0) kaminari-core (1.2.0) + katex (0.6.0) + execjs (~> 2.7) launchy (2.5.0) addressable (~> 2.7) libhoney (1.14.4) @@ -925,6 +927,7 @@ DEPENDENCIES jbuilder (~> 2.10) jquery-rails (~> 4.3) kaminari (~> 1.2) + katex (~> 0.6.0) launchy (~> 2.5) libhoney (~> 1.14) liquid (~> 4.0) diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index bca3cc0a1..9f69e5ad0 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -6,3 +6,4 @@ //= link s3_direct_upload.js // = link administrate/application.css // = link administrate/application.js +// = link katex.css diff --git a/app/assets/stylesheets/ltags/KatexTag.scss b/app/assets/stylesheets/ltags/KatexTag.scss new file mode 100644 index 000000000..725fe286e --- /dev/null +++ b/app/assets/stylesheets/ltags/KatexTag.scss @@ -0,0 +1 @@ +@import 'katex'; diff --git a/app/liquid_tags/katex_tag.rb b/app/liquid_tags/katex_tag.rb new file mode 100644 index 000000000..361c1463e --- /dev/null +++ b/app/liquid_tags/katex_tag.rb @@ -0,0 +1,31 @@ +class KatexTag < Liquid::Block + PARTIAL = "liquids/katex".freeze + KATEX_EXISTED = "katex_existed".freeze + + def initialize(tag_name, markup, tokens) + super + end + + def render(context) + block = Nokogiri::HTML(super).at("body").text + + parsed_content = begin + Katex.render(block, display_mode: true) + rescue ExecJS::ProgramError => e + e.message + end + + should_render_css = !context[KATEX_EXISTED] + + unless context[KATEX_EXISTED] + context[KATEX_EXISTED] = true + end + + ActionController::Base.new.render_to_string( + partial: PARTIAL, + locals: { parsed_content: parsed_content, should_render_css: should_render_css }, + ) + end +end + +Liquid::Template.register_tag("katex", KatexTag) diff --git a/app/views/liquids/_katex.html.erb b/app/views/liquids/_katex.html.erb new file mode 100644 index 000000000..da30faad8 --- /dev/null +++ b/app/views/liquids/_katex.html.erb @@ -0,0 +1,7 @@ +<% if should_render_css %> + <%= stylesheet_link_tag "katex" %> +<% end %> + +
Put executable code within a runkit liquid block, as follows:
{% runkit
// hidden setup JavaScript code goes in this preamble area
const hiddenVar = 42
%}
// visible, reader-editable JavaScript code goes here
console.log(hiddenVar)
{% endrunkit %}
+
+ Place your mathematical expression within a KaTeX liquid block, as follows:
+{% katex %}
c = \pm\sqrt{a^2 + b^2}
{% endkatex %}
+
All you need is the ID of the Stackblitz:
{% stackblitz ball-demo %}
diff --git a/config/initializers/redcarpet.rb b/config/initializers/redcarpet.rb
index 51660959e..09702983a 100644
--- a/config/initializers/redcarpet.rb
+++ b/config/initializers/redcarpet.rb
@@ -5,7 +5,7 @@
lax_html_blocks: true,
lax_spacing: true,
strikethrough: true,
- superscript: true,
+ superscript: false,
tables: true,
footnotes: true
}.freeze
diff --git a/spec/fixtures/approvals/katextag/render/generates_katex_output.approved.html b/spec/fixtures/approvals/katextag/render/generates_katex_output.approved.html
new file mode 100644
index 000000000..d31b7e2de
--- /dev/null
+++ b/spec/fixtures/approvals/katextag/render/generates_katex_output.approved.html
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+