【问题标题】:How can I access un-rendered (markdown) content in Jekyll with liquid tags?如何使用液体标签访问 Jekyll 中未渲染(降价)的内容?
【发布时间】:2020-06-26 16:36:34
【问题描述】:

通过阅读documentation Jekyll's template data,人们可能会认为访问未渲染内容的方式是page.content;但据我所知,这是提供降价解析器已经呈现的帖子内容。

我需要一个直接访问原始(原始降价)内容的解决方案,而不是简单地尝试将 html 转换回降价。

用例背景

我的用例如下:我使用pandoc plugin 为我的 Jekyll 网站呈现 markdown,使用“mathjax”选项来获得漂亮的方程式。但是,mathjax 需要 javascript,因此这些不会显示在 RSS 提要中,我通过循环 page.content 生成,如下所示:

 {% for post in site.posts %}
 <entry>
   <title>{{ post.title }}</title>
   <link href="{{ site.production_url }}{{ post.url }}"/>
   <updated>{{ post.date | date_to_xmlschema }}</updated>
   <id>{{ site.production_url }}{{ post.id }}</id>
   <content type="html">{{ post.content | xml_escape }}</content>
 </entry>
 {% endfor %}

正如xml_escape 过滤器所暗示的那样,post.content 出现在 html 中。如果我能获得原始内容(想象post.contentraw 或类似的存在),那么我可以轻松添加一个过滤器,该过滤器将使用带有“webtex”选项的 pandoc 在解析 RSS 提要时为方程式生成图像,例如:

require 'pandoc-ruby'
module TextFilter
  def webtex(input)
    PandocRuby.new(input, "webtex").to_html
  end
end
Liquid::Template.register_filter(TextFilter)

但是当我得到已经在 html+mathjax 中呈现的方程式而不是原始降价时,我被卡住了。转换回降价并没有帮助,因为它不会转换 mathjax(只是乱码)。

有什么建议吗?当然有一种方法可以调用原始降价?

【问题讨论】:

标签: markdown jekyll liquid mathjax pandoc


【解决方案1】:

这是我认为你会遇到的麻烦:https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rbhttps://github.com/mojombo/jekyll/blob/master/lib/jekyll/site.rb

根据我的阅读,对于给定的帖子/页面,self.content 将替换为通过 Markdown 和 Liquid 运行 self.content 的结果,在 convertible.rb 的第 79 行:

self.content = Liquid::Template.parse(self.content).render(payload, info)

帖子在页面之前呈现,见 site.rb 中的第 37-44 和 197-211 行:

def process
  self.reset
  self.read
  self.generate
  self.render
  self.cleanup
  self.write
end

... ...

def render
  payload = site_payload
  self.posts.each do |post|
    post.render(self.layouts, payload)
  end

  self.pages.each do |page|
    page.render(self.layouts, payload)
  end

  self.categories.values.map { |ps| ps.sort! { |a, b| b <=> a } }
  self.tags.values.map { |ps| ps.sort! { |a, b| b <=> a } }
rescue Errno::ENOENT => e
  # ignore missing layout dir
end

当您开始渲染此页面时,self.content 已被渲染为 HTML - 因此无需停止渲染。已经完成了。

但是,生成器 (https://github.com/mojombo/jekyll/wiki/Plugins) 在渲染阶段之前运行,因此,据我从阅读源代码中得知,您应该能够相当简单地编写一个生成器,它将 self.content 复制到某个属性中(例如 self.raw_content),您以后可以在模板 {{ page.raw_content }} 中将其作为原始 Markdown 访问。

【讨论】:

  • 很好地解释了这里发生的事情,以及很好的建议。我的 ruby​​ 很差(部分变量范围)——这是否像使用以下行编写生成器一样简单:self.raw_content = self.content 然后page.raw_content 会自动变为可用?
  • 关闭:看看这个要点是否可行:gist.github.com/4025507。然后,您可以使用 {{ page.raw_content }} 访问布局中的原始内容。
  • 请注意,您自己的插件(在本例中为生成器)不会在 Github 页面上运行。所以如果你打算在那里使用它,你需要找到其他的解决方案。
  • @heliotrope 我尝试了 gist 中的插件,但出现错误,看起来数据结构自 2012 年以来发生了变化……
  • 我确定它有!老实说,在过去的六年里,我根本没有关注 jekyll 中的任何事情,所以我真的帮不上忙。
【解决方案2】:

我最终将我的 .md 文件重命名为 .html,这样它们就不会被 MarkDown 渲染器渲染。

【讨论】:

    【解决方案3】:

    这应该可行。

    # frozen_string_literal: true
    
    module RawContent
      class Generator < Jekyll::Generator
        def generate(site)
          site.posts.docs.each do |post|
            post.data['raw_content'] = post.content
          end
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多