Entire collection inside single file

Currently, I build a ‘news’ page on my website using the following structure.

  • Inside the config.yml file, there is the following code:
collections:
  news:
    defaults:
      layout: post
    output: true
    permalink: /news/:path/
  • I have a _news folder with many .md files for each news item, which I call news1.md, news2.md, and so on. Each of these files looks like this:
---
date: 2018-07-01
inline: true
title: some title
---

Some text indicating the news
  • Under the _pages directory, I have a file called news.md with code that makes use of this collection:
---
layout: page
title: news + events
permalink: /news/
description: Here's what I have been up to recently.
nav: false
---

{% assign sorted_news = site.news | sort:"date" | reverse %}
{% for news in sorted_news %}
<article class="post-content">
 {{ news.date | date: "%b %d, %Y"}}
 {{ news.content }}
<article class="post-content">
{% endfor %}

This arrangement works well — I have one page, <site>/news, which contains a chronological list of all the news items. I don’t have (and don’t care for) individual html pages for each news item.

Now for my question. Is there a way to do this using a single .md file containing all my news items? It is getting cumbersome to have a new file just for a couple of lines of text. It would be much more convenient if there was just one .md file containing the text for every item in my collection.

I don’t see why not, jekyll doesn’t care how you do it. In the news.md page you could just edit it each time to add content as needed. I’d assume you would add it to the top each time? might get a little cumbersome over time.

what part of that are you not sure about?

maybe the styling of each news item? fairly sure md supports classes you just need to do a little searching for how to do it.

You can also use a data file and just edit it. Though I am not sure what the advantage of that would be.

Manually writing a news.md file would work, but it’s not a very elegant solution. For example, if I wanted to change something about how the news items are styled, I would have to do so manually for them all. With a collection and a for loop, I can separate out the styling from the content.

So, I would still like to know if there is a way that Jekyll collections support using a single file to store the entire collection. Another reason why this would be helpful is that I am using a template (GitHub - alshedivat/al-folio: A beautiful, simple, clean, and responsive Jekyll theme for academics) in which the following code in the ‘about’ page layout

{% if page.news %}
  {% include news.html %}
{% endif %}

renders the five most recent news items on the ‘about’ page, with a hyperlink to see the remainder. This is pretty neat, and I’m not sure how it’s achieved, but it seems that for this to work, the news items should form items in a collection rather than simply text in a .md file.

technically maybe you could do what you describe with front matter in one collection file but that would be a little ridiculous.

A data file could do what you are asking for, you could have a limit when you loop over them to do just the most recent ones.

Looping thru data files is more or less like looping thru collection items, main difference being that you can’t output data items to their own page, you have to loop over them on a page.

Main issue with a data file is formatting - it can be a little annoying to write in yml or even worse json. But it would be easier than making a new file I think.

Have you looked at data files?

as for my initial suggestion, the styling could be handled by classes and that would separate the content from the styling to an extent. But there would be no easy way to do the most recent 5. I would find this method easiest to add new items to, just not easy to do other things with like the new items thing

You could also use JS and store the news items in an actual db but by then you might want to use something other than jekyll.

Okay, thanks for your help! I will look into using data files, but after this discussion I think that I’d rather just skip to my current system – collection with each entry in a different file.