diff --git a/lib/plugins/processor/post.ts b/lib/plugins/processor/post.ts index e433858084..9c61a561b8 100644 --- a/lib/plugins/processor/post.ts +++ b/lib/plugins/processor/post.ts @@ -217,7 +217,9 @@ function scanAssetDir(ctx, post) { const assetDir = post.asset_dir; const baseDir = ctx.base_dir; + const sourceDir = ctx.config.source_dir; const baseDirLength = baseDir.length; + const sourceDirLength = sourceDir.length; const PostAsset = ctx.model('PostAsset'); return stat(assetDir).then(stats => { @@ -229,6 +231,7 @@ function scanAssetDir(ctx, post) { throw err; }).filter(item => !isExcludedFile(item, ctx.config)).map(item => { const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/'); + const renderablePath = id.substring(sourceDirLength + 1); const asset = PostAsset.findById(id); if (shouldSkipAsset(ctx, post, asset)) return undefined; @@ -237,7 +240,8 @@ function scanAssetDir(ctx, post) { _id: id, post: post._id, slug: item, - modified: true + modified: true, + renderable: ctx.render.isRenderable(renderablePath) && !isMatch(renderablePath, ctx.config.skip_render) }); }); } diff --git a/test/scripts/processors/post.js b/test/scripts/processors/post.js index 1beb68207f..fea2563d62 100644 --- a/test/scripts/processors/post.js +++ b/test/scripts/processors/post.js @@ -1256,4 +1256,76 @@ describe('post', () => { unlink(file.source) ]); }); + + it('asset - post - common render', async () => { + hexo.config.post_asset_folder = true; + + const file = newFile({ + path: 'foo.md', + published: true, + type: 'create', + renderable: true + }); + + const assetFile = newFile({ + path: 'foo/test.yml', + published: true, + type: 'create' + }); + + await Promise.all([ + writeFile(file.source, 'test'), + writeFile(assetFile.source, 'test') + ]); + await process(file); + const id = 'source/' + assetFile.path; + const post = Post.findOne({ source: file.path }); + PostAsset.findById(id).renderable.should.be.true; + + hexo.config.post_asset_folder = false; + + return Promise.all([ + unlink(file.source), + unlink(assetFile.source), + post.remove(), + PostAsset.removeById(id) + ]); + }); + + it('asset - post - skip render', async () => { + hexo.config.post_asset_folder = true; + hexo.config.skip_render = '**.yml'; + + const file = newFile({ + path: 'foo.md', + published: true, + type: 'create', + renderable: true + }); + + const assetFile = newFile({ + path: 'foo/test.yml', + published: true, + type: 'create' + }); + + await Promise.all([ + writeFile(file.source, 'test'), + writeFile(assetFile.source, 'test') + ]); + await process(file); + const id = 'source/' + assetFile.path; + const post = Post.findOne({ source: file.path }); + PostAsset.findById(id).renderable.should.be.false; + + hexo.config.post_asset_folder = false; + hexo.config.skip_render = ''; + + return Promise.all([ + unlink(file.source), + unlink(assetFile.source), + post.remove(), + PostAsset.removeById(id) + ]); + }); });