From ca38c7c3deae963b7c13ae4e54a8e85b5b8650fd Mon Sep 17 00:00:00 2001 From: guobaoyang Date: Mon, 24 Dec 2018 16:44:59 +0800 Subject: [PATCH 1/2] fix(asset-id): Change source config,the source assets folder can't be generated fix #3398 --- lib/plugins/processor/asset.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plugins/processor/asset.js b/lib/plugins/processor/asset.js index 29b3259204..ee39ade154 100644 --- a/lib/plugins/processor/asset.js +++ b/lib/plugins/processor/asset.js @@ -3,7 +3,7 @@ const common = require('./common'); const Promise = require('bluebird'); const yfm = require('hexo-front-matter'); -const { extname } = require('path'); +const { extname, relative } = require('path'); const { Pattern } = require('hexo-util'); module.exports = ctx => { @@ -84,7 +84,7 @@ module.exports = ctx => { } function processAsset(file) { - const id = file.source.substring(ctx.base_dir.length).replace(/\\/g, '/'); + const id = relative(ctx.base_dir, file.source).replace(/\\/g, '/'); const Asset = ctx.model('Asset'); const doc = Asset.findById(id); From 2a96c858bfb8fa2e67e613e1a4892abddc102dbd Mon Sep 17 00:00:00 2001 From: returnyang Date: Sat, 16 Mar 2019 16:44:54 +0800 Subject: [PATCH 2/2] test: add unit test for asset id --- test/scripts/processors/asset.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/scripts/processors/asset.js b/test/scripts/processors/asset.js index 03e8d955b4..3f00bec51c 100644 --- a/test/scripts/processors/asset.js +++ b/test/scripts/processors/asset.js @@ -83,7 +83,28 @@ describe('asset', () => { return asset.remove(); }).finally(() => fs.unlink(file.source)); }); + it('asset - type: create (when source path is configed to parent directory)', () => { + const file = newFile({ + path: '../../source/foo.jpg', + type: 'create', + renderable: false + }); + + return fs.writeFile(file.source, 'foo').then(() => process(file)).then(() => { + const id = '../source/foo.jpg'; // The id should a relative path,because the 'lib/models/assets.js' use asset path by joining base path with "_id" directly. + const asset = Asset.findById(id); + + asset._id.should.eql(id); + asset.path.should.eql(file.path); + asset.modified.should.be.true; + asset.renderable.should.be.false; + return asset.remove(); + }).finally(() => { + fs.unlink(file.source); + fs.rmdir(pathFn.dirname(file.source)); + }); + }); it('asset - type: update', () => { const file = newFile({ path: 'foo.jpg',