如何为单个 post 指定多个作者,然后计算每个作者与之相关的 post ?

How to specify multiple authors for a single post, then count the posts each author is related to?

我想做什么:

我正在尝试为 posts 每个作者 所写的内容创建一个列表。这是一个问题,因为 each post 应该能够指定多个作者。

假设我们总共有 3 个 post 和 3 个作者。

编辑 1: 按照答案中的建议,最好通过在前言列表而不是 CSV 字符串中列出作者来完成。所以,

像这样

Post:

---
title: post
authors:
  - foo
  - bar
---

而不是像这样:

Post:

---
title: post
authors: [foo, bar]
---

问题设置:

(编辑,根据编辑1

Post 1:

---
title: post1
authors:
  - foo
  - bar
---

Post 2:

---
title: post2
authors:
  - foo
  - bar
---

Post 3:

---
title: post3
authors:
  - john doe
  - bar
---

输出:

bar (3) 
john doe (1)
foo (2)

点击作者应该会显示所有 post。

或者一个数组也可以这样显示,但这对情况没有帮助,只是一个等效的样式。

我试过的

我对类别和标签做了同样的事情,this algorithm 效果很好。但是,以某种方式不支持将 site.categories 替换为 site.authors

returns:

Liquid Exception: 0 is not a symbol nor a string in authors.html

我想这是由于 categoriestags 默认情况下是数组的性质。 我认为能够以某种方式将 front matter 标记 authors 设置为数组会有所帮助。我想这是在 _config.yml 中完成的,但我一头扎进去了。

到目前为止,我已经想出了一种方法来定位数组中的各个作者,但我还远远不能单独列出他们并计算他们的数量。我想我是有限的,因为作者的性质在默认情况下不是数组,否则像 this one 这样的实现可以使用像 authors 这样的自定义前端变量,但它们不能。

我的意思(当我说"As of now"时):

{% for post in site.posts %}
    <li><a href="{{ SITEURL }}/{{ author.url }}">{{ post.authors }}</a> ({{ post.authors[0] }})</li>
{% endfor %}

输出:

foobar (foo)
foobar (foo)
john doebar (john doe)

毕竟,我想我在这里遗漏了一些东西。我可能不是第一个尝试这个的人,但我发现的唯一文档来自那些尝试过我正在尝试但没有真正实现的人。

This user for example created a way to count users,但与 site.authors 一起使用时

it returns array size == 0-if 子句:

No author

这似乎是一件简单的事情,但由于某些原因并非如此。至少对我来说。

编辑 2:

根据 Kieth 的回答,我离目标更近了,但我在创建空数组时遇到了问题。根据 Jekyll 中的这个问题,这似乎是 problem in general. However a workaround seems to be to assign a variable and 。 目前我正在努力将作者添加到数组中,以便我可以评估它的大小。

{% assign authors = site.posts | map: 'authors' | uniq %}
{% assign authorCount = '' | split: '' %}

  <div class="softwaretitle">
    {% for author in authors %}
      <a>{{ author }}</a>


      {% for post in site.posts %}
        {% for post_author in post.authors %}
          {% if post_author == author %}

            {% assign authorCount = authorCount | push author %}
            <a>({{ page.authorCount | size }})</a>

          {% endif %}
        {% endfor %Edit 2:}
      {% endfor %}
    {% endfor %}
  </div>

输出:

Error: Liquid error (.../_includes/authors.html line 14): wrong number of arguments (given 1, expected 2) included Error: Run jekyll build --trace for more information.

第 14 行:

{% assign authorCount = authorCount | push author %}

编辑 3:

最后,最终结果(没有link到post列表,但这是细节)

<!-- Get a list of unique authors -->
  {% assign authors = site.posts | map: 'authors' | uniq | sort%}
  {% assign author_post_count = 0 %}
  {% for author in authors %}
          {% assign author_post_count = 0 %}

    <div class="">
      <li><a>{{ author }}
      {% for post in site.posts %}

        {% for post_author in post.authors %}
          {% if post_author == author %}
          {% assign author_post_count = author_post_count | | plus: 1 %}

          {% endif %}

        {% endfor %}
      {% endfor %}
      <span>&nbsp ({{ author_post_count }})</span></a></li>
    </div>
{% endfor %}

输出:

bar (3) 
john doe (1)
foo (2)

更新的答案:

获取作者列表(不重复)并包括作者贡献的帖子总数,以及帖子标题列表和帖子的 link。

  {% assign authors = site.posts | map: 'authors' | uniq %}

  {% for author in authors %}
  {% assign author_post_count = 0 %}
   {% for post in site.posts %}
    {% if post.authors %}
     {% for post_author in post.authors %}
      {% if post_author == author %}
        {% assign author_post_count = author_post_count | plus: 1 %}
      {% endif %}
     {% endfor %}
    {% endif %}
  {% endfor %}
      <h2>{{ author }} - {{ author_post_count }}</h2>
      {% for post in site.posts %}
       {% if post.authors %}
        {% for post_author in post.authors %}
          {% if post_author == author %}
           {% assign author_url_query =  author | replace: ' ', '-' %}
            <a href="{{ post.url }}" title="A posts {{ author }}">
             {{ post.title }}
            </a>
          {% endif %}
        {% endfor %}
        {% endif %}
      {% endfor %}
  {% endfor %}

相反,如果您想为每个作者创建一个页面,其中包含他们撰写的帖子列表(自动生成),则需要通过自定义插件扩展 Jekyll。如果您有使用 Ruby 编程语言的经验,这是很有可能的。这是一个非常接近的示例:https://github.com/avillafiorita/jekyll-datapage_gen 并且您可以简单地删除 _config 数据要求并对目录名称和 permalinks 进行硬编码 :)

问题:

  • 在 post 上:打印作者 + 写的 post 的数量和 link 到作者页面
  • 在作者页面上:打印作者数据 + 列表 posts

解决方案

我们已经有 post 个描述如下:

---
title: post2
authors: [foo, bar]

# or with the alternate YAML array syntax
authors:
  - foo
  - bar

---

对于作者,我们可以使用将自动生成作者页面的特定集合。

_config.yml中:

collections:
  authors:
    output: true

defaults:
  - scope:
      type: authors
    values:
      layout: authors

一个作者的页面可以这样描述:

_authors/foo.md

---
uid : foo
firstname: John
lastname: Foo
---

Some bio here ...

帖子列表(索引或任何页面):

{% assign posts = site.posts %}
{% comment %}
or {% assign posts = paginator.posts %} if you use pagination
{% endcomment %}

<ul>
{% for post in posts %}
  <li>
    <a href="{{ site.baseurl }}{{ p.url }}">{{ p.title }}</a>
    <br>{% include authors.html post=post %}
  </li>
{% endfor %}
</ul>

我们还将使用我们的作者包含在 _layouts/post.html

...
    <h1 class="post-title" itemprop="name headline">{{ page.title | escape }}</h1>
    <p>{% include authors.html post=page %}</p>
...

现在魔法:_includes/authors.html

{% assign post = include.post %}

{% comment %}## if we have a least one author in post authors array {% endcomment %}
{% if post.authors and post.authors != empty %}

  {% comment %} ## We will build a string for each author,
                   store it in authorsArray
                   then reassemble it at the end {% endcomment %}
  {% assign authorsArray = "" | split: "" %}

  {% for author in post.authors %}

    {% comment %}## Count posts for current author {% endcomment %}
    {% assign authorsPostsCount = site.posts | where_exp: "post", "post.authors contains author" | size %}

    {% comment %}## Get authors data based on uid matching in the collection {% endcomment %}
    {% assign authorsDatas = site.authors | where: "uid", author | first %}

    {% if authorsDatas %}
      {% capture authorString %}
      <a href="{{ site.baseurl }}{{ authorsDatas.url }}">{{ authorsDatas.firstname }} {{ authorsDatas.lastname }} ({{ authorsPostsCount }}) </a>
      {% endcapture %}

    {% else %}
      {% comment %}## No entry for this author in the collection
                   ## or author spelling is wrong {% endcomment %}
      {% capture authorString %}
        {{ author | capitalize }} ({{ authorsPostsCount }})
      {% endcapture %}
    {% endif %}

    {% comment %}## Push result in authorsArray {% endcomment %}
    {% assign authorsArray = authorsArray | push: authorString %}
  {% endfor %}
  {% comment %}## Create a sentence with array elements {% endcomment %}
  by {{ authorsArray | array_to_sentence_string }}
{% endif %}

_layouts/author.html

---
layout: default
---

<h1>{{ page.firstname }} - {{ page. lastname }}</h1>

{% assign authorsPosts = site.posts | where_exp: "post", "post.authors contains page.uid" %}

<ul>
  {% for p in authorsPosts %}
    <li><a href="{{ site.baseurl }}{{ p.url }}">{{ p.title }}</a></li>
  {% endfor %}
</ul>