Skip to content

Commit 18d459f

Browse files
authored
Merge pull request #3710 from curbengh/encode-url-helper
fix(helpers, tag plugins): encode url by default
2 parents e1167b7 + 382b0de commit 18d459f

File tree

14 files changed

+89
-70
lines changed

14 files changed

+89
-70
lines changed

lib/plugins/helper/relative_url.js

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
11
'use strict';
22

3-
function relativeUrlHelper(from = '', to = '') {
4-
const fromParts = from.split('/');
5-
const toParts = to.split('/');
6-
const length = Math.min(fromParts.length, toParts.length);
7-
let i = 0;
3+
const { relative_url } = require('hexo-util');
84

9-
for (; i < length; i++) {
10-
if (fromParts[i] !== toParts[i]) break;
11-
}
12-
13-
let out = toParts.slice(i);
14-
15-
for (let j = fromParts.length - i - 1; j > 0; j--) {
16-
out.unshift('..');
17-
}
18-
19-
const outLength = out.length;
20-
21-
// If the last 2 elements of `out` is empty strings, replace them with `index.html`.
22-
if (outLength > 1 && !out[outLength - 1] && !out[outLength - 2]) {
23-
out = out.slice(0, outLength - 2).concat('index.html');
24-
}
25-
26-
return out.join('/').replace(/\/{2,}/g, '/');
27-
}
28-
29-
module.exports = relativeUrlHelper;
5+
module.exports = function(from, to) {
6+
return relative_url(from, to);
7+
};

lib/plugins/helper/url_for.js

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
11
'use strict';
22

3-
const url = require('url');
4-
const relative_url = require('./relative_url');
3+
const { url_for } = require('hexo-util');
54

6-
function urlForHelper(path = '/', options) {
7-
if (path[0] === '#' || path.startsWith('//')) {
8-
return path;
9-
}
10-
11-
const { config } = this;
12-
const { root } = config;
13-
const data = url.parse(path);
14-
15-
options = Object.assign({
16-
relative: config.relative_link
17-
}, options);
18-
19-
// Exit if this is an external path
20-
if (data.protocol) {
21-
return path;
22-
}
23-
24-
// Resolve relative url
25-
if (options.relative) {
26-
return relative_url(this.path, path);
27-
}
28-
29-
// Prepend root path
30-
path = root + path;
31-
32-
return path.replace(/\/{2,}/g, '/');
33-
}
34-
35-
module.exports = urlForHelper;
5+
module.exports = function(path, options) {
6+
return url_for.call(this, path, options);
7+
};

lib/plugins/tag/asset_img.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3-
const url = require('url');
3+
const { resolve } = require('url');
44
const img = require('./img');
5+
const { encodeURL } = require('hexo-util');
56

67
/**
78
* Asset image tag
@@ -19,7 +20,7 @@ module.exports = ctx => {
1920
for (let i = 0; i < len; i++) {
2021
const asset = PostAsset.findOne({post: this._id, slug: args[i]});
2122
if (asset) {
22-
args[i] = url.resolve('/', asset.path);
23+
args[i] = encodeURL(resolve('/', asset.path));
2324
return img(ctx)(args);
2425
}
2526
}

lib/plugins/tag/asset_link.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const url = require('url');
4-
const { escapeHTML } = require('hexo-util');
3+
const { encodeURL, escapeHTML } = require('hexo-util');
4+
const { resolve } = require('url');
55

66
/**
77
* Asset link tag
@@ -30,6 +30,8 @@ module.exports = ctx => {
3030
const attrTitle = escapeHTML(title);
3131
if (escape === 'true') title = attrTitle;
3232

33-
return `<a href="${url.resolve(ctx.config.root, asset.path)}" title="${attrTitle}">${title}</a>`;
33+
const link = encodeURL(resolve(ctx.config.root, asset.path));
34+
35+
return `<a href="${link}" title="${attrTitle}">${title}</a>`;
3436
};
3537
};

lib/plugins/tag/asset_path.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
const url = require('url');
3+
const { resolve } = require('url');
4+
const { encodeURL } = require('hexo-util');
45

56
/**
67
* Asset path tag
@@ -18,6 +19,8 @@ module.exports = ctx => {
1819
const asset = PostAsset.findOne({post: this._id, slug});
1920
if (!asset) return;
2021

21-
return url.resolve(ctx.config.root, asset.path);
22+
const path = encodeURL(resolve(ctx.config.root, asset.path));
23+
24+
return path;
2225
};
2326
};

lib/plugins/tag/post_link.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
const { escapeHTML } = require('hexo-util');
3+
const { encodeURL, escapeHTML } = require('hexo-util');
4+
const { resolve } = require('url');
45

56
/**
67
* Post link tag
@@ -29,6 +30,8 @@ module.exports = ctx => {
2930
const attrTitle = escapeHTML(title);
3031
if (escape === 'true') title = attrTitle;
3132

32-
return `<a href="${ctx.config.root}${post.path}" title="${attrTitle}">${title}</a>`;
33+
const link = encodeURL(resolve(ctx.config.root, post.path));
34+
35+
return `<a href="${link}" title="${attrTitle}">${title}</a>`;
3336
};
3437
};

lib/plugins/tag/post_path.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
const { resolve } = require('url');
4+
const { encodeURL } = require('hexo-util');
5+
36
/**
47
* Post path tag
58
*
@@ -16,6 +19,8 @@ module.exports = ctx => {
1619
const post = Post.findOne({slug});
1720
if (!post) return;
1821

19-
return ctx.config.root + post.path;
22+
const link = encodeURL(resolve(ctx.config.root, post.path));
23+
24+
return link;
2025
};
2126
};

test/scripts/helpers/relative_url.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ describe('relative_url', () => {
2727
relativeURL('foo/', '/').should.eql('../index.html');
2828
relativeURL('foo/index.html', '/').should.eql('../index.html');
2929
});
30+
31+
it('should encode path', () => {
32+
relativeURL('foo/', 'css/fôo.css').should.eql('../css/f%C3%B4o.css');
33+
});
3034
});

test/scripts/helpers/url_for.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ describe('url_for', () => {
88

99
const urlFor = require('../../../lib/plugins/helper/url_for').bind(ctx);
1010

11+
it('should encode path', () => {
12+
ctx.config.root = '/';
13+
urlFor('fôo.html').should.eql('/f%C3%B4o.html');
14+
15+
ctx.config.root = '/fôo/';
16+
urlFor('bár.html').should.eql('/f%C3%B4o/b%C3%A1r.html');
17+
});
18+
1119
it('internal url (relative off)', () => {
1220
ctx.config.root = '/';
1321
urlFor('index.html').should.eql('/index.html');

test/scripts/tags/asset_img.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ describe('asset_img', () => {
2828
slug: 'bar',
2929
post: post._id
3030
}),
31+
PostAsset.insert({
32+
_id: 'bár',
33+
slug: 'bár',
34+
post: post._id
35+
}),
3136
PostAsset.insert({
3237
_id: 'spaced asset',
3338
slug: 'spaced asset',
@@ -40,6 +45,10 @@ describe('asset_img', () => {
4045
assetImg('bar').should.eql('<img src="/foo/bar" class="">');
4146
});
4247

48+
it('should encode path', () => {
49+
assetImg('bár').should.eql('<img src="/foo/b%C3%A1r" class="">');
50+
});
51+
4352
it('default', () => {
4453
assetImg('bar title').should.eql('<img src="/foo/bar" class="" title="title">');
4554
});

0 commit comments

Comments
 (0)