diff --git a/eleventy.config.js b/eleventy.config.js index 7d866b0..d780aec 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -45,6 +45,7 @@ export default async function (eleventyConfig) { eleventyConfig.addPlugin(plugins.htmlConfig); eleventyConfig.addPlugin(plugins.cssConfig); eleventyConfig.addPlugin(plugins.jsConfig); + eleventyConfig.addPlugin(plugins.drafts); eleventyConfig.addPlugin(plugins.EleventyRenderPlugin); eleventyConfig.addPlugin(plugins.rss); diff --git a/src/_config/plugins.js b/src/_config/plugins.js index a74642c..cfa4006 100644 --- a/src/_config/plugins.js +++ b/src/_config/plugins.js @@ -6,6 +6,7 @@ import webc from '@11ty/eleventy-plugin-webc'; // custom import {markdownLib} from './plugins/markdown.js'; +import {drafts} from './plugins/drafts.js'; // Custom transforms import {htmlConfig} from './plugins/html-config.js'; @@ -20,6 +21,7 @@ export default { syntaxHighlight, webc, markdownLib, + drafts, htmlConfig, cssConfig, jsConfig diff --git a/src/_config/plugins/drafts.js b/src/_config/plugins/drafts.js new file mode 100644 index 0000000..05eec4a --- /dev/null +++ b/src/_config/plugins/drafts.js @@ -0,0 +1,30 @@ +export const drafts = eleventyConfig => { + eleventyConfig.addGlobalData('eleventyComputed.permalink', function () { + return data => { + // Always skip during non-watch/serve builds + if (data.draft && !process.env.BUILD_DRAFTS) { + return false; // Ensure templates that use this handle it correctly + } + return data.permalink; + }; + }); + + // When `eleventyExcludeFromCollections` is true, the file is not included in any collections + eleventyConfig.addGlobalData('eleventyComputed.eleventyExcludeFromCollections', function () { + return data => { + // Always exclude from non-watch/serve builds + if (data.draft && !process.env.BUILD_DRAFTS) { + return true; + } + + return data.eleventyExcludeFromCollections ?? false; + }; + }); + + eleventyConfig.on('eleventy.before', ({runMode}) => { + // Set the environment variable + if (runMode === 'serve' || runMode === 'watch') { + process.env.BUILD_DRAFTS = true; + } + }); +}; diff --git a/src/_data/helpers.js b/src/_data/helpers.js index 0c2fcd3..a6893bd 100644 --- a/src/_data/helpers.js +++ b/src/_data/helpers.js @@ -9,12 +9,15 @@ export function getLinkActiveState(itemUrl, pageUrl) { let response = ''; - if (itemUrl === pageUrl) { - response = ' aria-current="page"'; - } + // Ensure pageUrl is a string before proceeding + if (typeof pageUrl === 'string') { + if (itemUrl === pageUrl) { + response = ' aria-current="page"'; + } - if (itemUrl.length > 1 && pageUrl.indexOf(itemUrl.replace('/page-0/', '')) === 0) { - response += 'data-state="active"'; + if (itemUrl.length > 1 && pageUrl.startsWith(itemUrl.replace('/page-0/', ''))) { + response += ' data-state="active"'; + } } return response;