From 857a2bac62fa217a690eaf78bb9f9fe646718914 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Fri, 22 Dec 2023 21:12:10 +0800 Subject: [PATCH] fix(tag): use url_for --- lib/plugins/filter/before_exit/save_database.ts | 2 +- lib/plugins/tag/asset_img.ts | 1 + lib/plugins/tag/asset_link.ts | 4 ++-- lib/plugins/tag/asset_path.ts | 4 ++-- lib/plugins/tag/post_link.ts | 4 ++-- lib/plugins/tag/post_path.ts | 4 ++-- test/scripts/tags/post_link.js | 5 +++++ 7 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/plugins/filter/before_exit/save_database.ts b/lib/plugins/filter/before_exit/save_database.ts index 9a853eb93e..7c180edfd6 100644 --- a/lib/plugins/filter/before_exit/save_database.ts +++ b/lib/plugins/filter/before_exit/save_database.ts @@ -1,6 +1,6 @@ import type Hexo from '../../../hexo'; -function saveDatabaseFilter(this: Hexo) { +function saveDatabaseFilter(this: Hexo): Promise { if (!this.env.init || !this._dbLoaded) return; return this.database.save().then(() => { diff --git a/lib/plugins/tag/asset_img.ts b/lib/plugins/tag/asset_img.ts index 8a9941b74a..cc68a3c4a3 100644 --- a/lib/plugins/tag/asset_img.ts +++ b/lib/plugins/tag/asset_img.ts @@ -18,6 +18,7 @@ export = (ctx: Hexo) => { for (let i = 0; i < len; i++) { const asset = PostAsset.findOne({post: this._id, slug: args[i]}); if (asset) { + // img tag will call url_for so no need to call it here args[i] = encodeURL(new URL(asset.path, ctx.config.url).pathname); return img(ctx)(args); } diff --git a/lib/plugins/tag/asset_link.ts b/lib/plugins/tag/asset_link.ts index f132730db2..ae4014744c 100644 --- a/lib/plugins/tag/asset_link.ts +++ b/lib/plugins/tag/asset_link.ts @@ -1,4 +1,4 @@ -import { encodeURL, escapeHTML } from 'hexo-util'; +import { url_for, escapeHTML } from 'hexo-util'; import type Hexo from '../../hexo'; /** @@ -28,7 +28,7 @@ export = (ctx: Hexo) => { const attrTitle = escapeHTML(title); if (escape === 'true') title = attrTitle; - const link = encodeURL(new URL(asset.path, ctx.config.url).pathname); + const link = url_for.call(ctx, asset.path); return `${title}`; }; diff --git a/lib/plugins/tag/asset_path.ts b/lib/plugins/tag/asset_path.ts index 60e4c31746..e8c7dbad5d 100644 --- a/lib/plugins/tag/asset_path.ts +++ b/lib/plugins/tag/asset_path.ts @@ -1,4 +1,4 @@ -import { encodeURL } from 'hexo-util'; +import { url_for } from 'hexo-util'; import type Hexo from '../../hexo'; /** @@ -17,7 +17,7 @@ export = (ctx: Hexo) => { const asset = PostAsset.findOne({post: this._id, slug}); if (!asset) return; - const path = encodeURL(new URL(asset.path, ctx.config.url).pathname); + const path = url_for.call(ctx, asset.path); return path; }; diff --git a/lib/plugins/tag/post_link.ts b/lib/plugins/tag/post_link.ts index 67289df469..f24449bead 100644 --- a/lib/plugins/tag/post_link.ts +++ b/lib/plugins/tag/post_link.ts @@ -1,4 +1,4 @@ -import { encodeURL, escapeHTML } from 'hexo-util'; +import { url_for, encodeURL, escapeHTML } from 'hexo-util'; import { postFindOneFactory } from './'; import type Hexo from '../../hexo'; @@ -41,7 +41,7 @@ export = (ctx: Hexo) => { const attrTitle = escapeHTML(post.title || post.slug); if (escape === 'true') title = escapeHTML(title); - const url = new URL(post.path, ctx.config.url).pathname + (hash ? `#${hash}` : ''); + const url = url_for.call(ctx, post.path) + (hash ? `#${hash}` : ''); const link = encodeURL(url); return `${title}`; diff --git a/lib/plugins/tag/post_path.ts b/lib/plugins/tag/post_path.ts index fdc33eccab..6a00426f5e 100644 --- a/lib/plugins/tag/post_path.ts +++ b/lib/plugins/tag/post_path.ts @@ -1,4 +1,4 @@ -import { encodeURL } from 'hexo-util'; +import { url_for } from 'hexo-util'; import { postFindOneFactory } from './'; import type Hexo from '../../hexo'; @@ -17,7 +17,7 @@ export = (ctx: Hexo) => { const post = factory({ slug }) || factory({ title: slug }); if (!post) return; - const link = encodeURL(new URL(post.path, ctx.config.url).pathname); + const link = url_for.call(ctx, post.path); return link; }; diff --git a/test/scripts/tags/post_link.js b/test/scripts/tags/post_link.js index 05839a8098..deeac052b0 100644 --- a/test/scripts/tags/post_link.js +++ b/test/scripts/tags/post_link.js @@ -77,4 +77,9 @@ describe('post_link', () => { it('should keep hash', () => { postLink(['foo#bar']).should.eql('Hello world'); }); + + it('with root path', () => { + hexo.config.root = '/root/'; + postLink(['foo']).should.eql('Hello world'); + }); });