From 4c2cdf1746d0bf6b275eab6be52473fd17bf1f11 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Mon, 23 Jan 2023 16:35:14 -0600 Subject: [PATCH] Adds sample `drafts` implementation --- content/blog/blog.11tydata.js | 36 +++++++++++++++++++++++++++++++++++ content/blog/blog.json | 6 ------ content/blog/fifthpost.md | 6 ++++++ eleventy.config.js | 27 ++++++++++++++++++++------ 4 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 content/blog/blog.11tydata.js delete mode 100644 content/blog/blog.json create mode 100644 content/blog/fifthpost.md diff --git a/content/blog/blog.11tydata.js b/content/blog/blog.11tydata.js new file mode 100644 index 0000000..7657d51 --- /dev/null +++ b/content/blog/blog.11tydata.js @@ -0,0 +1,36 @@ +module.exports = { + tags: [ + "posts" + ], + "layout": "layouts/post.njk", + + // Draft posts: + eleventyComputed: { + permalink: data => { + if(data.draft) { + // BUILD_DRAFTS is set in eleventy.config.js + if(process.env.BUILD_DRAFTS) { + return data.permalink; + } + + // Always skip during non-watch/serve builds + return false; + } + + return data.permalink; + }, + eleventyExcludeFromCollections: data => { + if(data.draft) { + // BUILD_DRAFTS is set in eleventy.config.js + if(process.env.BUILD_DRAFTS) { + return data.eleventyExcludeFromCollections; + } + + // Always exclude from non-watch/serve builds + return true; + } + + return data.eleventyExcludeFromCollections; + } + }, +}; diff --git a/content/blog/blog.json b/content/blog/blog.json deleted file mode 100644 index 3bc5725..0000000 --- a/content/blog/blog.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "tags": [ - "posts" - ], - "layout": "layouts/post.njk" -} diff --git a/content/blog/fifthpost.md b/content/blog/fifthpost.md new file mode 100644 index 0000000..b173821 --- /dev/null +++ b/content/blog/fifthpost.md @@ -0,0 +1,6 @@ +--- +title: This is a fifth post (draft) +date: 2023-01-23 +draft: true +--- +This is a draft post diff --git a/eleventy.config.js b/eleventy.config.js index 303da60..f23785d 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -8,13 +8,22 @@ const pluginNavigation = require("@11ty/eleventy-navigation"); const eleventyImage = require("@11ty/eleventy-img"); const { EleventyHtmlBasePlugin } = require("@11ty/eleventy"); -function relativeToInputPath(inputPath, relativeFilePath) { - let split = inputPath.split(path.sep); - split.pop(); - return path.resolve(split.join(path.sep), relativeFilePath); -} - module.exports = function(eleventyConfig) { + eleventyConfig.addPlugin(function enableDrafts(eleventyConfig) { + let logged = false; + eleventyConfig.on("eleventy.before", ({runMode}) => { + // only show drafts in serve/watch modes + if(runMode === "serve" || runMode === "watch") { + process.env.BUILD_DRAFTS = true; + if(!logged) { + console.log( "[11ty/eleventy-base-blog] including `draft: true` posts" ); + } + + logged = true; + } + }); + }) + // Copy the contents of the `public` folder to the output folder // For example, `./public/css/` ends up in `_site/css/` eleventyConfig.addPassthroughCopy({ @@ -38,6 +47,12 @@ module.exports = function(eleventyConfig) { // Eleventy Image shortcode // https://www.11ty.dev/docs/plugins/image/ eleventyConfig.addPlugin(eleventyConfig => { + function relativeToInputPath(inputPath, relativeFilePath) { + let split = inputPath.split(path.sep); + split.pop(); + return path.resolve(split.join(path.sep), relativeFilePath); + } + eleventyConfig.addAsyncShortcode("image", async function imageShortcode(src, alt, sizes) { let file = relativeToInputPath(this.page.inputPath, src); let metadata = await eleventyImage(file, {