如何使用液体标签访问Jekyll中的未渲染(降价)内容?

时间:2012-10-31 13:37:57

标签: markdown jekyll liquid mathjax pandoc

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

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

用例背景

我的用例如下:我使用pandoc plugin为我的Jekyll网站渲染markdown,使用'mathjax'选项获得漂亮的方程式。但是,mathjax需要javascript,所以这些不会显示在RSS feed中,我通过循环遍历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中呈现的等式而不是原始降价的内容感到满意时,我就被卡住了。转换回markdown没有帮助,因为它不会转换mathjax(只是简单地加油)。

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

3 个答案:

答案 0 :(得分:9)

以下是我认为你会遇到的麻烦: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 - 因此不会停止渲染。它已经完成了。

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

答案 1 :(得分:0)

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

答案 2 :(得分: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
相关问题