我正在创建一个新的Jekyll博客设置,在那里我想要自我主机和生成服务器端的一切,我可以(基本上,除了Disqus和谷歌分析)。我希望我的站点即使在关闭JS的浏览器上也能很好地工作,这就是为什么我在站点生成期间转向Kramdown+Rouge来突出显示语法,使用KaTex来表示TeX公式。
不过我发现了个问题。当我有这样的例子时:
```bash$回声测试
测试
它将在KaTeX命令中中断:
{% katexmm %}
{{ content }}
{% endkatexmm %}我找出了原因:
<span class="gp">$</span>的东西katexmm表示每个$都与另一个$配对,或者作为\$转义。我想知道的是如何解决这个问题,例如,如何在pre中转义所有的pre,但保留其他$ (打算用作实际的LaTeX片段)。或者可能以某种方式将jekyll配置为忽略未配对的$?(在文本已经使用美元符号匹配之后,throw_error: false选项可以工作,因此没有帮助)。
我希望通过配置或应用katexmm来修复这个问题,这样我就不必修改任何post的内容了。
发布于 2018-12-26 01:45:42
我通过将标签从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就完全过时了(以及我的解决方案,如果有人好奇的话,我会留在这里)。
https://stackoverflow.com/questions/53924888
复制相似问题