Jekyll 有没有办法检索 post 内容中的图片数量?

Is there a way in Jekyll to retrieve the number of pictures in a post's content?

在 Jekyll 的 post 中,我看到了如何用 include.content | number_of_words 计算字数,但我想知道是否有办法计算 content[=36] 中的图片=]?

我知道有一种方法可以获得特色图片,如果我将它添加到 frontmatter 中,例如:

feature-img: "img/foobar.png"

根据我的搜索,我确实看到了 post.layout,但我无法找到是否有办法获取 post 内容中的图像数量。当我搜索是否有人问过这个问题或者有人在 Jekyll 问题中提出这个问题时,我唯一没有得到任何结果,但我已经阅读:

  • Is there a way to access rendered content in Jekyll?
  • How to retrieve the current post index number in Jekyll?
  • How to include all files in a folder in Jekyll?

我可以看看我是否要为 post 构建一个画廊,将图像添加到 frontmatter 中,例如:

---
layout: post
title: "This is a title"
images:
  - url: "img/foo.png"
    alt: "Enter the foo"
    title: "apart of foo"

  - url: "img/bar.png"
    alt: "Enter the bar"
    title: "apart of bar"
---

但图像遍布整个内容。我想我可以将它硬编码到前面的每个 post 中,例如:

---
image-count: 4
---

但我认为这只会夸大前面的内容。在 Jekyll 中有没有办法让 post 的图像动态计数?

你可以拆分内容,像这样:

{% assign contentparts = content | split: '<img' %}
{% assing amountofimages = contentparts | minus:1 %}

然后使用:

{{ amountofimages }}

请注意,我没有对此进行测试。

我想出了一种方法来获取 post 中的图像数量,而无需在前面对图像数量进行硬编码:

{% assign postPics = include.content | split: "<img" | size %}