如何使用液体标签访问Jekyll中的未呈现(降价)内容?(How can I access un

编程入门 行业动态 更新时间:2024-10-20 11:32:50
如何使用液体标签访问Jekyll中的未呈现(降价)内容?(How can I access un-rendered (markdown) content in Jekyll with liquid tags?)

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

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

用例背景

我的用例如下:我使用pandoc插件为我的Jekyll站点提供降价,使用'mathjax'选项获得漂亮的方程式。 然而,mathjax需要javascript,所以这些不会显示在RSS feed中,我通过循环page.content生成, 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或者存在的话),那么我可以很容易地添加一个过滤器,它可以在解析RSS提要时使用带有“webtex”选项的pandoc生成图像,例如:

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(简单地将其删除)。

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

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.

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

Background on use case

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 %}

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)

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?

最满意答案

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

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

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)

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

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.

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 }}.

更多推荐

本文发布于:2023-04-28 01:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   液体   标签   内容   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!