chore: move collection definitions out of .eleventy.js

This commit is contained in:
Cory Dransfeldt 2023-12-17 13:54:38 -08:00
parent 48defb4dbe
commit d347016b1d
No known key found for this signature in database
3 changed files with 47 additions and 40 deletions

View file

@ -0,0 +1,42 @@
const tagAliases = require('../data/tag-aliases.json')
const tagList = (collection) => {
const tagsSet = new Set()
collection.getAll().forEach((item) => {
if (!item.data.tags) return
item.data.tags
.filter((tag) => !['posts', 'all'].includes(tag))
.forEach((tag) => tagsSet.add(tag))
})
return Array.from(tagsSet).sort()
}
const tagMap = (collection) => {
const tags = {}
collection.getAll().forEach((item) => {
if (item.data.collections.posts) {
item.data.collections.posts.forEach((post) => {
const url = post.url.includes('http') ? post.url : `https://coryd.dev${post.url}`
const tagString = [...new Set(post.data.tags.map((tag) => tagAliases[tag.toLowerCase()]))]
.join(' ')
.trim()
if (tagString) tags[url] = tagString
})
}
if (item.data.links) {
item.data.links.forEach((link) => {
const tagString = link['tags']
.map((tag) => tagAliases[tag.toLowerCase()])
.join(' ')
.trim()
if (tagString) tags[link.url] = tagString
})
}
})
return tags
}
module.exports = {
tagList,
tagMap,
}