Jekyll中每个博客帖子的两个版本 [英] Two versions of each blog post in Jekyll

查看:67
本文介绍了Jekyll中每个博客帖子的两个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个非常简单的Jekyll设置中,我需要每个帖子的两个版本:面向公众的版本和带有专门用于嵌入的品牌的准系统版本.

I need two versions of each of my posts in a very simple Jekyll setup: The public facing version and a barebones version with branding specifically for embedding.

每种类型我都有一个布局:

I have one layout for each type:

post.html
post_embed.html

我可以通过复制每个帖子文件的开头部分具有不同布局的方式来完成此操作,但这显然是一种糟糕的方法.无论是在命令行级别还是在前端,都必须有一个更简单的解决方案?

I could accomplish this just fine by making duplicates of each post file with different layouts in the front matter, but that's obviously a terrible way to do it. There must be a simpler solution, either at the level of the command line or in the front matter?

更新: 这个 SO问题涵盖了为每个帖子创建JSON文件.我真的只需要一个生成器来遍历每个帖子,更改YAML前端问题中的一个值(embed_pa​​ge = True)并将其反馈回相同的模板.因此,每个帖子都会渲染两次,一次渲染为embed_page true,一次渲染为false.仍然没有足够的发电机知识.

Update: This SO question covers creating JSON files for each post. I really just need a generator to loop through each post, alter one value in the YAML front matter (embed_page=True) and feed it back to the same template. So each post is rendered twice, once with embed_page true and one with it false. Still don't have a full grasp of generators.

推荐答案

这是我的Jekyll插件来完成此任务.效率可能低得令人荒唐,但是我已经用Ruby编写了整整两天的内容.

Here's my Jekyll plugin to accomplish this. It's probably absurdly inefficient, but I've been writing in Ruby for all of two days.

module Jekyll
  # override write and destination functions to taking optional argument for pagename
  class Post
    def destination(dest, pagename)
      # The url needs to be unescaped in order to preserve the correct filename
      path = File.join(dest, CGI.unescape(self.url))
      path = File.join(path, pagename) if template[/\.html$/].nil?
      path
    end

    def write(dest, pagename="index.html")
      path = destination(dest, pagename)
      puts path
      FileUtils.mkdir_p(File.dirname(path))
      File.open(path, 'w') do |f|
        f.write(self.output)
      end
    end
  end

  # the cleanup function was erasing our work
  class Site
    def cleanup
    end
  end

  class EmbedPostGenerator < Generator
    safe true
    priority :low
    def generate(site)
      site.posts.each do |post|
        if post.data["embeddable"]
          post.data["is_embed"] = true
          post.render(site.layouts, site.site_payload)
          post.write(site.dest, "embed.html")
          post.data["is_embed"] = false
        end
      end
    end
  end
end

这篇关于Jekyll中每个博客帖子的两个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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