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: encode permalink by default #3708

Merged
merged 7 commits into from
Sep 20, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const defaultConfig = require('./default_config');
const loadDatabase = require('./load_database');
const multiConfigPath = require('./multi_config_path');
const resolve = require('resolve');
const full_url_for = require('../plugins/helper/full_url_for');

const { cloneDeep, debounce } = _;

Expand Down Expand Up @@ -285,13 +286,14 @@ function stopWatcher(box) {

Hexo.prototype._generateLocals = function() {
const { config, theme } = this;
const ctx = { config: { url: this.config.url } };

function Locals(path, locals) {
this.page = typeof locals === 'object' ? locals : {};
if (this.page.path == null) this.page.path = path;

this.path = path;
this.url = `${config.url}/${path}`;
this.url = full_url_for.call(ctx, path);
}

Locals.prototype.config = config;
Expand Down
7 changes: 4 additions & 3 deletions lib/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { Schema } = require('warehouse');
const { slugize } = require('hexo-util');
const full_url_for = require('../plugins/helper/full_url_for');

module.exports = ctx => {
const Category = new Schema({
Expand Down Expand Up @@ -39,9 +40,9 @@ module.exports = ctx => {

Category.virtual('permalink').get(function() {
const { config } = ctx;
let partial_url = this.path;
if (config.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, '');
return `${ctx.config.url}/${partial_url}`;
let url = full_url_for.call(ctx, this.path);
if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, '');
return url;
});

Category.virtual('posts').get(function() {
Expand Down
7 changes: 4 additions & 3 deletions lib/models/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { Schema } = require('warehouse');
const { join } = require('path');
const Moment = require('./types/moment');
const moment = require('moment');
const full_url_for = require('../plugins/helper/full_url_for');

module.exports = ctx => {
const Page = new Schema({
Expand Down Expand Up @@ -33,9 +34,9 @@ module.exports = ctx => {

Page.virtual('permalink').get(function() {
const { config } = ctx;
let partial_url = this.path;
if (config.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, '');
return `${ctx.config.url}/${partial_url}`;
let url = full_url_for.call(ctx, this.path);
if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, '');
return url;
});

Page.virtual('full_source').get(function() {
Expand Down
9 changes: 4 additions & 5 deletions lib/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const moment = require('moment');
const { extname, join, sep } = require('path');
const Promise = require('bluebird');
const Moment = require('./types/moment');
const full_url_for = require('../plugins/helper/full_url_for');

function pickID(data) {
return data._id;
Expand Down Expand Up @@ -50,12 +51,10 @@ module.exports = ctx => {
});

Post.virtual('permalink').get(function() {
const self = Object.assign({}, ctx.extend.helper.list(), ctx);
const { config } = ctx;
let partial_url = self.url_for(this.path);
if (config.relative_link) partial_url = `/${partial_url}`;
if (config.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, '');
return config.url + partial_url.replace(config.root, '/');
let url = full_url_for.call(ctx, this.path);
if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, '');
return url;
});

Post.virtual('full_source').get(function() {
Expand Down
7 changes: 4 additions & 3 deletions lib/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { Schema } = require('warehouse');
const { slugize } = require('hexo-util');
const { hasOwnProperty: hasOwn } = Object.prototype;
const full_url_for = require('../plugins/helper/full_url_for');

module.exports = ctx => {
const Tag = new Schema({
Expand Down Expand Up @@ -30,9 +31,9 @@ module.exports = ctx => {

Tag.virtual('permalink').get(function() {
const { config } = ctx;
let partial_url = this.path;
if (config.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, '');
return `${ctx.config.url}/${partial_url}`;
let url = full_url_for.call(ctx, this.path);
if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, '');
return url;
});

Tag.virtual('posts').get(function() {
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/url_for.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const url = require('url');
const relative_url = require('./relative_url');

function urlForHelper(path = '/', options) {
if (path[0] === '#' || path.startsWith('//')) {
Expand All @@ -22,7 +23,7 @@ function urlForHelper(path = '/', options) {

// Resolve relative url
if (options.relative) {
return this.relative_url(this.path, path);
return relative_url(this.path, path);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fix is already present in https://github.com/hexojs/hexo/pull/3710/files#diff-d89aa048c56f2709ea59155d3f91aa81 , I put this as not to depend #3710

}

// Prepend root path
Expand Down
14 changes: 14 additions & 0 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,20 @@ describe('Hexo', () => {
].join('\n')));
});

it('_generate() - should encode url', () => {
hexo.config.url = 'http://fôo.com';

hexo.theme.setView('test.swig', '{{ url }}');

hexo.extend.generator.register('test', () => ({
path: 'bár',
layout: 'test'
}));

return hexo._generate().then(() => checkStream(route.get('bár'),
'http://xn--fo-8ja.com/b%C3%A1r'));
});

it('_generate() - do nothing if it\'s generating', () => {
const spy = sinon.spy();
hexo.extend.generator.register('test', spy);
Expand Down
11 changes: 11 additions & 0 deletions test/scripts/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ describe('Category', () => {
});
});

it('permalink - should be encoded', () => {
hexo.config.url = 'http://fôo.com';
return Category.insert({
name: 'bár'
}).then(data => {
data.permalink.should.eql('http://xn--fo-8ja.com/' + data.path);
hexo.config.url = 'http://yoursite.com';
return Category.removeById(data._id);
});
});

it('posts - virtual', () => Post.insert([
{source: 'foo.md', slug: 'foo'},
{source: 'bar.md', slug: 'bar'},
Expand Down
12 changes: 12 additions & 0 deletions test/scripts/models/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ describe('Page', () => {
});
});

it('permalink - should be encoded', () => {
hexo.config.url = 'http://fôo.com';
return Page.insert({
source: 'foo',
path: 'bár'
}).then(data => {
data.permalink.should.eql('http://xn--fo-8ja.com/b%C3%A1r');
hexo.config.url = 'http://yoursite.com';
return Page.removeById(data._id);
});
});

it('full_source - virtual', () => Page.insert({
source: 'foo',
path: 'bar'
Expand Down
13 changes: 13 additions & 0 deletions test/scripts/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ describe('Post', () => {
});
});

it('permalink - should be encoded', () => {
hexo.config.url = 'http://fôo.com';
return Post.insert({
source: 'foo.md',
slug: 'bár'
}).then(data => {
data.permalink.should.eql('http://xn--fo-8ja.com/b%C3%A1r');
hexo.config.url = 'http://yoursite.com';
return Post.removeById(data._id);
});
});

it('permalink - virtual - when set relative_link', () => {
hexo.config.root = '/';
hexo.config.relative_link = true;
Expand All @@ -92,6 +104,7 @@ describe('Post', () => {
slug: 'bar'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path);
hexo.config.relative_link = false;
return Post.removeById(data._id);
});
});
Expand Down
11 changes: 11 additions & 0 deletions test/scripts/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ describe('Tag', () => {
});
});

it('permalink - should be encoded', () => {
hexo.config.url = 'http://fôo.com';
return Tag.insert({
name: 'bár'
}).then(data => {
data.permalink.should.eql('http://xn--fo-8ja.com/' + data.path);
hexo.config.url = 'http://yoursite.com';
return Tag.removeById(data._id);
});
});

it('posts - virtual', () => Post.insert([
{source: 'foo.md', slug: 'foo'},
{source: 'bar.md', slug: 'bar'},
Expand Down