feat: made tags nestable
This commit is contained in:
parent
2f5bf48b42
commit
dc8eb631b6
1 changed files with 25 additions and 9 deletions
|
|
@ -41,6 +41,19 @@ const extractTags = (content, mdInstance) => {
|
||||||
return [...new Set(tags)];
|
return [...new Set(tags)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const expandHierarchicalTags = (tags) => {
|
||||||
|
const expanded = new Set();
|
||||||
|
tags.forEach(tag => {
|
||||||
|
expanded.add(tag);
|
||||||
|
const parts = tag.split('/');
|
||||||
|
for (let i = 1; i < parts.length; i++) {
|
||||||
|
const parentTag = parts.slice(0, i).join('/');
|
||||||
|
expanded.add(parentTag);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return [...expanded];
|
||||||
|
};
|
||||||
|
|
||||||
const getPostTags = (post, tagMdInstance) => {
|
const getPostTags = (post, tagMdInstance) => {
|
||||||
const filePath = post.inputPath;
|
const filePath = post.inputPath;
|
||||||
try {
|
try {
|
||||||
|
|
@ -275,7 +288,8 @@ module.exports = (eleventyConfig) => {
|
||||||
const tagMap = {};
|
const tagMap = {};
|
||||||
|
|
||||||
posts.forEach(post => {
|
posts.forEach(post => {
|
||||||
const tags = getPostTags(post, tagExtractorMd)
|
const rawTags = getPostTags(post, tagExtractorMd);
|
||||||
|
const tags = expandHierarchicalTags(rawTags);
|
||||||
tags.forEach((tag) => {
|
tags.forEach((tag) => {
|
||||||
tagMap[tag] = {
|
tagMap[tag] = {
|
||||||
name: tag,
|
name: tag,
|
||||||
|
|
@ -286,7 +300,7 @@ module.exports = (eleventyConfig) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.values(tagMap).forEach(tagData => {
|
Object.values(tagMap).forEach(tagData => {
|
||||||
tagData.posts.sort((a, b) => {
|
tagData.posts = [...new Set(tagData.posts)].sort((a, b) => {
|
||||||
const aDate = a.data.createdAt || a.date;
|
const aDate = a.data.createdAt || a.date;
|
||||||
const bDate = b.data.createdAt || b.date;
|
const bDate = b.data.createdAt || b.date;
|
||||||
return aDate - bDate;
|
return aDate - bDate;
|
||||||
|
|
@ -349,7 +363,9 @@ module.exports = (eleventyConfig) => {
|
||||||
const postTags = posts.flatMap(post => getPostTags(post, tagExtractorMd));
|
const postTags = posts.flatMap(post => getPostTags(post, tagExtractorMd));
|
||||||
const recipeTags = recipes.flatMap(recipe => getRecipeTags(recipe));
|
const recipeTags = recipes.flatMap(recipe => getRecipeTags(recipe));
|
||||||
|
|
||||||
return [...new Set([...postTags, ...recipeTags])].sort();
|
const allTags = expandHierarchicalTags([...postTags, ...recipeTags]);
|
||||||
|
|
||||||
|
return [...new Set(allTags)].sort();
|
||||||
});
|
});
|
||||||
|
|
||||||
eleventyConfig.addCollection("contentByTag", (collectionApi) => {
|
eleventyConfig.addCollection("contentByTag", (collectionApi) => {
|
||||||
|
|
@ -363,9 +379,9 @@ module.exports = (eleventyConfig) => {
|
||||||
return aDate - bDate;
|
return aDate - bDate;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Build tag map from posts
|
|
||||||
const postTagMap = posts.reduce((acc, post) => {
|
const postTagMap = posts.reduce((acc, post) => {
|
||||||
const tags = getPostTags(post, tagExtractorMd);
|
const rawTags = getPostTags(post, tagExtractorMd);
|
||||||
|
const tags = expandHierarchicalTags(rawTags);
|
||||||
return tags.reduce((innerAcc, tag) => ({
|
return tags.reduce((innerAcc, tag) => ({
|
||||||
...innerAcc,
|
...innerAcc,
|
||||||
[tag]: {
|
[tag]: {
|
||||||
|
|
@ -376,9 +392,9 @@ module.exports = (eleventyConfig) => {
|
||||||
}), acc);
|
}), acc);
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
// Merge recipe tags into the tag map
|
|
||||||
const tagMap = recipes.reduce((acc, recipe) => {
|
const tagMap = recipes.reduce((acc, recipe) => {
|
||||||
const tags = getRecipeTags(recipe);
|
const rawTags = getRecipeTags(recipe);
|
||||||
|
const tags = expandHierarchicalTags(rawTags);
|
||||||
return tags.reduce((innerAcc, tag) => ({
|
return tags.reduce((innerAcc, tag) => ({
|
||||||
...innerAcc,
|
...innerAcc,
|
||||||
[tag]: {
|
[tag]: {
|
||||||
|
|
@ -389,12 +405,12 @@ module.exports = (eleventyConfig) => {
|
||||||
}), acc);
|
}), acc);
|
||||||
}, postTagMap);
|
}, postTagMap);
|
||||||
|
|
||||||
// Return with sorted posts
|
|
||||||
return Object.entries(tagMap).reduce((acc, [tag, tagData]) => ({
|
return Object.entries(tagMap).reduce((acc, [tag, tagData]) => ({
|
||||||
...acc,
|
...acc,
|
||||||
[tag]: {
|
[tag]: {
|
||||||
...tagData,
|
...tagData,
|
||||||
posts: [...tagData.posts].sort(sortByDate),
|
posts: [...new Set(tagData.posts)].sort(sortByDate),
|
||||||
|
recipes: [...new Set(tagData.recipes)],
|
||||||
},
|
},
|
||||||
}), {});
|
}), {});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue