Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(full_url_for): bring up a new helper (#84)
* feat(full_url_for): bring up a new helper
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const { parse } = require('url'); | ||
const encodeURL = require('./encode_url'); | ||
|
||
function fullUrlForHelper(path = '/') { | ||
if (path.startsWith('//')) return path; | ||
const { config } = this; | ||
const data = parse(path); | ||
|
||
// Exit if this is an external path | ||
if (data.protocol) return path; | ||
|
||
path = encodeURL(config.url + `/${path}`.replace(/\/{2,}/g, '/')); | ||
return path; | ||
} | ||
|
||
module.exports = fullUrlForHelper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
describe('full_url_for', () => { | ||
const ctx = { | ||
config: {} | ||
}; | ||
|
||
const fullUrlFor = require('../lib/full_url_for').bind(ctx); | ||
|
||
it('internal url - root directory', () => { | ||
ctx.config.url = 'https://example.com'; | ||
fullUrlFor('index.html').should.eql(ctx.config.url + '/index.html'); | ||
fullUrlFor('/').should.eql(ctx.config.url + '/'); | ||
}); | ||
|
||
it('internal url - subdirectory', () => { | ||
ctx.config.url = 'https://example.com/blog'; | ||
fullUrlFor('index.html').should.eql(ctx.config.url + '/index.html'); | ||
fullUrlFor('/').should.eql(ctx.config.url + '/'); | ||
}); | ||
|
||
it('internal url - no duplicate slash', () => { | ||
ctx.config.url = 'https://example.com'; | ||
fullUrlFor('/index.html').should.eql('https://example.com/index.html'); | ||
}); | ||
|
||
it('external url', () => { | ||
[ | ||
'https://hexo.io/', | ||
'//google.com/' | ||
].forEach(url => { | ||
fullUrlFor(url).should.eql(url); | ||
}); | ||
}); | ||
|
||
it('only hash', () => { | ||
ctx.config.url = 'https://example.com/blog'; | ||
fullUrlFor('#test').should.eql(ctx.config.url + '/#test'); | ||
}); | ||
}); |