katex_on_rails 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +86 -4
- data/katex_on_rails.gemspec +1 -0
- data/lib/katex_on_rails/package.rb +2 -2
- data/lib/katex_on_rails/version.rb +1 -1
- data/lib/katex_on_rails.rb +171 -5
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65a6875d2787c1479ffd681369effa5f4a401020d7196a37de1fa351b46e8356
|
4
|
+
data.tar.gz: da26e87077a698b725e93077ce79eb4307b808d9922907ad5528c5f59736032c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c181e0591c9732c1ec3414407b89ae08f6373476c0ccf6fb7bb3e974f179d5a64bf68078a48e5a36329cee7125c81e3f5703588aaa4fd595da5149e34412dc5
|
7
|
+
data.tar.gz: cc7bacb594ece4a57d5d4faeb8214ca450a372e4f5928a11eab93adc4103ae20a4fd197286c239e6fb6bf2178af907e44a96fd7269454113b43f46df8e8984f6
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -35,17 +35,99 @@ from the `katex` Node.js package, make the KaTeX font files available to the cli
|
|
35
35
|
and use the HTML5 doctype. See [browser usage](https://katex.org/docs/browser).
|
36
36
|
Note, however, that you do not need to include `katex.js` on the client.
|
37
37
|
|
38
|
-
##
|
38
|
+
## Configuration
|
39
|
+
|
40
|
+
The KaTeX on Rails initializer allows you to customize how KaTeX processes mathematical expressions.
|
41
|
+
You can specify which delimiters to recognize and which HTML tags to ignore during processing.
|
39
42
|
|
40
43
|
```ruby
|
44
|
+
# Using the default configuration:
|
41
45
|
katex = KatexOnRails.new
|
42
46
|
|
47
|
+
# Specifying a custom configuration:
|
48
|
+
katex = KatexOnRails.new(
|
49
|
+
delimiters: [
|
50
|
+
{ left: '$$', right: '$$', display: true },
|
51
|
+
{ left: '$', right: '$', display: false }
|
52
|
+
],
|
53
|
+
ignored_tags: %w[span]
|
54
|
+
)
|
55
|
+
```
|
56
|
+
|
57
|
+
### `delimiters` (`Array<Hash>`)
|
58
|
+
|
59
|
+
A list of delimiters to look for math expressions, processed in the specified order.
|
60
|
+
Each delimiter must be a Hash with the following keys:
|
61
|
+
|
62
|
+
* `left` (`String`) – The opening delimiter that starts the math expression.
|
63
|
+
* `right` (`String`) – The closing delimiter that ends the math expression.
|
64
|
+
* `display` (`Boolean`) – Whether to render in display mode (block) or inline mode.
|
65
|
+
|
66
|
+
Default delimiters:
|
67
|
+
```ruby
|
68
|
+
[
|
69
|
+
{ left: '$$', right: '$$', display: true },
|
70
|
+
{ left: '\(', right: '\)', display: false },
|
71
|
+
{ left: '\begin{equation}', right: '\end{equation}', display: true },
|
72
|
+
{ left: '\begin{align}', right: '\end{align}', display: true },
|
73
|
+
{ left: '\begin{alignat}', right: '\end{alignat}', display: true },
|
74
|
+
{ left: '\begin{gather}', right: '\end{gather}', display: true },
|
75
|
+
{ left: '\begin{CD}', right: '\end{CD}', display: true },
|
76
|
+
{ left: '\[', right: '\]', display: true }
|
77
|
+
]
|
78
|
+
```
|
79
|
+
|
80
|
+
If you want to support inline math via `$`, be sure to place it after `$$` in the array.
|
81
|
+
Otherwise, `$$` expressions will be incorrectly parsed as empty `$` expressions.
|
82
|
+
|
83
|
+
### `ignored_tags` (`Array<String>`)
|
84
|
+
|
85
|
+
A list of HTML tag names to skip when searching for math expressions.
|
86
|
+
Use this to prevent processing math-like content in code blocks, scripts, etc.
|
87
|
+
|
88
|
+
Default ignored tags:
|
89
|
+
```ruby
|
90
|
+
%w[script noscript style textarea pre code option]
|
91
|
+
```
|
92
|
+
|
93
|
+
## Usage
|
94
|
+
|
95
|
+
### `render_to_string`
|
96
|
+
|
97
|
+
This method takes a LaTeX math expression and converts it directly to an HTML fragment using KaTeX.
|
98
|
+
|
99
|
+
Example:
|
100
|
+
|
101
|
+
```ruby
|
43
102
|
katex.render_to_string('x', { output: 'html' })
|
44
|
-
|
103
|
+
# => "<span class=\"katex\">..."
|
45
104
|
```
|
46
105
|
|
47
|
-
|
106
|
+
Common rendering options are:
|
107
|
+
- `display_mode` (`Boolean`) – Render in display (block) mode or inline mode.
|
108
|
+
- `throw_on_error` (`Boolean`) – Throw an error on invalid LaTeX (default: true).
|
109
|
+
- `output` (`String`) – Output format (`html`, `mathml`, or `htmlAndMathml`).
|
110
|
+
|
111
|
+
All the supported options are listed in the [KaTeX documentation](https://katex.org/docs/options).
|
112
|
+
|
113
|
+
### `render_in_html`
|
114
|
+
|
115
|
+
This method processes an HTML string, automatically detecting and rendering all math expressions found in the HTML
|
116
|
+
based on the configured delimiters. It returns a Nokogiri document fragment that can be converted back to a string.
|
117
|
+
|
118
|
+
Common rendering options are:
|
119
|
+
- `throw_on_error` (`Boolean`) – Throw an error on invalid LaTeX (default: true).
|
120
|
+
- `output` (`String`) – Output format (`html`, `mathml`, or `htmlAndMathml`).
|
121
|
+
|
122
|
+
All the supported options are listed in the [KaTeX documentation](https://katex.org/docs/options).
|
123
|
+
|
124
|
+
Example:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
katex.render_in_html('<div>Formula: \(x^2\)</div>', { output: 'html' }).to_html
|
128
|
+
# => "<div>Formula: <span class=\"katex\">...</span></div>"
|
129
|
+
```
|
48
130
|
|
49
131
|
## License
|
50
132
|
|
51
|
-
The KaTeX on Rails gem is
|
133
|
+
The KaTeX on Rails gem is released under the [MIT License](https://opensource.org/licenses/MIT).
|
data/katex_on_rails.gemspec
CHANGED
data/lib/katex_on_rails.rb
CHANGED
@@ -1,16 +1,182 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'nokogiri'
|
4
|
+
|
4
5
|
require 'katex_on_rails/package'
|
6
|
+
require 'katex_on_rails/version'
|
5
7
|
|
6
8
|
class KatexOnRails
|
7
|
-
|
8
|
-
|
9
|
+
DELIMITERS = [
|
10
|
+
{ left: '$$', right: '$$', display: true },
|
11
|
+
{ left: '\(', right: '\)', display: false },
|
12
|
+
{ left: '\begin{equation}', right: '\end{equation}', display: true },
|
13
|
+
{ left: '\begin{align}', right: '\end{align}', display: true },
|
14
|
+
{ left: '\begin{alignat}', right: '\end{alignat}', display: true },
|
15
|
+
{ left: '\begin{gather}', right: '\end{gather}', display: true },
|
16
|
+
{ left: '\begin{CD}', right: '\end{CD}', display: true },
|
17
|
+
{ left: '\[', right: '\]', display: true }
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
IGNORED_TAGS = %w[script noscript style textarea pre code option].freeze
|
21
|
+
|
22
|
+
# Initialize the KaTeX on Rails with the provided configuration.
|
23
|
+
#
|
24
|
+
# This initializer allows you to customize how KaTeX processes mathematical expressions
|
25
|
+
# in your HTML content. You can specify which delimiters to recognize and which HTML
|
26
|
+
# tags to ignore during processing.
|
27
|
+
#
|
28
|
+
# @param [Hash] configuration Custom KaTeX on Rails configuration options.
|
29
|
+
# @option configuration [Array<Hash>] delimiters A list of delimiters to look for math.
|
30
|
+
# @option configuration [Array<String>] ignored_tags A list of HTML tags to skip when searching for math.
|
31
|
+
#
|
32
|
+
# @return [KatexOnRails] The KaTeX on Rails instance.
|
33
|
+
#
|
34
|
+
# == Configuration Options
|
35
|
+
#
|
36
|
+
# === +:delimiters+ (Array<Hash>)
|
37
|
+
#
|
38
|
+
# A list of delimiters to look for math expressions, processed in the specified order.
|
39
|
+
# Each delimiter must be a Hash with the following keys:
|
40
|
+
#
|
41
|
+
# * +:left+ (String):: The opening delimiter that starts the math expression.
|
42
|
+
# * +:right+ (String):: The closing delimiter that ends the math expression.
|
43
|
+
# * +:display+ (Boolean):: Whether to render in display mode (block) or inline mode.
|
44
|
+
#
|
45
|
+
# Default delimiters:
|
46
|
+
#
|
47
|
+
# [
|
48
|
+
# { left: '$$', right: '$$', display: true },
|
49
|
+
# { left: '\(', right: '\)', display: false },
|
50
|
+
# { left: '\begin{equation}', right: '\end{equation}', display: true },
|
51
|
+
# { left: '\begin{align}', right: '\end{align}', display: true },
|
52
|
+
# { left: '\begin{alignat}', right: '\end{alignat}', display: true },
|
53
|
+
# { left: '\begin{gather}', right: '\end{gather}', display: true },
|
54
|
+
# { left: '\begin{CD}', right: '\end{CD}', display: true },
|
55
|
+
# { left: '\[', right: '\]', display: true }
|
56
|
+
# ]
|
57
|
+
#
|
58
|
+
# If you want to support inline math via +$+, be sure to place it after +$$+ in the array.
|
59
|
+
# Otherwise, +$$+ expressions will be incorrectly parsed as empty +$+ expressions.
|
60
|
+
#
|
61
|
+
# === +:ignored_tags+ (Array<String>)
|
62
|
+
#
|
63
|
+
# A list of HTML tag names to skip when searching for math expressions.
|
64
|
+
# Use this to prevent processing math-like content in code blocks, scripts, etc.
|
65
|
+
#
|
66
|
+
# Default ignored tags:
|
67
|
+
#
|
68
|
+
# %w[script noscript style textarea pre code option]
|
69
|
+
#
|
70
|
+
# == Examples
|
71
|
+
#
|
72
|
+
# Usage with the default configuration:
|
73
|
+
#
|
74
|
+
# katex = KatexOnRails.new
|
75
|
+
#
|
76
|
+
# Usage with a custom configuration:
|
77
|
+
#
|
78
|
+
# katex = KatexOnRails.new(
|
79
|
+
# delimiters: [
|
80
|
+
# { left: '$$', right: '$$', display: true },
|
81
|
+
# { left: '$', right: '$', display: false }
|
82
|
+
# ],
|
83
|
+
# ignored_tags: %w[span]
|
84
|
+
# )
|
85
|
+
#
|
86
|
+
def initialize(**configuration)
|
87
|
+
@delimiters = configuration.fetch(:delimiters, DELIMITERS)
|
88
|
+
@ignored_tags = configuration.fetch(:ignored_tags, IGNORED_TAGS)
|
89
|
+
@katex_package ||= Package.new
|
90
|
+
end
|
91
|
+
|
92
|
+
# Generate an HTML string of the rendered math expression.
|
93
|
+
#
|
94
|
+
# This method takes a LaTeX math expression and converts it directly to an HTML fragment using KaTeX.
|
95
|
+
#
|
96
|
+
# @param [String] expression The LaTeX math expression to render.
|
97
|
+
# @param [Hash] options The KaTeX rendering options (see KaTeX documentation).
|
98
|
+
#
|
99
|
+
# @return [String] HTML string containing the rendered math.
|
100
|
+
#
|
101
|
+
# == Rendering Options
|
102
|
+
#
|
103
|
+
# Common options are:
|
104
|
+
#
|
105
|
+
# - +:display_mode+ (Boolean):: Render in display (block) mode or inline mode.
|
106
|
+
# - +:throw_on_error+ (Boolean):: Throw an error on invalid LaTeX (default: true).
|
107
|
+
# - +:output+ (String):: Output format ('html', 'mathml', or 'htmlAndMathml').
|
108
|
+
#
|
109
|
+
# All the supported options are listed in the {KaTeX documentation}[https://katex.org/docs/options].
|
110
|
+
#
|
111
|
+
# == Example
|
112
|
+
#
|
113
|
+
# KatexOnRails.new.render_to_string('x', { output: 'html' })
|
114
|
+
# # => "<span class=\"katex\">..."
|
115
|
+
#
|
116
|
+
def render_to_string(expression, options = {})
|
117
|
+
@katex_package.render_to_string(expression, options)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Render all math expressions found within an HTML fragment.
|
121
|
+
#
|
122
|
+
# This method processes an HTML string, automatically detecting
|
123
|
+
# and rendering any math expressions based on the configured delimiters.
|
124
|
+
# It returns a Nokogiri document fragment that can be converted back to a string.
|
125
|
+
#
|
126
|
+
# @param [String] html_fragment The HTML content to process.
|
127
|
+
# @param [Hash] options KaTeX rendering options.
|
128
|
+
#
|
129
|
+
# @return [Nokogiri::HTML5::DocumentFragment] Processed HTML with rendered math.
|
130
|
+
#
|
131
|
+
# == Rendering Options
|
132
|
+
#
|
133
|
+
# Common options are:
|
134
|
+
#
|
135
|
+
# - +:throw_on_error+ (Boolean):: Throw an error on invalid LaTeX (default: true).
|
136
|
+
# - +:output+ (String):: Output format ('html', 'mathml', or 'htmlAndMathml').
|
137
|
+
#
|
138
|
+
# All the supported options are listed in the {KaTeX documentation}[https://katex.org/docs/options].
|
139
|
+
#
|
140
|
+
# == Examples
|
141
|
+
#
|
142
|
+
# KatexOnRails.new.render_in_html('<div>Formula: \(x^2\)</div>', { output: 'html' }).to_html
|
143
|
+
# # => "<div>Formula: <span class=\"katex\">...</span></div>"
|
144
|
+
#
|
145
|
+
def render_in_html(html_fragment, options = {})
|
146
|
+
html_fragment = html_fragment.to_html if html_fragment.respond_to?(:to_html)
|
147
|
+
doc = Nokogiri::HTML5.fragment(html_fragment.to_s)
|
148
|
+
|
149
|
+
@delimiters.each do |delimiter|
|
150
|
+
delimiter_left = Regexp.escape(delimiter[:left].to_s)
|
151
|
+
delimiter_right = Regexp.escape(delimiter[:right].to_s)
|
152
|
+
math_regexp = /#{delimiter_left}(.*?)#{delimiter_right}/
|
153
|
+
|
154
|
+
each_text_node(doc) do |current_node|
|
155
|
+
content = current_node.content
|
156
|
+
next unless math_regexp.match?(content)
|
157
|
+
|
158
|
+
new_content = content.gsub(math_regexp) do |_match|
|
159
|
+
render_to_string(
|
160
|
+
::Regexp.last_match(1),
|
161
|
+
options.merge({ displayMode: delimiter[:display] })
|
162
|
+
)
|
163
|
+
end
|
164
|
+
|
165
|
+
new_fragment = Nokogiri::HTML5.fragment(new_content)
|
166
|
+
current_node.replace(new_fragment)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
doc
|
9
171
|
end
|
10
172
|
|
11
173
|
private
|
12
174
|
|
13
|
-
def
|
14
|
-
|
175
|
+
def each_text_node(node, &)
|
176
|
+
return if @ignored_tags.include?(node.name)
|
177
|
+
|
178
|
+
node.children.each { |child| each_text_node(child, &) }
|
179
|
+
|
180
|
+
yield node if node.text?
|
15
181
|
end
|
16
182
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: katex_on_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juraj Kostolansky
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-06-
|
11
|
+
date: 2025-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nodo
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 1.8.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.18.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.18.0
|
27
41
|
description:
|
28
42
|
email:
|
29
43
|
- juraj@kostolansky.sk
|