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); 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',