Jekyll中每篇博客文章有两个版本

3
我需要在一个非常简单的Jekyll设置中为每篇文章制作两个版本:公共展示版本和专门用于嵌入的带有品牌标识的基本版本。
每种类型都有一个布局。
post.html
post_embed.html

我可以通过在每个帖子文件的front matter中使用不同的布局来完成这个任务,但这显然是一种糟糕的方法。必须有一个更简单的解决方案,无论是在命令行级别还是在front matter中?更新:这个SO问题涵盖了为每个post创建JSON文件。我只需要一个生成器来循环遍历每个post,在YAML front matter中更改一个值(embed_page=True),并将其反馈到同一模板中。因此,每篇文章都会呈现两次,一次为embed_page true,一次为false。仍然没有完全掌握生成器。
1个回答

2
这是我为实现此功能编写的Jekyll插件。它可能非常低效,但我只学习了两天的Ruby编程。
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接