Eleventy
Eleventy is my static site generator of choice for my personal sites and projects.
Ignore .gitignore
#
By default, Eleventy won't watch files listed in your .gitignore
. You can tell it to not do that:
eleventyConfig.setUseGitIgnore(false)
Changing Directory Locations #
I tend to do this so I can have the source of my project inside a directory instead of all of it in the root. In .eleventy.js
:
return {
dir: {
input: "src/pages",
output: "public",
}
}
Copy Assets and Files to the Output #
If you want non-template files copied through to your final build (like css files or images), you can use a passthrough in .eleventy.js
:
eleventyConfig.addPassthroughCopy({ "src/pages/_eleventy/assets": "assets" })
Filters #
Filters allow you to extend the template engines to modify content. I'm probably misusing these in some places. Below is an example of a filter to strip /index.html
from some paths I had on my personal site.
eleventyConfig.addFilter('stripIndex', function(path) {
return path.replace('/index.html', '/');
})
Using Local plugins #
.eleventy.js
eleventyConfig.addFilter("myPlugin", require('./path/to/plugin.js'))
plugin.js
module.exports = function() {
return 'my plugin'
}
Object Debug #
// .eleventy.js
eleventyConfig.addFilter('objectDebug', function(value) {
return `<pre>${JSON.stringify(value, '', 2)}</pre>`
})
// layout.njk
{{ myObject | objectDebug | safe }}
Highlight Lines in Code Blocks #
Lines are zero-indexed.
```lang/1 const myFunction = () => { return 'Hello' } ```
Outputs:
const myFunction = () => {
return 'Hello'
}
Ignore Nunjucks in Markdown Files #
templateEngineOverride: njk,md
// index.md
{% aShortCode %}
{{ aVariable }}
Post Archive by Year #
{% set datePrinted = false %}
{% set currentYear = 1000 %}
{% set started = false %}
{%- for post in collections.posts %}
{% set postYear = post.data.date.getFullYear() %}
{% if postYear != currentYear %}
{% if started %}
</ul>
{% endif %}
<p>{{ postYear }}<p>
<ul>
{% set started = true %}
{% endif %}
<li>
{{ post.data.title }}
</li>
{% set currentYear = postYear %}
{% endfor %}
Automatic Ordering for Eleventy Navigation #
Eleventy Navigation is a great plugin for generating navigation and breadcrumbs. One of the properties is order
to indicate the order of the pages. This is great if you have a few pages but if you have hundreds (like this site does) you can get some unexpected results like random ordering, especially if you have some ordered but not others.
To solve this, I create a pageIndex
collection by mapping through the ordered collection of pages:
// .eleventy.js
eleventyConfig.addCollection('pageIndex', function(collections) {
let pageIndex = {}
let index = 1
collection.getFilteredByGlob("src/pages/**/*.md")
.sort() // sort by whatever attribute, I used URL
.forEach(c => {
pageIndex[c.data.page.url] = index
index++
})
})
// pageIndex output
{
'/': 1,
'/a/': 2,
'/a/subpage/': 3,
'/s/': 4
}
Then use this collection to get the count in pages.11tydata.js
:
module.exports = {
eleventyComputed: {
eleventyNavigation: (data) => {
let order = 0
const indexCollection = data.collections.pageIndex
if (indexCollection)
{
const pageIndex = indexCollection[data.page.url]
if (pageIndex) order = pageIndex.order
}
return {
// key, title, etc
order,
}
},
Links #
- Eleventy Docs
- andy-piccalilli/11ty-base: Eleventy base project
- List of Supported Languages in Prism
- reeseschultz/11r: America's favorite Eleventy blog template.
- patrickxchong/eleventy-plugin-svg-sprite: A high performance Eleventy universal plugin that compiles a directory of SVG files into a single SVG Sprite and adds shortcodes to embed SVG Sprite and SVG content in Eleventy templates.
- Learn Eleventy From Scratch - Piccalilli
- philhawksworth/eleventyone: A scaffold for a quick start building with the Eleventy SSG
- I Finally Understand Eleventy's Data Cascade.
- maxboeck/eleventastic: An Eleventy Starter Kit
- Learn how to display a directory treeview with your 11ty syntax highlighter
- Add a Sitemap to Eleventy - 11ty Recipes
- Eleventy/11ty Recipes
- charisrooda/eleventy-data-starter: This is a starter template if you want to work with JSON data.
- A Set of Useful 11ty Filters | Aleksandr Hovhannisyan
- 11ty/eleventy-base-blog: A starter repository for a blog web site using the Eleventy static site generator.
- Organizing the Eleventy config file | Lene Saile
- Build a Blogroll with Eleventy | Ben Myers
- Automatically generate open graph images in Eleventy | bnijenhuis