如何使用液体标签访问Jekyll中未渲染(降价)的内容? [英] How can I access un-rendered (markdown) content in Jekyll with liquid tags?

查看:57
本文介绍了如何使用液体标签访问Jekyll中未渲染(降价)的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

From reading the documentation Jekyll's template data one might think that the way to access un-rendered content would be page.content; but as far as I can tell, this is providing the content of the post as already rendered by the markdown parser.

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

I need a solution that accesses the raw (original markdown) content directly, rather than simply trying to convert the html back to markdown.

我的用例如下:我使用 pandoc插件为我的Jekyll网站提供降价促销,方法如下: "mathjax"选项可获取漂亮的方程式.但是,mathjax需要使用javascript,因此它们不会显示在RSS提要中,而我通过循环遍历page.content生成了RSS提要,如下所示:

My use case is the following: I use the pandoc plugin to render markdown for my Jekyll site, using the 'mathjax' option to get pretty equations. However, mathjax requires javascript, so these do not display in the RSS feed, which I generate by looping over page.content like so:

 {% 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或类似的内容),那么我可以轻松地添加一个过滤器,该过滤器在解析RSS feed时使用带有"webtex"选项的pandoc为方程式生成图像,例如:

As the xml_escape filter implies, post.content here appears in html. If I could get the raw content (imagine post.contentraw or such existed) then I could easily add a filter that would use pandoc with the "webtex" option to generate images for equations when parsing the RSS feed, e.g:

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

但是当我对已经在html + mathjax中呈现的方程式而不是原始的markdown感到满意时,我陷入了困境.转换回markdown并没有帮助,因为它不会转换mathjax(简单地显示为乱码).

But as I get content with the equations already rendered in html+mathjax instead of the raw markdown, I'm stuck. Converting back to markdown doesn't help, since it doesn't convert the mathjax (simply garbles it).

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

Any suggestions? Surely there's a way to call the raw markdown instead?

推荐答案

这是我认为您将遇到的麻烦:

Here's the trouble that I think you'll have: 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行:

From my reading, for a given post/page self.content is replaced by the result of running self.content through Markdown and Liquid, at line 79 in convertible.rb:

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

帖子显示在页面之前,在site.rb的第37-44行和197-211行可见:

Posts are rendered before pages, seen at lines 37-44 and 197-211 in site.rb:

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-因此,这不是停止呈现该页面的情况.已经完成了.

By the time you get to rendering this page, self.content has been rendered to HTML - so it isn't a case of stopping it rendering. It's already done.

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

However, Generators (https://github.com/mojombo/jekyll/wiki/Plugins) run before the render stage, so, as far as I can tell from reading the source, you should be able to fairly trivially write a generator which will duplicate self.content into some attribute (such as self.raw_content) which you can later access as raw Markdown in your templates {{ page.raw_content }}.

这篇关于如何使用液体标签访问Jekyll中未渲染(降价)的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆