GʀᴜᴍᴘʏCᴀᴛ
GʀᴜᴍᴘʏCᴀᴛ

Reputation: 8938

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

In a Jekyll's post I've seen how to count words with include.content | number_of_words but I was wondering if there was a way to count pictures in the content?

I do know there is a way to get a featured image if I add it to the frontmatter like:

feature-img: "img/foobar.png"

Per my searches I did see post.layout but I am unable to find if there is a way to get the count of images in a post's content. When I search to see if this has been asked or someone has brought this up in Jekyll issues the only I do not get any results but I have read:

I could see if I was going to build a gallery for a post adding the images to the frontmatter like:

---
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"
---

but the images are spread throughout the content. I guess I could hard code it to every post in the frontmatter like:

---
image-count: 4
---

but I think that just bloats the frontmatter. In Jekyll is there a way to get the post's images count dynamically?

Upvotes: 1

Views: 141

Answers (2)

GʀᴜᴍᴘʏCᴀᴛ
GʀᴜᴍᴘʏCᴀᴛ

Reputation: 8938

I've figured out a way to get the count of images in a post without hard coding the image count in the front matter:

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

Upvotes: 0

Mr. Hugo
Mr. Hugo

Reputation: 12590

You can probably split the content, like this:

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

Then use:

{{ amountofimages }}

Note that I did not test this.

Upvotes: 1

Related Questions