11ty / eleventy-base-blog

A starter repository for a blog web site using the Eleventy static site generator.
https://eleventy-base-blog.netlify.app/
MIT License
1.2k stars 609 forks source link

Inconsistency between documentation and implementation for "draft" feature #175

Open thoresson opened 3 months ago

thoresson commented 3 months ago

Since I'm new both to JS and Eleventy, it might be that I'm missing something. But when I couldn't get drafts working as I expected, I dived into the draft functions and found that there is only checks for wether draft is defined in the frontmatter or not. According to the docs, "draft: true" should exclude drafts from the builds, but any post where draft is defined is actually omitted.

This gave me the behaviour I was expecting.

        if(data.draft == true && !process.env.BUILD_DRAFTS) {
            return true;
        }

Sorry if I'm missing something obvious here! :)

ashleyross commented 1 week ago

The original code if(data.draft && !process.env.BUILD_DRAFTS) uses data.draft in a boolean context, which means that Javascript evaluates whether or not its value is truthy, that is, anything that is not falsy.

Using YAML front matter, the following values for draft are falsy:

draft: false  # boolean false
draft: 0      # numeric zero
draft: ""     # empty string
draft: null   # no value

Just about everything else is truthy, including:

draft: true     # boolean true
draft: 1        # non-zero number
draft: -1       # non-zero number
draft: "true"   # non-empty string
draft: "false"  # non-empty string
draft: "no"     # non-empty string
draft: no       # non-empty string (In YAML, this is the same as the line before)

Maybe double-check that draft isn't set to something that is being treated as a string.