Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(url_for): absolute path is processed by relative_url #262

Merged
merged 2 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/url_for.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const Cache = require('./cache');
const cache = new Cache();

function urlForHelper(path = '/', options) {
if (/^(#|\/\/|http(s)?:)/.test(path)) return path;

const { config } = this;

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

// cacheId is designed to works across different hexo.config & options
return cache.apply(`${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
if (/^(#|\/\/|http(s)?:)/.test(path)) return path;

const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);

Expand Down
14 changes: 14 additions & 0 deletions test/url_for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ describe('url_for', () => {
});
});

it('absolute url - should not be processed by relative_url', () => {
ctx.config.relative_link = true;
[
'https://hexo.io/',
'//google.com/',
'https://hexo.io/docs/index.html',
'http://example.com/foo/bar/',
'https://example.com/foo/bar/'
].forEach(url => {
urlFor(url).should.eql(url);
});
ctx.config.relative_link = false;
});

it('only hash', () => {
urlFor('#test').should.eql('#test');
});
Expand Down