Link: fallback to test if post does not exist

Is there a tag or a way to create a link to a post that may or may not exist yet?

I‘m looking to build a reference site (think a simple Wiki) where the some posts I’m linking to may not exist yet, but will at some point. So it would be nice if there was a tag where I could specify a post and if that post does not exist at build time, it just falls back to displaying the text instead of wrapping the text in an <a> tag.

Does such a tag exist in Jekyll out of the box?

Jekyll has the concept of future posts, but I do not think that is what you are looking to use.

Jekyll also has the concept of drafts, so you can put a post in the root _drafts folder and then move it to the _posts folder when ready. However, I wouldn’t say I like using that feature because it takes more steps to name the file when ready, and you have to move the post from one folder to another physically.

:arrow_right: Preferred method: New YAML tag

My preferred option is to use the standard _posts folder and add a new line of custom YML, like this:

/posts/2022-05-24-my-post.md

---
title: My post
date: 2022-05-24
layout: post
isPublic: false
---
Here is the text for this unfinished post

Note how I added isPublic: false. That means you can write custom code to display (or not display) the post. Of course, you can type isPublic: true for any posts that should have links.

:negative_squared_cross_mark: Display the post without a link

The following code will display all the posts, but not provide a link if isPublic: false. I added the [unfinished work] text at the beginning just so the reader knows there is not a link yet:

{%- assign posts = site.posts -%}
{%- for post in posts -%}
  {%- if post.isPublic -%}
    <a href="{{ post.url | relative_url }}">
      {{ post.title | escape }}
    </a>
  {%- else -%}
      [unfinished work] {{ post.title | escape }}
  {%- endif -%}
  <p>
{%- endfor -%}

:face_with_peeking_eye: Do not display unfinished links

If you do not want to display the post at all, you can use the following code:

{%- assign posts = site.posts | where_exp: "post", "post.isPublic" -%}
{%- for post in posts -%}
  <a href="{{ post.url | relative_url }}">
    {{ post.title | escape }}
  </a>
{%- endfor -%}

Considerations

:open_book: Drafts and future posts

I mentioned that draft and future posts might be an option to look into, so here are the direct links just in case you want to read up on them.

To learn more about draft posts, check out this link:

To learn more about future posts, check out this link:

:books: Collections (for awareness)

Since you are building a wiki, you might find that you want to categorize and manage these posts in folders. That method is helpful if there are primary categories of content and you have a considerable amount of content (think hundreds or thousands of posts over a few dozen posts). While collections will still require code, as I shared above, I thought you might be interested in this feature.

To read about collections, check out this link: