11

通过阅读Jekyll 的模板数据文档,人们可能会认为访问未渲染内容的方式是page.content:但据我所知,这是提供降价解析器已经呈现的帖子内容。

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

用例背景

我的用例如下:我使用pandoc 插件为我的 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或存在的),那么我可以轻松添加一个过滤器,该过滤器将使用 pandoc 和“webtex”选项在解析 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(只是乱码)。

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

4

3 回答 3

11

这是我认为你会遇到的麻烦:https ://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb https://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 访问。

于 2012-11-01T20:05:11.120 回答
1

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

于 2019-03-22T15:39:18.267 回答
0

这应该有效。

# 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
于 2020-06-26T08:36:34.757 回答