首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有Rouge和KaTeX的Jekyll不能有$ in代码

带有Rouge和KaTeX的Jekyll不能有$ in代码
EN

Stack Overflow用户
提问于 2018-12-25 18:57:35
回答 1查看 271关注 0票数 2

我正在创建一个新的Jekyll博客设置,在那里我想要自我主机和生成服务器端的一切,我可以(基本上,除了Disqus和谷歌分析)。我希望我的站点即使在关闭JS的浏览器上也能很好地工作,这就是为什么我在站点生成期间转向Kramdown+Rouge来突出显示语法,使用KaTex来表示TeX公式。

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

代码语言:javascript
复制
```bash

$回声测试

测试

代码语言:javascript
复制

它将在KaTeX命令中中断:

代码语言:javascript
复制
{% katexmm %}
{{ content }}
{% endkatexmm %}

我找出了原因:

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

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

我希望通过配置或应用katexmm来修复这个问题,这样我就不必修改任何post的内容了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-26 01:45:42

我通过将标签jekyll-katex修改为code (内联,使用单倾斜)和pre code (使用缩进或3个倾斜块),从而消除了错误:

代码语言:javascript
复制
# 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就完全过时了(以及我的解决方案,如果有人好奇的话,我会留在这里)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53924888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档