• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Jekyll plugin that copies static files from the _posts to the _site folder

jekyll-postfiles

Gem Version Gem Downloads Build Status

Easing the management of images (and other files) attached to Markdown posts

The pain of Jekyll's recommended posts assets management

Jekyll's natural way to deal with static files attached to posts, like images or PDFs, is to put them all in a global assets/ (or downloads/) folder at the site root. Read "Including images and resources" in Jekyll's documentation.

You can of course put files in subfolders of assets/, but it will be really cumbersome to manage posts' Markdown files in _posts/ or a subfolder, and images elsewhere, and then use the good hierarchy in all Markdown image tags.

Imagine you have these files:

_posts/
  2016-06/
    2016-06-09-so-long-cloudflare-and-thanks-for-all-the-fissh.md
…
assets/
  2016-06-09-cloudflare/
    cloudflare-architecture.png
    performance-report-sample.pdf

To use the image and PDF files in the post's Markdown, you will have to write this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.

![Cloudflare architecture](/assets/2016-06-09-cloudflare/cloudflare-architecture.png)

Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.

Here is [an example of performance report](/assets/2016-06-09-cloudflare/performance-report-sample.pdf).

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.

Painful to write.

Imagine you want to change the post's publication date, or one of the file names?

Painful to update.

What if you want to put new WIP Markdown files in _drafts/, and the attached assets somewhere in a way they won't be copied to the destination _site/ folder next time you build the site? You can't put the files in the assets/ folder, so when you will publish the draft, you will have to change the assets location in the Markdown file.

Painful, and prone to errors.

And what about previewing the content while editing? If you use an editor like MacDown with live preview, how will it find the actual path to the images? What means /assets/… for the editor?

Painful to preview.

There must be another way

What if instead, you could have the files stored like that:

_posts/
  2016-06-09-cloudflare/
    2016-06-09-so-long-cloudflare-and-thanks-for-all-the-fissh.md
    cloudflare-architecture.png
    performance-report-sample.pdf

And if you could write your Markdown like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.

![Cloudflare architecture](cloudflare-architecture.png)

Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.

Here is [an example of performance report](performance-report-sample.pdf).

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.

Much easier!

  • Easy to store, everything is in one single folder.
  • Easy to write, no path to add to file links
  • Easy to update
  • Easy to move from _drafts/ to _posts/, without anything to change in the Markdown content
  • Easy to edit in any editor with live preview

Not every assets need this

Some Jekyll users will try to convince you it's a bad idea, because it means the asset is tightly linked to the post.

In my own experience, 95% of assets, at least, are used in one single post. And this is pretty common to find such requests from users of other static generators, like Hugo (fixed in May 2015), Nikola (already there, but not obvious or user friendly), Octopress, etc.

But it's true this might not be ideal for all assets (the remaining 5%), so you can of course continue using full assets paths with /assets/… to have a few assets shared by several posts.

How does it work?

This plugin takes any file that is in posts folders, and copy them to the folder in which the post HTML page will be created.

Let's say you have these files:

_posts/
  2016-06-09-cloudflare/
    2016-06-09-so-long-cloudflare-and-thanks-for-all-the-fissh.md
    cloudflare-architecture.png
    performance-report-sample.pdf

And your Jekyll settings for permalinks are these:

# Permalinks
permalink: /:year/:month/:day/:title/

Jekyll with this plugin will generate the site content like this:

2016/
  06/
    09/
      so-long-cloudflare-and-thanks-for-all-the-fissh/
        index.html
        cloudflare-logo.png
        performance-report-sample.pdf

If you change your Jekyll settings for permalinks like these:

# Permalinks
permalink: /:year/:month/:day/:title.html

Jekyll with this plugin will generate the site content like this:

2016/
  06/
    09/
      so-long-cloudflare-and-thanks-for-all-the-fissh.html
      cloudflare-logo.png
      performance-report-sample.pdf

Handy, isn't it?

Installation

Add gem 'jekyll-postfiles' to the jekyll_plugin group in your Gemfile:

source 'https://rubygems.org'

gem 'jekyll'

group :jekyll_plugins do
  gem 'jekyll-postfiles'
end

Then run bundle to install the gem.

Usage

You don't have anything to do.

Just put the images (and PDFs, etc.) in the same folder as your Markdown files, or even subfolders, and use the standard Markdown image syntax, with a relative path.

Compatibility

⚠️ This plugin is not supported by Github Pages, host your site on services like https://netlify.com which support third party plugins.

Contributing

Thanks for your interest in contributing! There are many ways to contribute to this project. Get started here.

License

The gem is available as open source under the terms of the MIT License.

Thanks

Inspired by this old Gist by @kevinoid.

More Repositories

1

vscode-gremlins

Gremlins tracker for Visual Studio Code: reveals invisible whitespace and other annoying characters
JavaScript
188
star
2

pack11ty

An heavily opinionated Eleventy template project with Sass
SCSS
183
star
3

jekyll-cloudinary

Jekyll plugin adding a Liquid tag for Cloudinary, for better responsive images
Ruby
89
star
4

nicolas-hoizey.com

The personal website/blog from Nicolas Hoizey, built with https://pack11ty.dev/
JavaScript
82
star
5

1y

A template project to build a short URL manager with Eleventy
Nunjucks
76
star
6

images-responsiver

Global solution for responsive images, transforming simple image HTML syntax into better responsive images syntax.
JavaScript
64
star
7

PinboardInGoogle

Showing Pinboard bookmarks alongside Google search results
JavaScript
19
star
8

Better500px-WebExtension

Enhances 500px.com with a few useful shortcuts for better navigation
JavaScript
19
star
9

spip2markdown

Migration facilitée de la syntaxe spécifique à SPIP vers Markdown
PHP
18
star
10

macOS-init

Script d'installation complète des applications sur un Mac vierge
Shell
18
star
11

HTML5-Titles-Inception

How to style h1-h3 elements according to the number of sectioning elements they are embeded into.
CSS
13
star
12

github-action-feed-to-mastodon

A GitHub Action to create toots on Mastodon from a JSON Feed
JavaScript
12
star
13

nicolas-hoizey.photo

Nicolas Hoizey's photography portfolio
Nunjucks
11
star
14

demo-11ty-netlify-cloudinary

A demonstration of responsive images in simple Markdown with Eleventy, Netlify and Cloudinary
JavaScript
5
star
15

automations

3
star
16

eleventy-plugin-link_to

A Nunjucks tag to link to another internal content in Eleventy with its slug.
JavaScript
3
star
17

eleventy-plugin-images-responsiver

Global solution for responsive images in Eleventy, allowing authors to use Markdown syntax for image and yet get responsive images in generated HTML.
JavaScript
3
star
18

precious-prana.com

Precious Prana, communauté qui permet aux mères actives de se ressourcer
JavaScript
2
star
19

flickrSuggest

PHP
2
star
20

github-labels

Tools to sync default labels for my Github repositories Issues and Pull Requests
Shell
2
star
21

homebrew-cask

My own tap of Homebrew casks
Ruby
2
star
22

jekyll-firstimage

A Jekyll plugin that adds a Liquid filter finding the first image in a HTML content string, including responsive images srcset
Ruby
2
star
23

eleventy-plugin-pack11ty

An heavily opinionated set of filters, shortcodes, transforms, collections for Eleventy, linked to the Pack11ty template project
JavaScript
1
star
24

knowledge-base

My own slightly opinionated knowledge base
JavaScript
1
star
25

lab.nicolas-hoizey.com

Nicolas Hoizey's lab
HTML
1
star