Skip to content

Commit

Permalink
fix(helpers): call url_for from hexo-util
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jul 28, 2020
1 parent 1489074 commit 0fb3e31
Show file tree
Hide file tree
Showing 25 changed files with 39 additions and 52 deletions.
6 changes: 3 additions & 3 deletions lib/plugins/helper/css.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { htmlTag } = require('hexo-util');
const { htmlTag, url_for } = require('hexo-util');

const flatten = function(arr, result = []) {
for (const i in arr) {
Expand All @@ -24,10 +24,10 @@ function cssHelper(...args) {
if (!path.endsWith('.css')) {
path += '.css';
}
result += `<link rel="stylesheet" href="${this.url_for(path)}">\n`;
result += `<link rel="stylesheet" href="${url_for.call(this, path)}">\n`;
} else {
// New syntax
item.href = this.url_for(item.href);
item.href = url_for.call(this, item.href);
if (!item.href.endsWith('.css')) item.href += '.css';
result += htmlTag('link', { rel: 'stylesheet', ...item }) + '\n';
}
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/helper/favicon_tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const { url_for } = require('hexo-util');

function faviconTagHelper(path) {
return `<link rel="shortcut icon" href="${this.url_for(path)}">`;
return `<link rel="shortcut icon" href="${url_for.call(this, path)}">`;
}

module.exports = faviconTagHelper;
8 changes: 5 additions & 3 deletions lib/plugins/helper/feed_tag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { url_for } = require('hexo-util');

const feedFn = (str = '') => {
if (str) return str.replace(/2$/, '');
return str;
Expand All @@ -21,19 +23,19 @@ function feedTagHelper(path, options = {}) {

const typeAttr = type ? `type="application/${type}+xml"` : '';

return `<link rel="alternate" href="${this.url_for(path)}" title="${title}" ${typeAttr}>`;
return `<link rel="alternate" href="${url_for.call(this, path)}" title="${title}" ${typeAttr}>`;
}

if (config.feed) {
const { feed } = config;
if (feed.type && feed.path) {
if (typeof feed.type === 'string') {
return `<link rel="alternate" href="${this.url_for(feed.path)}" title="${title}" type="application/${feedFn(feed.type)}+xml">`;
return `<link rel="alternate" href="${url_for.call(this, feed.path)}" title="${title}" type="application/${feedFn(feed.type)}+xml">`;
}

let result = '';
for (const i in feed.type) {
result += `<link rel="alternate" href="${this.url_for(feed.path[i])}" title="${title}" type="application/${feedFn(feed.type[i])}+xml">`;
result += `<link rel="alternate" href="${url_for.call(this, feed.path[i])}" title="${title}" type="application/${feedFn(feed.type[i])}+xml">`;
}
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/helper/image_tag.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const { htmlTag } = require('hexo-util');
const { htmlTag, url_for } = require('hexo-util');

function imageTagHelper(path, options = {}) {
const attrs = Object.assign({
src: this.url_for(path)
src: url_for.call(this, path)
}, options);

if (attrs.class && Array.isArray(attrs.class)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/helper/js.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { htmlTag } = require('hexo-util');
const { htmlTag, url_for } = require('hexo-util');

/* flatten() to be replaced by Array.flat()
after Node 10 has reached EOL */
Expand All @@ -26,10 +26,10 @@ function jsHelper(...args) {
if (!path.endsWith('.js')) {
path += '.js';
}
result += `<script src="${this.url_for(path)}"></script>\n`;
result += `<script src="${url_for.call(this, path)}"></script>\n`;
} else {
// New syntax
item.src = this.url_for(item.src);
item.src = url_for.call(this, item.src);
if (!item.src.endsWith('.js')) item.src += '.js';
result += htmlTag('script', { ...item }, '') + '\n';
}
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/helper/link_to.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

const { htmlTag } = require('hexo-util');
const { htmlTag, url_for } = require('hexo-util');

function linkToHelper(path, text, options = {}) {
if (typeof options === 'boolean') options = {external: options};

if (!text) text = path.replace(/^https?:\/\/|\/$/g, '');

const attrs = Object.assign({
href: this.url_for(path),
href: url_for.call(this, path),
title: text
}, options);

Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/list_archives.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { toMomentLocale } = require('./date');
const { url_for } = require('hexo-util');

function listArchivesHelper(options = {}) {
const { config } = this;
Expand Down Expand Up @@ -61,7 +62,7 @@ function listArchivesHelper(options = {}) {
url += `${item.month}/`;
}

return this.url_for(url);
return url_for.call(this, url);
};

if (style === 'list') {
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/helper/list_categories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { url_for } = require('hexo-util');

function listCategoriesHelper(categories, options) {
if (!options && (!categories || !Object.prototype.hasOwnProperty.call(categories, 'length'))) {
options = categories;
Expand Down Expand Up @@ -57,7 +59,7 @@ function listCategoriesHelper(categories, options) {

result += `<li class="${className}-list-item${additionalClassName}">`;

result += `<a class="${className}-list-link${isCurrent ? ' current' : ''}" href="${this.url_for(cat.path)}${suffix}">`;
result += `<a class="${className}-list-link${isCurrent ? ' current' : ''}" href="${url_for.call(this, cat.path)}${suffix}">`;
result += transform ? transform(cat.name) : cat.name;
result += '</a>';

Expand All @@ -81,7 +83,7 @@ function listCategoriesHelper(categories, options) {
prepareQuery(parent).forEach((cat, i) => {
if (i || level) result += separator;

result += `<a class="${className}-link" href="${this.url_for(cat.path)}${suffix}">`;
result += `<a class="${className}-link" href="${url_for.call(this, cat.path)}${suffix}">`;
result += transform ? transform(cat.name) : cat.name;

if (showCount) {
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/helper/list_posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { url_for } = require('hexo-util');

function listPostsHelper(posts, options) {
if (!options && (!posts || !Object.prototype.hasOwnProperty.call(posts, 'length'))) {
options = posts;
Expand Down Expand Up @@ -30,7 +32,7 @@ function listPostsHelper(posts, options) {

result += `<li class="${className}-list-item">`;

result += `<a class="${className}-list-link" href="${this.url_for(post.path)}">`;
result += `<a class="${className}-list-link" href="${url_for.call(this, post.path)}">`;
result += transform ? transform(title) : title;
result += '</a>';

Expand All @@ -44,7 +46,7 @@ function listPostsHelper(posts, options) {

const title = post.title || post.slug;

result += `<a class="${className}-link" href="${this.url_for(post.path)}">`;
result += `<a class="${className}-link" href="${url_for.call(this, post.path)}">`;
result += transform ? transform(title) : title;
result += '</a>';
});
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/helper/list_tags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { url_for } = require('hexo-util');

function listTagsHelper(tags, options) {
if (!options && (!tags || !Object.prototype.hasOwnProperty.call(tags, 'length'))) {
options = tags;
Expand Down Expand Up @@ -50,7 +52,7 @@ function listTagsHelper(tags, options) {
tags.forEach(tag => {
result += `<li class="${liClass}">`;

result += `<a class="${aClass}" href="${this.url_for(tag.path)}${suffix}" rel="tag">`;
result += `<a class="${aClass}" href="${url_for.call(this, tag.path)}${suffix}" rel="tag">`;
result += transform ? transform(tag.name) : tag.name;
result += '</a>';

Expand All @@ -66,7 +68,7 @@ function listTagsHelper(tags, options) {
tags.forEach((tag, i) => {
if (i) result += separator;

result += `<a class="${aClass}" href="${this.url_for(tag.path)}${suffix}" rel="tag">`;
result += `<a class="${aClass}" href="${url_for.call(this, tag.path)}${suffix}" rel="tag">`;
result += transform ? transform(tag.name) : tag.name;

if (showCount) {
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/helper/paginator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const { htmlTag } = require('hexo-util');
const { htmlTag, url_for } = require('hexo-util');

const createLink = (options, ctx) => {
const { base, format } = options;

return i => ctx.url_for(i === 1 ? base : base + format.replace('%d', i));
return i => url_for.call(ctx, i === 1 ? base : base + format.replace('%d', i));
};

const createPageTag = (options, ctx) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/helper/tagcloud.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { Color } = require('hexo-util');
const { Color, url_for } = require('hexo-util');

function tagcloudHelper(tags, options) {
if (!options && (!tags || !Object.prototype.hasOwnProperty.call(tags, 'length'))) {
Expand Down Expand Up @@ -67,7 +67,7 @@ function tagcloudHelper(tags, options) {
}

result.push(
`<a href="${this.url_for(tag.path)}" style="${style}"${attr}>${transform ? transform(tag.name) : tag.name}</a>`
`<a href="${url_for.call(this, tag.path)}" style="${style}"${attr}>${transform ? transform(tag.name) : tag.name}</a>`
);
});

Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ describe('css', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const css = require('../../../lib/plugins/helper/css').bind(ctx);

function assertResult(result, expected) {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/favicon_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ describe('favicon_tag', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const favicon = require('../../../lib/plugins/helper/favicon_tag').bind(ctx);

it('path', () => {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/feed_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ describe('feed_tag', () => {

beforeEach(() => { ctx.config.feed = {}; });

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const feed = require('../../../lib/plugins/helper/feed_tag').bind(ctx);

it('path - atom', () => {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/image_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ describe('image_tag', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const img = require('../../../lib/plugins/helper/image_tag').bind(ctx);

it('path', () => {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ describe('js', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const js = require('../../../lib/plugins/helper/js').bind(ctx);

function assertResult(result, expected) {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/link_to.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ describe('link_to', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const linkTo = require('../../../lib/plugins/helper/link_to').bind(ctx);

it('path', () => {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/list_archives.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ describe('list_archives', () => {
page: {}
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const listArchives = require('../../../lib/plugins/helper/list_archives').bind(ctx);

function resetLocals() {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/list_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ describe('list_categories', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const listCategories = require('../../../lib/plugins/helper/list_categories').bind(ctx);

before(async () => {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/list_posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ describe('list_posts', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const listPosts = require('../../../lib/plugins/helper/list_posts').bind(ctx);

hexo.config.permalink = ':title/';
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/list_tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ describe('list_tags', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const listTags = require('../../../lib/plugins/helper/list_tags').bind(ctx);

before(async () => {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/mail_to.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ describe('mail_to', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const mailto = require('../../../lib/plugins/helper/mail_to').bind(ctx);

it('path', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/scripts/helpers/paginator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { url_for } = require('hexo-util');

describe('paginator', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(__dirname);
Expand All @@ -13,12 +15,10 @@ describe('paginator', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const paginator = require('../../../lib/plugins/helper/paginator').bind(ctx);

function link(i) {
return ctx.url_for(i === 1 ? '' : 'page/' + i + '/');
return url_for.call(ctx, i === 1 ? '' : 'page/' + i + '/');
}

function checkResult(result, data) {
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/helpers/tagcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ describe('tagcloud', () => {
config: hexo.config
};

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const tagcloud = require('../../../lib/plugins/helper/tagcloud').bind(ctx);

before(async () => {
Expand Down

0 comments on commit 0fb3e31

Please sign in to comment.