Using filters in front matter #3691
-
Notice the ---
eleventyComputed:
title: Category Archive for "{{ tag | formatCategoryTag }}"
layout: blog-posts.njk
pagination:
data: collections
size: 1
alias: tag
filter:
- all
- blog
- blog-archives
- blog-page
- blog-posts
- contact-page
- homepage
permalink: /blog/category/{{ tag | formatCategoryTag }}/
robots: noindex
--- Can it not reference such in my eleventy.config.mjs?: export default config => {
config.addFilter('formatCategoryTag', tag => tag.replace('blog-category-', ''));
// return { ... };
}; If I must use config.addGlobalData('formatCategoryTag', () => {
return tag => tag.replace('blog-category-', '');
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I don't think you can use filters in frontmatter (except for permalinks). |
Beta Was this translation helpful? Give feedback.
-
Thanks, @dwkns. I renamed the file from category.njk to category.11ty.mjs and rewrote the YAML as JavaScript: /**
* Generate a page of blog posts for each blog category (tag).
*/
export const data = {
eleventyComputed: {
permalink: ({ tag }) => `/blog/category/${formatTag(tag)}/`,
title: ({ tag }) => `Category Archive for "${formatTag(tag)}"`,
},
layout: 'blog-posts.njk',
pagination: {
alias: 'tag', // Provided to `eleventyComputed` and `layout`
/**
* @param {{ data: { tags?: string[] } }[]} posts
* @returns {string[]}
*/
before: (posts) => [
...(posts.reduce((result, post) => {
post.data.tags?.forEach((tag) => {
if (tag.startsWith(TAG_PREFIX)) {
result.add(tag);
}
});
return result;
}, new Set()) ?? []),
],
data: 'collections.blog-post',
size: 1, // No actual "pagination"
},
robots: ['follow', 'noindex'],
tags: ['blog-category'], // Provided to `layout`
};
/**
* Removes the technically necessary prefix from a blog category tag name.
* @param {string} tag
*/
const formatTag = (tag) => tag.replace(TAG_PREFIX, '');
const TAG_PREFIX = 'blog-category-'; so my category tags such as blog-category-miscellaneous become miscellaneous in the URLs. |
Beta Was this translation helpful? Give feedback.
I don't think you can use filters in frontmatter (except for permalinks).
Could you use JavaScript frontmatter instead?