Skip to content

Commit

Permalink
Generate draft assets in draft mode
Browse files Browse the repository at this point in the history
Since hexojs#3489, no draft assets are generated. This is the desired behavior when not rendering draft posts. But when working on drafts, the according assets are never rendered. This change checks if draft posts should be rendered, and if this is the case, the assets will not be deleted.

I validated this fix by

- Running `hexo clean && rm -rf public/ && hexo generate` - Draft assets are not generated (as before)
- Running `hexo clean && rm -rf public/ && hexo generate --draft` - Draft assets are generated
- Running `hexo server --draft` - Draft assets are generated
- Adjusting the existing unit test

Fixes: hexojs#4556
  • Loading branch information
darekkay committed Mar 15, 2021
1 parent 2acded6 commit 006abfa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/plugins/processor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ module.exports = ctx => {
]));
}

function shouldSkipDraftAsset(post, asset) {
if (asset) {
if (post.published === false) asset.remove(); // delete if already exist
return true;
} else if (post.published === false) return true; // skip assets for unpulished posts and
return false;
}

function scanAssetDir(post) {
if (!ctx.config.post_asset_folder) return;

Expand All @@ -164,8 +172,9 @@ module.exports = ctx => {
const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/');
const asset = PostAsset.findById(id);

if (asset) return post.published === false ? asset.remove() : undefined; // delete if already exist
else if (post.published === false) return undefined; // skip assets for unpulished posts and
if (!ctx._showDrafts()) {
if (shouldSkipDraftAsset(post, asset)) return undefined;
}

return PostAsset.save({
_id: id,
Expand All @@ -192,8 +201,7 @@ module.exports = ctx => {

// TODO: Better post searching
const post = Post.toArray().find(post => file.source.startsWith(post.asset_dir));

if (post != null && post.published) {
if (post != null && (post.published || ctx._showDrafts())) {
return PostAsset.save({
_id: id,
slug: file.source.substring(post.asset_dir.length),
Expand Down
11 changes: 11 additions & 0 deletions test/scripts/processors/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,11 +959,22 @@ describe('post', () => {
writeFile(file.source, body),
writeFile(assetPath, '')
]);

// drafts disabled - no draft assets should be generated
await process(file);
const post = Post.findOne({ source: file.path });

post.published.should.be.false;
should.not.exist(PostAsset.findById(assetId));

// drafts enabled - all assets should be generated
hexo.config.render_drafts = true;
await process(file);

should.exist(PostAsset.findById(assetId));

hexo.config.render_drafts = false;

post.remove();

await Promise.all([
Expand Down

0 comments on commit 006abfa

Please sign in to comment.