diff --git a/lib/plugins/processor/post.js b/lib/plugins/processor/post.js index 7336025bbb..f3e50546ef 100644 --- a/lib/plugins/processor/post.js +++ b/lib/plugins/processor/post.js @@ -159,7 +159,9 @@ module.exports = ctx => { }).filter(item => !isExcludedFile(item, ctx.config)).map(item => { const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/'); const asset = PostAsset.findById(id); - if (asset) return undefined; + + 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 return PostAsset.save({ _id: id, @@ -187,7 +189,7 @@ module.exports = ctx => { // TODO: Better post searching const post = Post.toArray().find(post => file.source.startsWith(post.asset_dir)); - if (post != null) { + if (post != null && post.published) { return PostAsset.save({ _id: id, slug: file.source.substring(post.asset_dir.length), diff --git a/test/scripts/processors/post.js b/test/scripts/processors/post.js index 07cacaa210..d37a96c70e 100644 --- a/test/scripts/processors/post.js +++ b/test/scripts/processors/post.js @@ -826,6 +826,42 @@ describe('post', () => { ]); }); + it('post - post_asset_folder enabled with unpublished posts', async () => { + hexo.config.post_asset_folder = true; + + const body = [ + 'title: "Hello world"', + 'published: false', + '---' + ].join('\n'); + + const file = newFile({ + path: 'foo.html', + published: true, + type: 'create', + renderable: true + }); + + const assetId = 'source/_posts/foo/bar.jpg'; + const assetPath = join(hexo.base_dir, assetId); + + await Promise.all([ + writeFile(file.source, body), + writeFile(assetPath, '') + ]); + await process(file); + const post = Post.findOne({ source: file.path }); + + post.published.should.be.false; + should.not.exist(PostAsset.findById(assetId)); + post.remove(); + + await Promise.all([ + unlink(file.source), + unlink(assetPath) + ]); + }); + it('post - post_asset_folder disabled', async () => { hexo.config.post_asset_folder = false;