diff --git a/lib/plugins/helper/full_url_for.js b/lib/plugins/helper/full_url_for.js new file mode 100755 index 0000000000..997db74ab6 --- /dev/null +++ b/lib/plugins/helper/full_url_for.js @@ -0,0 +1,6 @@ +'use strict'; +const { full_url_for } = require('hexo-util'); + +module.exports = function(path) { + return full_url_for.call(this, path); +}; diff --git a/lib/plugins/helper/index.js b/lib/plugins/helper/index.js index e4cd830a36..7186c79195 100644 --- a/lib/plugins/helper/index.js +++ b/lib/plugins/helper/index.js @@ -70,6 +70,7 @@ module.exports = ctx => { helper.register('relative_url', require('./relative_url')); helper.register('url_for', require('./url_for')); + helper.register('full_url_for', require('./full_url_for')); const debug = require('./debug'); helper.register('inspect', debug.inspectObject); diff --git a/test/scripts/helpers/full_url_for.js b/test/scripts/helpers/full_url_for.js new file mode 100755 index 0000000000..bcd53f2de5 --- /dev/null +++ b/test/scripts/helpers/full_url_for.js @@ -0,0 +1,32 @@ +'use strict'; + +describe('full_url_for', () => { + const ctx = { + config: { url: 'https://example.com' } + }; + + const fullUrlFor = require('../../../lib/plugins/helper/full_url_for').bind(ctx); + + it('no path input', () => { + fullUrlFor().should.eql(ctx.config.url + '/'); + }); + + it('internal url', () => { + fullUrlFor('index.html').should.eql(ctx.config.url + '/index.html'); + fullUrlFor('/').should.eql(ctx.config.url + '/'); + fullUrlFor('/index.html').should.eql(ctx.config.url + '/index.html'); + }); + + it('external url', () => { + [ + 'https://hexo.io/', + '//google.com/' + ].forEach(url => { + fullUrlFor(url).should.eql(url); + }); + }); + + it('only hash', () => { + fullUrlFor('#test').should.eql(ctx.config.url + '/#test'); + }); +}); diff --git a/test/scripts/helpers/index.js b/test/scripts/helpers/index.js index f6ac31f530..96eda35326 100644 --- a/test/scripts/helpers/index.js +++ b/test/scripts/helpers/index.js @@ -7,6 +7,7 @@ describe('Helpers', () => { require('./favicon_tag'); require('./feed_tag'); require('./fragment_cache'); + require('./full_url_for'); require('./gravatar'); require('./image_tag'); require('./is');