1
0
Fork 0
blog/eleventy.config.drafts.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

2025-01-01 22:06:51 +00:00
export function eleventyComputedPermalink() {
2025-05-03 03:37:23 +00:00
// When using `addGlobalData` and you *want* to return a function, you must nest functions like this.
// `addGlobalData` acts like a global data file and runs the top level function it receives.
return (data) => {
// Always skip during non-watch/serve builds
if (data.draft && !process.env.BUILD_DRAFTS) {
return false;
}
2024-07-01 17:43:44 +00:00
2025-05-03 03:37:23 +00:00
return data.permalink;
};
}
2024-07-01 17:43:44 +00:00
2025-01-01 22:06:51 +00:00
export function eleventyComputedExcludeFromCollections() {
2025-05-03 03:37:23 +00:00
// When using `addGlobalData` and you *want* to return a function, you must nest functions like this.
// `addGlobalData` acts like a global data file and runs the top level function it receives.
return (data) => {
// Always exclude from non-watch/serve builds
if (data.draft && !process.env.BUILD_DRAFTS) {
return true;
}
2024-07-01 17:43:44 +00:00
2025-05-03 03:37:23 +00:00
return data.eleventyExcludeFromCollections;
};
}
2024-07-01 17:43:44 +00:00
2025-01-01 22:06:51 +00:00
export default function (eleventyConfig) {
2025-05-03 03:37:23 +00:00
eleventyConfig.addGlobalData(
"eleventyComputed.permalink",
eleventyComputedPermalink,
);
eleventyConfig.addGlobalData(
"eleventyComputed.eleventyExcludeFromCollections",
eleventyComputedExcludeFromCollections,
);
2024-07-01 17:43:44 +00:00
2025-05-03 03:37:23 +00:00
let logged = false;
eleventyConfig.on("eleventy.before", ({ runMode }) => {
let text = "Excluding";
// Only show drafts in serve/watch modes
if (runMode === "serve" || runMode === "watch") {
process.env.BUILD_DRAFTS = true;
text = "Including";
}
2024-07-01 17:43:44 +00:00
2025-05-03 03:37:23 +00:00
// Only log once.
if (!logged) {
console.log(`[11ty/eleventy-base-blog] ${text} drafts.`);
}
2024-07-01 17:43:44 +00:00
2025-05-03 03:37:23 +00:00
logged = true;
});
2024-07-01 17:43:44 +00:00
}