Skip to content

Commit

Permalink
fix(moize): helper function not working fine with relative_url (#5217)
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jul 9, 2023
1 parent 7157e85 commit 3eaafb4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
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;
});
});

0 comments on commit 3eaafb4

Please sign in to comment.