Skip to content

Commit 4552552

Browse files
authored
fix(url_for): absolute path is processed by relative_url (#262)
* fix(url_for): absolute path is processed by relative_url * test: absolute urls are processed by relative_url
1 parent b19468d commit 4552552

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/url_for.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const Cache = require('./cache');
99
const cache = new Cache();
1010

1111
function urlForHelper(path = '/', options) {
12+
if (/^(#|\/\/|http(s)?:)/.test(path)) return path;
13+
1214
const { config } = this;
1315

1416
options = Object.assign({
@@ -28,8 +30,6 @@ function urlForHelper(path = '/', options) {
2830

2931
// cacheId is designed to works across different hexo.config & options
3032
return cache.apply(`${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
31-
if (/^(#|\/\/|http(s)?:)/.test(path)) return path;
32-
3333
const sitehost = parse(config.url).hostname || config.url;
3434
const data = new URL(path, `http://${sitehost}`);
3535

test/url_for.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ describe('url_for', () => {
128128
});
129129
});
130130

131+
it('absolute url - should not be processed by relative_url', () => {
132+
ctx.config.relative_link = true;
133+
[
134+
'https://hexo.io/',
135+
'//google.com/',
136+
'https://hexo.io/docs/index.html',
137+
'http://example.com/foo/bar/',
138+
'https://example.com/foo/bar/'
139+
].forEach(url => {
140+
urlFor(url).should.eql(url);
141+
});
142+
ctx.config.relative_link = false;
143+
});
144+
131145
it('only hash', () => {
132146
urlFor('#test').should.eql('#test');
133147
});

0 commit comments

Comments
 (0)