Jekyll with Rouge和KaTeX的代码中不能有$



我正在创建一个新的Jekyll博客设置,我想在那里自我托管并生成服务器端的所有内容(所以基本上除了Disqus和Google Analytics之外的所有内容)。我希望我的网站即使在关闭JS的浏览器上也能很好地工作,这就是为什么我在网站生成过程中使用Kramdown+Rouge进行语法高亮显示,并使用KaTex来减少TeX公式。

不过我发现了一个问题。当我有这样的例子时:

```bash
$ echo test
test
```

它将在KaTeX命令内部中断:

{% katexmm %}
{{ content }}
{% endkatexmm %}

我找到了原因:

  • 胭脂将代码片段变成类似<span class="gp">$</span>的东西
  • katexmm表示每个$与另一个$配对或转义为$

我想知道的是如何解决这个问题,例如,通过转义pre中的所有$,但保留其他$(打算用作实际的LaTeX片段)不变。或者以某种方式配置jekyll katex以忽略未配对的$?(throw_error: false选项在使用美元符号匹配文本后有效,因此没有帮助)。

我想在配置中解决这个问题,或者代替应用katexmm,这样我就不必修改任何帖子的内容。

我通过修改jekyll-katex中的标签来处理code(内联,使用单个波浪号)和pre code(使用缩进或3个波浪号生成的块),成功地消除了错误:

# frozen_string_literal: true
require 'jekyll'
require 'jekyll-katex/configuration'
require 'jekyll-katex/katex_js'
require 'nokogiri'
module Jekyll
module Tags
# 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 %}
class KatexMathModeFixed < Liquid::Block
LOG_TOPIC = 'KatexMathModeFixed:'
KATEX ||= Jekyll::Katex::KATEX_JS
LATEX_TOKEN_PATTERN = /(?<!\)([$]{2}|[$]{1})(.+?)(?<!\)1/m
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
def render(context)
enclosed_block = super
fixed_block = fix_code(enclosed_block)
rendered_str = fixed_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
def fix_code(input)
updated = false
html = Nokogiri::HTML.fragment(input)
Jekyll.logger.debug LOG_TOPIC, "Fixing - #{input}"
html.css("code, code span").each do |c|
if c.css('*').empty? && c.content['$']
updated = true
Jekyll.logger.debug LOG_TOPIC, "current tag - #{c}"
content = c.content
content['$'] = '$'
c.content = content
Jekyll.logger.debug LOG_TOPIC, "current tag now - #{c}/#{content}"
end
end
output = html.to_s
Jekyll.logger.debug LOG_TOPIC, "Fixed - #{output}"
if updated then html.to_s else input end
end
end
end
end
Liquid::Template.register_tag('katexmmx', Jekyll::Tags::KatexMathModeFixed)

它可以安装在_plugins目录中。

问题是,这仍然有缺陷——默认情况下,kramdown仍然尝试使用mathjax引擎并生成<script type="math/tex">,因此必须更改。当我研究如何使用时,我发现kramdown也支持math_engine: katex——这样我只需要添加字体和CSS,jekyll-katex就完全过时了(还有我的变通方法,如果有人好奇,我会把它留在这里)。

最新更新