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(moize): helper function not working fine with relative_url #5217

Merged
merged 2 commits into from
Jul 9, 2023
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
8 changes: 7 additions & 1 deletion lib/plugins/helper/css.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { htmlTag, url_for } from 'hexo-util';
import moize from 'moize';

let relative_link = true;
function cssHelper(...args) {
let result = '\n';

relative_link = this.config.relative_link;

args.flat(Infinity).forEach(item => {
if (typeof item === 'string' || item instanceof String) {
let path = item;
Expand All @@ -23,5 +26,8 @@ function cssHelper(...args) {

export = moize(cssHelper, {
maxSize: 10,
isDeepEqual: true
isDeepEqual: true,
updateCacheForKey() {
return relative_link;
}
});
8 changes: 7 additions & 1 deletion lib/plugins/helper/js.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { htmlTag, url_for } from 'hexo-util';
import moize from 'moize';

let relative_link = true;
function jsHelper(...args) {
let result = '\n';

relative_link = this.config.relative_link;

args.flat(Infinity).forEach(item => {
if (typeof item === 'string' || item instanceof String) {
let path = item;
Expand All @@ -23,5 +26,8 @@ function jsHelper(...args) {

export = moize(jsHelper, {
maxSize: 10,
isDeepEqual: true
isDeepEqual: true,
updateCacheForKey() {
return relative_link;
}
});
13 changes: 13 additions & 0 deletions test/scripts/helpers/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,17 @@ describe('css', () => {
assertResult(css([{href: '/aaa.css', bbb: 'ccc'}, {href: '/ddd.css', eee: 'fff'}]),
[{href: '/aaa.css', bbb: 'ccc'}, {href: '/ddd.css', eee: 'fff'}]);
});

it('relative link', () => {
ctx.config.relative_link = true;
ctx.config.root = '/';

ctx.path = '';
assertResult(css('style'), 'style.css');

ctx.path = 'foo/bar/';
assertResult(css('style'), '../../style.css');

ctx.config.relative_link = false;
});
});
13 changes: 13 additions & 0 deletions test/scripts/helpers/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@ describe('js', () => {
assertResult(js({src: '/foo.js', 'async': true}), {src: '/foo.js', 'async': true});
assertResult(js({src: '/bar.js', 'defer': true}), {src: '/bar.js', 'defer': true});
});

it('relative link', () => {
ctx.config.relative_link = true;
ctx.config.root = '/';

ctx.path = '';
assertResult(js('script'), 'script.js');

ctx.path = 'foo/bar/';
assertResult(js('script'), '../../script.js');

ctx.config.relative_link = false;
});
});