From 3eaafb44fca9a537b4089481a9f4f60f10ab4c2a Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Mon, 10 Jul 2023 01:28:07 +0800 Subject: [PATCH] fix(moize): helper function not working fine with relative_url (#5217) --- lib/plugins/helper/css.ts | 8 +++++++- lib/plugins/helper/js.ts | 8 +++++++- test/scripts/helpers/css.js | 13 +++++++++++++ test/scripts/helpers/js.js | 13 +++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/plugins/helper/css.ts b/lib/plugins/helper/css.ts index b7a86f5524..74ca0ba357 100644 --- a/lib/plugins/helper/css.ts +++ b/lib/plugins/helper/css.ts @@ -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; @@ -23,5 +26,8 @@ function cssHelper(...args) { export = moize(cssHelper, { maxSize: 10, - isDeepEqual: true + isDeepEqual: true, + updateCacheForKey() { + return relative_link; + } }); diff --git a/lib/plugins/helper/js.ts b/lib/plugins/helper/js.ts index 120d1738bb..24ca0fca3d 100644 --- a/lib/plugins/helper/js.ts +++ b/lib/plugins/helper/js.ts @@ -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; @@ -23,5 +26,8 @@ function jsHelper(...args) { export = moize(jsHelper, { maxSize: 10, - isDeepEqual: true + isDeepEqual: true, + updateCacheForKey() { + return relative_link; + } }); diff --git a/test/scripts/helpers/css.js b/test/scripts/helpers/css.js index ea68001273..6241ff93fd 100644 --- a/test/scripts/helpers/css.js +++ b/test/scripts/helpers/css.js @@ -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; + }); }); diff --git a/test/scripts/helpers/js.js b/test/scripts/helpers/js.js index 5dbc4a16d4..22a23805b1 100644 --- a/test/scripts/helpers/js.js +++ b/test/scripts/helpers/js.js @@ -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; + }); });