image - Jekyll 有没有办法检索帖子内容中的图片数量?
问题描述
在 Jekyll 的帖子中,我看到了如何计算单词,include.content | number_of_words
但我想知道是否有办法计算内容中的图片?
我确实知道如果我将它添加到 frontmatter 中,有一种方法可以获得特色图片,例如:
feature-img: "img/foobar.png"
根据我的搜索,我确实看到了post.layout
,但我无法找到是否有办法获取帖子内容中的图像数量。当我搜索是否有人问过这个问题或者有人在 Jekyll 问题中提出了这个问题时,我唯一没有得到任何结果,但我读过:
我可以看看我是否要为将图像添加到前端的帖子建立一个画廊,例如:
---
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"
---
但图像遍布整个内容。我想我可以将它硬编码到 frontmatter 中的每个帖子中,例如:
---
image-count: 4
---
但我认为这只会让前面的事情变得臃肿。在 Jekyll 中有没有办法让帖子的图片动态计数?
解决方案
您可能可以拆分内容,如下所示:
{% assign contentparts = content | split: '<img' %}
{% assing amountofimages = contentparts | minus:1 %}
然后使用:
{{ amountofimages }}
请注意,我没有对此进行测试。
推荐阅读
- react-native - 如何修复构建失败的 React Native 应用程序?
- javascript - 如何在 SPA 上使用随机生成的 API 密钥?
- forms - 更新到 xamarin forms 5 导致调试速度非常慢
- android - ScrollView 错误,即使它有一个孩子
- jmeter - 根据示例响应中的 queryTime 断言
- bash - 提取两个字符之间的行的正则表达式
- c# - ASP.NET Core 5 MVC 用员工等级计算工资
- php - PHP imagejpeg() 不会立即覆盖图像文件
- android-layout - Nativescript(JS,XML)中的版本感知组件
- r - Rvest/Stringr:删除不可见的符号分裂词?