Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a draft mode #2060

Closed
pbowyer opened this issue Oct 31, 2021 · 9 comments
Closed

Add a draft mode #2060

pbowyer opened this issue Oct 31, 2021 · 9 comments

Comments

@pbowyer
Copy link

pbowyer commented Oct 31, 2021

Is your feature request related to a problem? Please describe.
Say I've got 10-15 drafts. I want them rendered locally but I don't want the file to exist in production

Describe the solution you'd like
I'd like a built-in draft mode.

Describe alternatives you've considered
I've added a setting to the front matter to exclude the files from my sitemap.xml and I comment them out of the navigation - but the draft is still generated as a page in the output directory

@pdehaan
Copy link
Contributor

pdehaan commented Oct 31, 2021

@pbowyer I know you're asking for a built-in mode, but for future travellers, I believe the current solution is a combination of eleventyExcludeFromCollections: true and permalink: false to prevent draft pages from being written.

@pbowyer
Copy link
Author

pbowyer commented Oct 31, 2021

@pdehaan Never thought of permalink: false - that may be the trick I've missed!

@pdehaan
Copy link
Contributor

pdehaan commented Oct 31, 2021

Ah, apologies. I assumed you were just asking for an EASIER way to do draft modes (which I also agree with).

https://www.11ty.dev/docs/permalinks/#permalink-false is a good place to start reading. And I recall a few really good discussions in the issues and forums about it. I'll see if I can dig any up.

@pdehaan
Copy link
Contributor

pdehaan commented Oct 31, 2021

Probably a bunch more interesting threads if you search issues/discussions for permalink.

@pdehaan
Copy link
Contributor

pdehaan commented Oct 31, 2021

In fact, here was my solution.

// ./src/src.11tydata.js
module.exports = {
  eleventyComputed: {
    permalink(data) {
      // If the page is in `draft:true` mode, don't write it to disk...
      if (data.draft) {
        return false;
      }
      // Return the original value (which could be `false`, or a custom value,
      // or default empty string).
      return data.permalink;
    },
    eleventyExcludeFromCollections(data) {
      // If the page is in `draft:true` mode, or has `permalink:false` exclude
      // it from any collections since it shouldn't be visible anywhere.
      if (data.draft || data.permalink === false) {
        return true;
      }
      return data.eleventyExcludeFromCollections;
    }
  }
};

Posted my sample repo at https://github.com/pdehaan/11ty-drafts

@pracplayopen
Copy link

pracplayopen commented Jun 2, 2022

initially also tried to use draft:true, obv didn't work.

per this issue tried to use permalink: false

page didn't render but still showed in collections, so generated a 404 (also seems this is expected)

then tried to add various combinations of following (to module.exports and to global .eleventy.js file) to fix latter, w/ no success :

eleventyExcludeFromCollections: true;
eleventyExcludeFromCollections = true,

perhaps where to add this is implicit based on some other resource. (tried eleventyComputed suggestion from OP but copy/paste w/bracket-count check didn't parse properly).

since everybody that has a site creates drafts, this is probably something that could be simplified further imho.
(hate trying to play javascript-magic-guess-game, bc hard to remember/maintain those things)

@pracplayopen
Copy link

after a lot of looking, this is clealy being held off in eleventy.

whether this is reason or not, fairly easy to do:

mv news/post ../drafts/  # or news/some-post.html

benefits:

  • avoids understanding any subtleties (draft-vs-post)
  • no need to redefine collections based on tags
  • still version controlled :) :)

@zachleat
Copy link
Member

zachleat commented Jun 15, 2022

I think you can improve on that approach using the Configuration API for ignores. I like that folks can decide what convention they want to use for drafts (the example below uses a _ prefix and a .md suffix).

module.exports = function(eleventyConfig) {
  if(process.env.CI) {
    eleventyConfig.ignores.add("**/_*.md");
  }
};

In this example process.env.CI is set by your CI server, e.g. https://docs.netlify.com/configure-builds/environment-variables/#netlify-configuration-variables

@zachleat
Copy link
Member

Let’s consider #188 the home base for this, please subscribe over there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants