Skip to content

Commit

Permalink
Merge pull request #3 containing cleanup eleventy config & updated de…
Browse files Browse the repository at this point in the history
…pendencies
  • Loading branch information
mangamaui authored Mar 3, 2023
2 parents 7f32520 + 528c7f7 commit 5c61263
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 133 deletions.
72 changes: 51 additions & 21 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,73 @@
const eleventyNavigationPlugin = require('@11ty/eleventy-navigation')
const pluginRss = require('@11ty/eleventy-plugin-rss')

const { DateTime } = require('luxon')
const {
getAllPosts,
getCategoryList,
getCategorisedPosts
} = require('./config/collections')

const {
readableDate
} = require('./config/filters')

const {
imageShortcode
} = require('./config/shortcodes')


module.exports = function(eleventyConfig) {

/*================================*/
/* plugins and configurations */
/*================================*/
eleventyConfig.addPlugin(eleventyNavigationPlugin)
eleventyConfig.addPlugin(pluginRss)

eleventyConfig.addLayoutAlias('page', 'layouts/page')
eleventyConfig.addLayoutAlias('article', 'layouts/article')
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
excerpt_separator: "<!-- excerpt -->",
excerpt_alias: 'excerpt'
})

/*===================================================*/
/* files that need to be copied to the build folder */
/*===================================================*/
eleventyConfig.addPassthroughCopy('./src/assets/social-image.jpg')
eleventyConfig.addPassthroughCopy('./src/assets/icons')
eleventyConfig.addPassthroughCopy('./src/assets/sprite.svg')
eleventyConfig.addPassthroughCopy({
'node_modules/svg-icon-sprite/dist/svg-icon-sprite.js': 'assets/svg-icon-sprite.js'
})
eleventyConfig.addPassthroughCopy('./src/assets/social-image.jpg')

eleventyConfig.addNunjucksAsyncShortcode('image', require('./src/_11ty/imageShortcode').imageShortcode)

eleventyConfig.addFilter('readableDate', dateObj => {
return DateTime.fromJSDate(dateObj, {
zone: 'Europe/Paris',
}).setLocale('en').toLocaleString(DateTime.DATE_FULL)
})
/*=================*/
/* Layouts */
/*=================*/
eleventyConfig.addLayoutAlias('page', 'layouts/page')
eleventyConfig.addLayoutAlias('article', 'layouts/article')

/* Creating a collection of blogposts by filtering based on folder and filetype */
eleventyConfig.addCollection('blog', (collectionApi) => {
return collectionApi.getFilteredByGlob('./src/blog/*.md').reverse()
})
eleventyConfig.addCollection('categoryList', require('./src/_11ty/getCategoryList'))
eleventyConfig.addCollection('categories', require('./src/_11ty/createCategories'))

/*=================*/
/* Collections */
/*=================*/
eleventyConfig.addCollection('blog', getAllPosts)
eleventyConfig.addCollection('categoryList', getCategoryList)
eleventyConfig.addCollection('categorisedPosts', getCategorisedPosts)


/*=================*/
/* Filters */
/*=================*/
eleventyConfig.addFilter('readableDate', readableDate)


/*=================*/
/* shortcodes */
/*=================*/
eleventyConfig.addNunjucksAsyncShortcode('image', imageShortcode)


eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
excerpt_separator: "<!-- excerpt -->",
excerpt_alias: 'excerpt'
})

return {
dir: {
Expand Down
77 changes: 77 additions & 0 deletions config/collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const slugify = require('slugify')

/* Creating a collection containing all blogposts by filtering based on folder and filetype */
const getAllPosts = (collectionApi) => {
return collectionApi.getFilteredByGlob('./src/blog/*.md')
.reverse()
}

const getCategoryList = (collectionApi) => {
const catPages = []
let categories = []
const blogPosts = collectionApi.getFilteredByGlob('./src/blog/*.md')

blogPosts.map((item) => {
categories.push(item.data.category)
})

categories = categories.sort(sortAlphabetically)
const temp = [...new Set(categories)]

temp.forEach((category) => {
const slug = strToSlug(category);

if(slug !== 'in-the-spotlight') {
catPages.push({
'key': slug,
'name': category
})
}
})

return catPages
}

const getCategorisedPosts = (collectionApi) => {
const categorisedPosts = {}

collectionApi.getFilteredByGlob('./src/blog/*.md').forEach(item => {
const category = item.data.category

// Ignore the ones without a category
if (typeof category !== 'string')
return

const slug = strToSlug(category)

if (Array.isArray(categorisedPosts[slug])) {
categorisedPosts[slug].push(item)
} else {
categorisedPosts[slug] = [item]
}
})

return categorisedPosts
}

module.exports = {
getAllPosts,
getCategoryList,
getCategorisedPosts
}


function strToSlug(str) {
const options = {
replacement: "-",
remove: /[&,+()$~%.'":*?<>{}]/g,
lower: true,
}

return slugify(str, options)
}


function sortAlphabetically(a, b) {
return a.localeCompare(b, "en", { sensitivity: "base" })
}
11 changes: 11 additions & 0 deletions config/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { DateTime } = require('luxon')

const readableDate = (dateObj) => {
return DateTime.fromJSDate(dateObj, {
zone: 'Europe/Paris',
}).setLocale('en').toLocaleString(DateTime.DATE_FULL)
}

module.exports = {
readableDate
}
30 changes: 30 additions & 0 deletions config/shortcodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Image = require('@11ty/eleventy-img')

const imageShortcode = async (imageObj = {}) => {
const widths = imageObj.widths || [300, 600, 900, 1200]
const className = imageObj.className || "image"

const sizes = "(min-width: 100px) 50vw, 100vw"
const metadata = await Image(imageObj.src, {
formats: ["webp"],
outputDir: "./_site/assets/images/generated/",
urlPath: "/assets/images/generated/",
widths: widths
})
const alt = imageObj.alt;

const imageAttributes = {
class: className,
alt,
sizes,
loading: "lazy",
decoding: "async",
}

return Image.generateHTML(metadata, imageAttributes)
}


module.exports = {
imageShortcode
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
},
"license": "MIT",
"dependencies": {
"@11ty/eleventy": "^1.0.2",
"@11ty/eleventy-img": "^2.0.1",
"@11ty/eleventy": "^2.0.0",
"@11ty/eleventy-img": "^3.0.0",
"@11ty/eleventy-navigation": "^0.3.5",
"@11ty/eleventy-plugin-rss": "^1.2.0",
"luxon": "^3.2.1",
"npm-run-all": "^4.1.5",
"sass": "^1.57.1",
"sass": "^1.58.3",
"svg-icon-sprite": "^1.1.1"
}
}
33 changes: 0 additions & 33 deletions src/_11ty/createCategories.js

This file was deleted.

43 changes: 0 additions & 43 deletions src/_11ty/getCategoryList.js

This file was deleted.

31 changes: 0 additions & 31 deletions src/_11ty/imageShortcode.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/blog/article1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ category: "cat1"
Having landed on this Eleventy starter, you probably are no stranger to Eleventy. But if you are, you should definitely check out its benefits. Getting started with Eleventy is quick and easy.
<!-- excerpt -->

{% image "https://images.unsplash.com/photo-1555066931-4365d14bab8c", "A laptop with some lines of code on the screen", "image", [300, 600] %}
{% image {src: "https://images.unsplash.com/photo-1555066931-4365d14bab8c", alt: "A laptop with some lines of code on the screen", className: "image", widths: [300, 600] } %}

2 changes: 1 addition & 1 deletion src/category.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ eleventyComputed:
<h1>{{ title }}</h1>

{% include "./_includes/components/categoryNav.njk" %}
{% set articles = collections.categories[cat.key] | reverse %}
{% set articles = collections.categorisedPosts[cat.key] | reverse %}
{% include "./_includes/components/articleList.njk" %}

0 comments on commit 5c61263

Please sign in to comment.