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

skip assets of unpublished posts and delete them if exist #3489

Merged
merged 2 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/plugins/processor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
36 changes: 36 additions & 0 deletions test/scripts/processors/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down