-
Notifications
You must be signed in to change notification settings - Fork 182
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
Hide draft posts from /build_production, but include in /build_local #221
Comments
Hmm, there's not a built-in way to specifically exclude a collection item from a particular environment. One possible approach, which would involve a change to Jigsaw, would be extending the events feature (#189) to add a new |
If a collection is being used here, wouldn't it be possible to remove the collection items in the Just an idea - haven't tested it - but since |
@hellocosmin thanks, this seems to be the right direction. So far I've got in $events->afterCollections(function ($jigsaw) {
// are we in production?
if ($jigsaw->getConfig("production") == false) {
return;
}
// removing draft posts from the posts collection
$publicPosts = $jigsaw->getCollection("posts")->filter(function ($post) {
return $post->draft == false && $post->status !== "draft";
});
$jigsaw->setCollection("posts", $publicPosts);
}); The But the last line is not working, due to @damiani maybe a new feature? ;-) Another idea would be to transform the collection directly with $jigsaw->getCollection("posts")->transform(function ($post) {
if ( $post->draft == false && $post->status !== "draft") {
unset($post);
}
}); But the specific |
Just been testing this, cause I need something similar, and this appears to work: $events->afterCollections(function ($jigsaw) {
if ($jigsaw->getConfig("production") == false) {
return;
}
$jigsaw->getCollection('posts')->each(function ($post, $key) use ($jigsaw) {
if ($post->publish !== true) {
$jigsaw->getCollection('blog')->forget($key);
}
});
}); Tested on my site, and without this, the posts collection has 7 entires, and with it, it has 6 entires (I have one post that is set to However, this does seem to generate this message (strangely not every time, but most) Notice: Trying to get property of non-object in vendor/tightenco/jigsaw/src/Handlers/MarkdownHandler.php on line 4 Though it doesn't appear to have any effect on the output, still needs looking into further. |
Cool, your solution works in altering the collection. Thank you. $events->afterCollections(function ($jigsaw) {
// are we in production?
if ($jigsaw->getConfig("production") == false) {
return;
}
// removing draft posts from the posts collection
$jigsaw->getCollection('posts')->each(function ($post, $key) use ($jigsaw) {
if ($post->draft == true or $post->status == "draft") {
$jigsaw->getCollection('posts')->forget($key);
}
});
}); But that gives me the same notice too: I think, altering the collection after it is build is a dead end, as the removal of a single item does not alter the reference from the remaining items to the removed items. This only creates links to non existing items. I think @damiani's approach is more feasible as to not consider rejected files when building the collection. |
Turns out this is pretty complicated using events, for exactly the reasons you've run into—for collection items, drafts would need to be removed from both the collection item array and the built files; those are processed at different times during a build, so a single I'm wondering now if there's a better way to solve this need. Similar to what Jekyll does, we could allow users to store draft collection items in a
...then, we could add a
If |
Agreed, the more I think about it, this makes the most sense from the user side.
'collections' => [
'people' => [
'drafts' => true,
],
'posts' => [
'drafts' => false,
],
], |
+1 for the solution proposed by @damiani. My only issue with this is that, just like with I've had to do a workaround for this in all my Jigsaw projects. If you ever need to send args to Create a PHP script that gets the merged config, based on build environment: // tasks/php/config
#!/usr/bin/env php
<?php
/*
|--------------------------------------------------------------------------
| Site Config
|--------------------------------------------------------------------------
|
| Return the environment-specific site configuration as JSON.
| This is called in `tasks/build.js`, to store the config in a var
| allowing us to pass arguments otherwise unavailable
| in AfterWebpack(), to `jigsaw build`.
|
*/
$env = getopt('e:');
$config_dev = require_once('config.php');
if($env) {
$file = 'config.' . $env['e'] . '.php';
if(file_exists($file)) {
$config_env = require_once($file);
$out = array_merge($config_dev, $config_env);
print json_encode($out);
exit();
}
}
print json_encode($config_dev);
exit(); Get that config in // tasks/build.js
let execSync = require('child_process').execSync;
let config = JSON.parse(execSync('php ./tasks/php/config -e' + env));
// We now have the (merged) config in that variable, so we can do this for drafts and pretty URLs:
jigsaw: new AfterWebpack(() => {
let prettyURLs = config.pretty == false ? ' --pretty=false ' : '';
let buildDrafts = config.buildDrafts == true ? ' --drafts=true ' : '';
command.get(bin.path() + ' build ' + env + prettyURLs + buildDrafts, (error, stdout, stderr) => {
...
});
}); Of course, the above assumes you have a Hope this helps - looking forward to this feature :) |
I'd be happy to take up this issue and submit a PR. But, I am new to open source, it would be helpful if someone could point me in the right direction. Thanks 🙂 |
I am also looking for some ability to have drafts within a blog. As noted in #285 the idea of "drafts" is very specific to blogs, so not necessarily something that belongs in the core of Jigsaw. I prefer keeping my drafts in the same folder as the rest of my content and using the
The first two parts ensure the item is not added to the site data for the collection. The third item keeps the |
@camuthig I like this solution, would you PR it? |
Thanks for the great solution! |
Is there a way to hide/exclude a .md file/post from the posts collection when compiling to build_production, but have it included in build_local?
The YAML of the post.md file would be:
I'm struggling with the collections helper methods. Thanks for the help.
[Proposal] Maybe add a "How to" section to the docs?
The text was updated successfully, but these errors were encountered: