中间人博客扩展:同一来源的两个输出变体

Middleman blog extension: Two output variants of same source

我想要一个包含文章的源文件夹并生成两个或多个输出变体。 (例如 print/archive 版本或用于 A/B 测试的特殊移动版本,其中一个变体具有规范 link。)

当有这样的配置时

activate :blog do |blog|
  blog.name = "variant"
  blog.sources = "news/{year}-{month}-{day}-{title}.html"
  blog.layout = "news/variant-layout"
  blog.permalink = "variant/{year}/{title}.html"
  ...
end
...
activate :blog do |blog|
  blog.name = "news"
  blog.sources = "news/{year}-{month}-{day}-{title}.html"
  blog.layout = "news/layout"
  blog.permalink = "news/{year}/{title}.html"
  ...
end

请注意生成 URL 的 blog.permalink 配置的差异。

布局混乱,link错误(总是指向配置文件中最后出现的版本)和页面缺失。

我添加了一个自定义扩展来挂钩中间人生命周期,将缺少的资源添加到站点地图。 (我认为这是一个 hack...)至少在那之后出现了缺失的页面,但是布局错误并且 links 总是指向错误的版本。 尝试 proxy 使用不同的模板似乎已被阻止,因为博客扩展本身会生成动态代理页面。 我目前坚持使用 middleman 3.4,因为 middleman 4 的 asciidoc 扩展尚未发布。这是中间人的一般限制,我不能生成多个变体吗?

我认为您最好的做法如下:

  • 升级到gem 'middleman', '~> 4.1.7'
  • 使用gem 'middleman-targets'

然后您可以配置两个构建目标:defaultvariant,如下所示:

# config.rb

set :target, :default

set :targets, {
  default: {
     layout: 'layout-one',
     build_dir: 'build/default',
     target_specific_config: 'foo',
     features : {
        feature_one: true 
     }  
  },
  variant: {
     layout: 'layout-two',
     build_dir: 'build/variant',
     target_specific_config: 'bar',
     features : {
        feature_one: false 
     }  
  }

现在您应该可以像这样切换完整的布局:

# layout.erb

<% wrap_layout(target_value(:layout)) do %>
  <%= yield %>
<% end %>

或者您可以在各个页面中使用功能标志或特定配置值,如下所示:

# page.erb

<% if target_feature?(:feature_one) %>
  <p>Feature One Is ON</p>
  <p>Value is: <%= target_value(:target_specific_config) %>
<% else %>
  <p>Feature One Is OFF</p>
  <p>Value is: <%= target_value(:target_specific_config) %>
<% end %>

文档目前有点稀缺,所以最好在这里阅读源代码的 'helpers' 部分:https://github.com/middlemac/middleman-targets/blob/master/lib/middleman-targets/extension.rb