diff --git a/eleventy.config.drafts.js b/eleventy.config.drafts.js new file mode 100644 index 0000000..2de748f --- /dev/null +++ b/eleventy.config.drafts.js @@ -0,0 +1,51 @@ +function eleventyComputedPermalink() { + // 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; + } + + return data.permalink; + } +}; + +function eleventyComputedExcludeFromCollections() { + // 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; + } + + return data.eleventyExcludeFromCollections; + } +}; + +module.exports.eleventyComputedPermalink = eleventyComputedPermalink; +module.exports.eleventyComputedExcludeFromCollections = eleventyComputedExcludeFromCollections; + +module.exports = eleventyConfig => { + eleventyConfig.addGlobalData("eleventyComputed.permalink", eleventyComputedPermalink); + eleventyConfig.addGlobalData("eleventyComputed.eleventyExcludeFromCollections", eleventyComputedExcludeFromCollections); + + let logged = false; + eleventyConfig.on("eleventy.before", ({runMode}) => { + let text = "Excluding"; + + if(process.env.BUILD_DRAFTS == "true") { + process.env.BUILD_DRAFTS = true; + text = "Including"; + } + + // Only log once. + if(!logged) { + console.log(process.env.BUILD_DRAFTS, typeof(process.env.BUILD_DRAFTS)); + console.log( `[11ty/drafts] ${text} drafts.` ); + } + + logged = true; + }); +} diff --git a/eleventy.config.js b/eleventy.config.js index 04325ea..b679c51 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -5,6 +5,7 @@ const mdDefList = require("markdown-it-deflist"); const mdToc = require("markdown-it-table-of-contents"); const mdAnchor = require("markdown-it-anchor"); const eleventyRss = require("@11ty/eleventy-plugin-rss"); +const eleventyDrafts = require("./eleventy.config.drafts"); module.exports = function (eleventyConfig) { eleventyConfig.setBrowserSyncConfig({ @@ -21,6 +22,7 @@ module.exports = function (eleventyConfig) { eleventyConfig.addPlugin(eleventyNavigationPlugin); eleventyConfig.addPlugin(eleventySyntaxHighlight); eleventyConfig.addPlugin(eleventyRss); + eleventyConfig.addPlugin(eleventyDrafts); eleventyConfig.addPlugin(eleventySass, { sass: { loadPaths: ["node_modules"], @@ -55,19 +57,6 @@ module.exports = function (eleventyConfig) { eleventyConfig.addFilter("orderBySectionOrder", (collection) => collection.sort((a, b) => a.data.sectionOrder - b.data.sectionOrder) ); - eleventyConfig.addFilter("filterDrafts", collection => { - if (process.env.BUILD_DRAFTS == true || process.env.BUILD_DRAFTS == null) { - return collection; - } - - return collection.filter(item => { - if (item.data.draft != null) { - return !item.data.draft; - } - - return true; - }); - }); eleventyConfig.addFilter("log", (value) => console.log(value)); return { diff --git a/src/_includes/layouts/layout.njk b/src/_includes/layouts/layout.njk index 590d40c..0f73623 100644 --- a/src/_includes/layouts/layout.njk +++ b/src/_includes/layouts/layout.njk @@ -24,7 +24,7 @@