Use a better method of excluding drafts

This commit is contained in:
Neil Brommer 2023-07-13 10:23:04 -07:00
parent 326a240b8c
commit 0c0ab839d6
7 changed files with 60 additions and 20 deletions

51
eleventy.config.drafts.js Normal file
View file

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

View file

@ -5,6 +5,7 @@ const mdDefList = require("markdown-it-deflist");
const mdToc = require("markdown-it-table-of-contents"); const mdToc = require("markdown-it-table-of-contents");
const mdAnchor = require("markdown-it-anchor"); const mdAnchor = require("markdown-it-anchor");
const eleventyRss = require("@11ty/eleventy-plugin-rss"); const eleventyRss = require("@11ty/eleventy-plugin-rss");
const eleventyDrafts = require("./eleventy.config.drafts");
module.exports = function (eleventyConfig) { module.exports = function (eleventyConfig) {
eleventyConfig.setBrowserSyncConfig({ eleventyConfig.setBrowserSyncConfig({
@ -21,6 +22,7 @@ module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyNavigationPlugin); eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.addPlugin(eleventySyntaxHighlight); eleventyConfig.addPlugin(eleventySyntaxHighlight);
eleventyConfig.addPlugin(eleventyRss); eleventyConfig.addPlugin(eleventyRss);
eleventyConfig.addPlugin(eleventyDrafts);
eleventyConfig.addPlugin(eleventySass, { eleventyConfig.addPlugin(eleventySass, {
sass: { sass: {
loadPaths: ["node_modules"], loadPaths: ["node_modules"],
@ -55,19 +57,6 @@ module.exports = function (eleventyConfig) {
eleventyConfig.addFilter("orderBySectionOrder", (collection) => eleventyConfig.addFilter("orderBySectionOrder", (collection) =>
collection.sort((a, b) => a.data.sectionOrder - b.data.sectionOrder) 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)); eleventyConfig.addFilter("log", (value) => console.log(value));
return { return {

View file

@ -24,7 +24,7 @@
<header> <header>
<nav class="navbar" aria-label="Main Menu"> <nav class="navbar" aria-label="Main Menu">
<ul class="siteNav"> <ul class="siteNav">
{% for section in collections.MainPage | filterDrafts | eleventyNavigation %} {% for section in collections.MainPage | eleventyNavigation %}
<li {% if section.url == page.url %}class="active"{% endif %}> <li {% if section.url == page.url %}class="active"{% endif %}>
<a href="/#{{ section.title | slugify }}"> <a href="/#{{ section.title | slugify }}">
{% if section.icon is defined %} {% if section.icon is defined %}
@ -37,7 +37,7 @@
</li> </li>
{% endfor %} {% endfor %}
{% set standalonePages = collections.all | filterDrafts | IsNotMainPageSection | eleventyNavigation %} {% set standalonePages = collections.all | IsNotMainPageSection | eleventyNavigation %}
{% if standalonePages | length %} {% if standalonePages | length %}

View file

@ -1,5 +1,5 @@
{% if postsList | filterDrafts | IsNotPage(page.url) | length %} {% if postsList | length %}
{% for post in postsList | filterDrafts | reverse %} {% for post in postsList | reverse %}
<section class="posts-list-post"> <section class="posts-list-post">
<h2><a href={{ post.url }}>{{ post.data.title }}</a></h2> <h2><a href={{ post.url }}>{{ post.data.title }}</a></h2>
<dl class="postMetadata"> <dl class="postMetadata">

View file

@ -19,7 +19,7 @@ metadata:
<author> <author>
<name>{{ metadata.author.name }}</name> <name>{{ metadata.author.name }}</name>
</author> </author>
{%- for post in collections.posts | filterDrafts | IsNotPage("/posts/") | reverse %} {%- for post in collections.posts | reverse %}
{%- set absolutePostUrl = post.url | absoluteUrl(metadata.url) %} {%- set absolutePostUrl = post.url | absoluteUrl(metadata.url) %}
<entry> <entry>
<title>{{ post.data.title }}</title> <title>{{ post.data.title }}</title>

View file

@ -7,7 +7,7 @@ eleventyNavigation:
tags: [ "MainPage" ] tags: [ "MainPage" ]
--- ---
{% for section in collections.MainPage | filterDrafts | IsNotPage(page.url) | IsMainPageSection | orderBySectionOrder %} {% for section in collections.MainPage | IsNotPage(page.url) | IsMainPageSection | orderBySectionOrder %}
{% if not loop.first %} {% if not loop.first %}
<h2 id="{{ section.data.title | slugify }}">{{ section.data.title }}</h2> <h2 id="{{ section.data.title | slugify }}">{{ section.data.title }}</h2>
{% endif %} {% endif %}

View file

@ -7,5 +7,5 @@ eleventyNavigation:
order: 1 order: 1
--- ---
{% set postsList = collections.posts | filterDrafts | IsNotPage(page.url) %} {% set postsList = collections.posts %}
{% include "posts-list.njk" %} {% include "posts-list.njk" %}