add drafts functionality

This commit is contained in:
madrilene 2024-07-21 11:49:54 +02:00
parent e0b6131921
commit 84b0ea9d92
4 changed files with 41 additions and 5 deletions

View file

@ -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);

View file

@ -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

View file

@ -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;
}
});
};

View file

@ -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;