fix: exclude draft posts from all Eleventy collections

Posts with `draft: true` frontmatter were included in every collection
(posts, notes, articles, feed, recentPosts, categories, etc.), making
them visible on the blog, homepage, RSS feed, and sidebar. Added an
isPublished filter to all 12 collections.
This commit is contained in:
Ricardo
2026-02-22 11:04:49 +01:00
parent 4fc5701290
commit f8aae34c01
+15 -4
View File
@@ -500,42 +500,51 @@ export default function (eleventyConfig) {
return `<svg viewBox="0 0 ${w} ${h}" width="${w}" height="${h}" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Posting frequency over the last 12 months"><polyline fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" points="${points}"/></svg>`; return `<svg viewBox="0 0 ${w} ${h}" width="${w}" height="${h}" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Posting frequency over the last 12 months"><polyline fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" points="${points}"/></svg>`;
}); });
// Helper: exclude drafts from collections
const isPublished = (item) => !item.data.draft;
// Collections for different post types // Collections for different post types
// Note: content path is content/ due to symlink structure // Note: content path is content/ due to symlink structure
// "posts" shows ALL content types combined // "posts" shows ALL content types combined
eleventyConfig.addCollection("posts", function (collectionApi) { eleventyConfig.addCollection("posts", function (collectionApi) {
return collectionApi return collectionApi
.getFilteredByGlob("content/**/*.md") .getFilteredByGlob("content/**/*.md")
.filter(isPublished)
.sort((a, b) => b.date - a.date); .sort((a, b) => b.date - a.date);
}); });
eleventyConfig.addCollection("notes", function (collectionApi) { eleventyConfig.addCollection("notes", function (collectionApi) {
return collectionApi return collectionApi
.getFilteredByGlob("content/notes/**/*.md") .getFilteredByGlob("content/notes/**/*.md")
.filter(isPublished)
.sort((a, b) => b.date - a.date); .sort((a, b) => b.date - a.date);
}); });
eleventyConfig.addCollection("articles", function (collectionApi) { eleventyConfig.addCollection("articles", function (collectionApi) {
return collectionApi return collectionApi
.getFilteredByGlob("content/articles/**/*.md") .getFilteredByGlob("content/articles/**/*.md")
.filter(isPublished)
.sort((a, b) => b.date - a.date); .sort((a, b) => b.date - a.date);
}); });
eleventyConfig.addCollection("bookmarks", function (collectionApi) { eleventyConfig.addCollection("bookmarks", function (collectionApi) {
return collectionApi return collectionApi
.getFilteredByGlob("content/bookmarks/**/*.md") .getFilteredByGlob("content/bookmarks/**/*.md")
.filter(isPublished)
.sort((a, b) => b.date - a.date); .sort((a, b) => b.date - a.date);
}); });
eleventyConfig.addCollection("photos", function (collectionApi) { eleventyConfig.addCollection("photos", function (collectionApi) {
return collectionApi return collectionApi
.getFilteredByGlob("content/photos/**/*.md") .getFilteredByGlob("content/photos/**/*.md")
.filter(isPublished)
.sort((a, b) => b.date - a.date); .sort((a, b) => b.date - a.date);
}); });
eleventyConfig.addCollection("likes", function (collectionApi) { eleventyConfig.addCollection("likes", function (collectionApi) {
return collectionApi return collectionApi
.getFilteredByGlob("content/likes/**/*.md") .getFilteredByGlob("content/likes/**/*.md")
.filter(isPublished)
.sort((a, b) => b.date - a.date); .sort((a, b) => b.date - a.date);
}); });
@@ -544,7 +553,7 @@ export default function (eleventyConfig) {
eleventyConfig.addCollection("replies", function (collectionApi) { eleventyConfig.addCollection("replies", function (collectionApi) {
return collectionApi return collectionApi
.getAll() .getAll()
.filter((item) => item.data.inReplyTo || item.data.in_reply_to) .filter((item) => isPublished(item) && (item.data.inReplyTo || item.data.in_reply_to))
.sort((a, b) => b.date - a.date); .sort((a, b) => b.date - a.date);
}); });
@@ -553,7 +562,7 @@ export default function (eleventyConfig) {
eleventyConfig.addCollection("reposts", function (collectionApi) { eleventyConfig.addCollection("reposts", function (collectionApi) {
return collectionApi return collectionApi
.getAll() .getAll()
.filter((item) => item.data.repostOf || item.data.repost_of) .filter((item) => isPublished(item) && (item.data.repostOf || item.data.repost_of))
.sort((a, b) => b.date - a.date); .sort((a, b) => b.date - a.date);
}); });
@@ -564,7 +573,7 @@ export default function (eleventyConfig) {
const rootPages = collectionApi.getFilteredByGlob("content/*.md"); const rootPages = collectionApi.getFilteredByGlob("content/*.md");
const pagesDir = collectionApi.getFilteredByGlob("content/pages/*.md"); const pagesDir = collectionApi.getFilteredByGlob("content/pages/*.md");
return [...rootPages, ...pagesDir] return [...rootPages, ...pagesDir]
.filter(page => !page.inputPath.includes('content.json') && !page.inputPath.includes('pages.json')) .filter(page => isPublished(page) && !page.inputPath.includes('content.json') && !page.inputPath.includes('pages.json'))
.sort((a, b) => (a.data.title || a.data.name || "").localeCompare(b.data.title || b.data.name || "")); .sort((a, b) => (a.data.title || a.data.name || "").localeCompare(b.data.title || b.data.name || ""));
}); });
@@ -572,6 +581,7 @@ export default function (eleventyConfig) {
eleventyConfig.addCollection("feed", function (collectionApi) { eleventyConfig.addCollection("feed", function (collectionApi) {
return collectionApi return collectionApi
.getFilteredByGlob("content/**/*.md") .getFilteredByGlob("content/**/*.md")
.filter(isPublished)
.sort((a, b) => b.date - a.date) .sort((a, b) => b.date - a.date)
.slice(0, 20); .slice(0, 20);
}); });
@@ -581,7 +591,7 @@ export default function (eleventyConfig) {
const categoryMap = new Map(); // slug -> original name (first seen) const categoryMap = new Map(); // slug -> original name (first seen)
const slugify = (str) => str.toLowerCase().replace(/[^\w\s-]/g, "").replace(/[\s_-]+/g, "-").replace(/^-+|-+$/g, ""); const slugify = (str) => str.toLowerCase().replace(/[^\w\s-]/g, "").replace(/[\s_-]+/g, "-").replace(/^-+|-+$/g, "");
collectionApi.getAll().forEach((item) => { collectionApi.getAll().filter(isPublished).forEach((item) => {
if (item.data.category) { if (item.data.category) {
const cats = Array.isArray(item.data.category) ? item.data.category : [item.data.category]; const cats = Array.isArray(item.data.category) ? item.data.category : [item.data.category];
cats.forEach((cat) => { cats.forEach((cat) => {
@@ -601,6 +611,7 @@ export default function (eleventyConfig) {
eleventyConfig.addCollection("recentPosts", function (collectionApi) { eleventyConfig.addCollection("recentPosts", function (collectionApi) {
return collectionApi return collectionApi
.getFilteredByGlob("content/**/*.md") .getFilteredByGlob("content/**/*.md")
.filter(isPublished)
.sort((a, b) => b.date - a.date) .sort((a, b) => b.date - a.date)
.slice(0, 5); .slice(0, 5);
}); });