Jekyll定制收藏钩

问题描述 投票:1回答:2

这个问题困扰了我一段时间了。

生成的Jekyll站点确实在所需页面上显示正确的集合及其所有信息。但是除了(默认)posts集合之外的所有集合都不能用在ruby插件中。

案件:

我们有一个使用Jekyll的博客站点。要使用作者信息丰富博客帖子,反之亦然,有一些使用Jekyll::Hooks.register的自定义插件

博客文章以markdown语言编写,并且都包含YAML格式的一些信息:

---
layout: post
authors: [author]
title: 'title'
intro: a short intro text
image: /path/to/image.png
tags: [jekyll, collections]
category: Programming
comments: true
---

这是用作者信息丰富帖子的代码:

Jekyll::Hooks.register :site, :post_read do |site|
    puts 'Enriching posts with link to authors'

    site.posts.docs.each { |post|
        post.data['authors'].collect! { |author_username|
            site.data['authors'][author_username]
        }.compact!
    }
end

下一步:

由于我们还想展示将要提供的培训课程和讲座,因此我更新了两个馆藏。

两个集合都包含一些markdown格式的文件,文件顶部有相同类型的YAML信息。

---                               ---
layout: talk                      layout: training
speakers: [author]                trainers: [author]
title: Title                      title: Title
subtitle: Subtitle                subtitle: Subtitle
duration: 120 minutes             duration: 180 minutes
level: beginner                   level: intermediate
image: /path/to/image.png         image: /path/to/image.png
tags: []                          tags: []
category: Talks                   category: Training
comments: false                   comments: false
---                               ---

我将这些收藏品添加到了_config.yml

collections:
  training:
      output: true
      permalink: /training/:path/
  talks:
      output: true
      permalink: /talks/:path/

defaults:
  - scope:
      path: "training"
      type: training
    values:
      layout: training
  - scope:
      path: "talks"
      type: talks
    values:
      layout: talk

期望的实施:

现在我想与作者一起丰富会谈和培训课程。

由于帖子,会谈和训练文件都在一个集合中,我假设将posts对象更改为talkstraining应该获得所需的对象并循环它们。

所以我将自定义插件更改为:

Jekyll::Hooks.register :site, :post_read do |site|
    puts 'Enriching posts with link to authors'

    site.posts.docs.each { |post|
        post.data['authors'].collect! { |author_username|
            site.data['authors'][author_username]
        }.compact!
    }

    site.talks.docs.each { |talk|
        talk.data['authors'].collect! { |author_username|
            site.data['authors'][author_username]
        }.compact!
    }
end

但后来我得到了这个错误:

jekyll 3.7.3 | Error:  undefined method `talks' for #<Jekyll::Site:0x00007f9325268e90>

困扰我的是,当我记录集合内容时,我得到以下结果:(格式化为json结构以获得更好的可读性)

{
  "posts"=>
    #<Jekyll::Collection @label=posts docs=[
      #<Jekyll::Document _posts/2017-02-01-page1.md collection=posts>, 
      #<Jekyll::Document _posts/2017-02-02-page2.md collection=posts>,
      #<Jekyll::Document _posts/2018-04-04-page3.md collection=posts>
    ]>, 
  "training"=>
    #<Jekyll::Collection @label=training docs=[
      #<Jekyll::Document _training/page1.md collection=training>, 
      #<Jekyll::Document _training/page2.md collection=training>, 
      #<Jekyll::Document _training/page3.md collection=training>
    ]>, 
  "talks"=>
    #<Jekyll::Collection @label=talks docs=[
      #<Jekyll::Document _talks/page1.md collection=talks>, 
      #<Jekyll::Document _talks/page2.md collection=talks>, 
      #<Jekyll::Document _talks/page3.md collection=talks>
    ]>
}

据我所知,talkstraining集合与posts集合处于同一级别和数据结构。

题:

如何在我的自定义插件中获得会话/培训集合,以便我可以使用作者信息丰富数据。

为了便于阅读,我减少了文件和输出。 GitHub提供完整的代码库

ruby collections jekyll hook
2个回答
1
投票

你必须看看site.collections

这可以做到这一点:

Jekyll::Hooks.register :site, :post_read do |site|

    ['posts', 'talks'].each do collection{
        site.collections[collections].docs.each { |talk|
            talk.data['authors'].collect! { |author_username|
                site.data['authors'][author_username]
            }.compact!
        }
    }

end

0
投票

David指出我正确的方向。

我不得不循环收藏

site.collections.each do |collection|
  collection.each do |item|
    if item.respond_to?("each")
      item.docs.each do |post|
        if post.data['authors']
          post.data['authors'].collect! { |author_username|
            site.data['authors'][author_username]
          }.compact!
        end
      end
    end
  end
end

我仍然觉得奇怪的是site对象上没有其他集合。

但这对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.