【问题标题】:Two versions of each blog post in JekyllJekyll 中每篇博文的两个版本
【发布时间】:2026-01-11 10:55:01
【问题描述】:

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

每种类型都有一个布局:

post.html
post_embed.html

我可以通过在前面制作具有不同布局的每个帖子文件的副本来很好地做到这一点,但这显然是一种糟糕的方法。一定有更简单的解决方案,要么在命令行层面,要么在前端?

更新: 这个SO question covers creating JSON files for each post。我真的只需要一个生成器来循环遍历每个帖子,更改 YAML 前文中的一个值(embed_pa​​ge=True)并将其反馈给同一个模板。所以每篇文章都会被渲染两次,一次是embed_page true,一次是 false。仍然没有完全掌握生成器。

【问题讨论】:

    标签: ruby templates jekyll static-pages


    【解决方案1】:

    这是我的 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
    

    【讨论】:

      最近更新 更多