Added Category shortcodes
- added a shortcode that creates the categories (used for navigation & category-focused page-creation) - added a shortcode that creates collections based on categories listed in articles
This commit is contained in:
parent
94f0c3e5e7
commit
396c4739a0
|
@ -20,6 +20,8 @@ module.exports = function(eleventyConfig) {
|
||||||
eleventyConfig.addCollection("blog", (collectionApi) => {
|
eleventyConfig.addCollection("blog", (collectionApi) => {
|
||||||
return collectionApi.getFilteredByGlob('./src/blog/*.md').reverse()
|
return collectionApi.getFilteredByGlob('./src/blog/*.md').reverse()
|
||||||
})
|
})
|
||||||
|
eleventyConfig.addCollection('categoryList', require('./src/_11ty/getCategoryList'))
|
||||||
|
eleventyConfig.addCollection('categories', require('./src/_11ty/createCategories'))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dir: {
|
dir: {
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
const slugify = require('slugify')
|
||||||
|
|
||||||
|
module.exports = (collectionApi) => {
|
||||||
|
const categories = {}
|
||||||
|
|
||||||
|
collectionApi.getFilteredByGlob('./src/blog/*.md').forEach(item => {
|
||||||
|
let category = item.data.category
|
||||||
|
|
||||||
|
// Ignore the ones without a category
|
||||||
|
if (typeof category !== 'string')
|
||||||
|
return
|
||||||
|
|
||||||
|
const slug = strToSlug(category)
|
||||||
|
|
||||||
|
if (Array.isArray(categories[slug])) {
|
||||||
|
categories[slug].push(item)
|
||||||
|
} else {
|
||||||
|
categories[slug] = [item]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return categories
|
||||||
|
}
|
||||||
|
|
||||||
|
function strToSlug(str) {
|
||||||
|
const options = {
|
||||||
|
replacement: '-',
|
||||||
|
remove: /[&,+()$~%.'':*?<>{}]/g,
|
||||||
|
lower: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return slugify(str, options);
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
const slugify = require('slugify')
|
||||||
|
|
||||||
|
module.exports = (collectionApi) => {
|
||||||
|
const catPages = []
|
||||||
|
let categories = []
|
||||||
|
const blogPosts = collectionApi.getFilteredByGlob('./src/blog/*.md')
|
||||||
|
|
||||||
|
blogPosts.map((item) => {
|
||||||
|
let category = item.data.category
|
||||||
|
categories.push(category)
|
||||||
|
})
|
||||||
|
|
||||||
|
categories = categories.sort(sortAlphabetically)
|
||||||
|
let temp = [...new Set(categories)]
|
||||||
|
|
||||||
|
temp.forEach((category) => {
|
||||||
|
let slug = strToSlug(category);
|
||||||
|
|
||||||
|
if(slug !== 'in-the-spotlight') {
|
||||||
|
catPages.push({
|
||||||
|
'key': slug,
|
||||||
|
'name': category
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return catPages
|
||||||
|
}
|
||||||
|
|
||||||
|
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' });
|
||||||
|
}
|
Loading…
Reference in New Issue