From 94f6ad3c91f59a605338827bf08a610c305f3da1 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Tue, 9 Apr 2024 16:00:51 +0800 Subject: [PATCH] fix(tag): use url_for (#5385) Co-authored-by: Uiolee <22849383+uiolee@users.noreply.github.com> --- lib/box/index.ts | 4 +++- lib/models/post_asset.ts | 5 +++-- 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 | 11 ++--------- lib/plugins/tag/post_path.ts | 4 ++-- test/scripts/models/post_asset.ts | 10 +++++----- test/scripts/tags/post_link.ts | 2 +- 9 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/box/index.ts b/lib/box/index.ts index e55079c8de..4da7c69d4e 100644 --- a/lib/box/index.ts +++ b/lib/box/index.ts @@ -160,8 +160,10 @@ class Box extends EventEmitter { if (!params) return count; const file = new File({ + // source is used for filesystem path, keep backslashes on Windows source: join(base, path), - path, + // path is used for URL path, replace backslashes on Windows + path: escapeBackslash(path), params, type }); diff --git a/lib/models/post_asset.ts b/lib/models/post_asset.ts index fe73a02a47..8572029b8c 100644 --- a/lib/models/post_asset.ts +++ b/lib/models/post_asset.ts @@ -1,5 +1,5 @@ import warehouse from 'warehouse'; -import { join } from 'path'; +import { join, posix } from 'path'; import type Hexo from '../hexo'; export = (ctx: Hexo) => { @@ -19,7 +19,8 @@ export = (ctx: Hexo) => { // PostAsset.path is file path relative to `public_dir` // no need to urlescape, #1562 // strip /\.html?$/ extensions on permalink, #2134 - return join(post.path.replace(/\.html?$/, ''), this.slug); + // Use path.posix.join to avoid path.join introducing unwanted backslashes on Windows. + return posix.join(post.path.replace(/\.html?$/, ''), this.slug); }); PostAsset.virtual('source').get(function() { 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 9aec591a9d..f0203342fb 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, escapeHTML } from 'hexo-util'; import { postFindOneFactory } from './'; import type Hexo from '../../hexo'; @@ -41,14 +41,7 @@ export = (ctx: Hexo) => { const attrTitle = escapeHTML(post.title || post.slug); if (escape === 'true') title = escapeHTML(title); - // guarantee the base url ends with a slash. (case of using a subdirectory in the url of the site) - let baseUrl = ctx.config.url; - if (!baseUrl.endsWith('/')) { - baseUrl += '/'; - } - - const url = new URL(post.path, baseUrl).pathname + (hash ? `#${hash}` : ''); - const link = encodeURL(url); + const link = url_for.call(ctx, post.path + (hash ? `#${hash}` : '')); 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/models/post_asset.ts b/test/scripts/models/post_asset.ts index 26004d6225..52e6a608f8 100644 --- a/test/scripts/models/post_asset.ts +++ b/test/scripts/models/post_asset.ts @@ -1,4 +1,4 @@ -import { join } from 'path'; +import { join, posix } from 'path'; import Hexo from '../../../lib/hexo'; import defaults from '../../../lib/hexo/default_config'; @@ -54,7 +54,7 @@ describe('PostAsset', () => { slug: 'foo.jpg', post: post._id }); - data.path.should.eql(join(post.path, data.slug)); + data.path.should.eql(posix.join(post.path, data.slug)); PostAsset.removeById(data._id); }); @@ -66,7 +66,7 @@ describe('PostAsset', () => { slug: 'foo.htm', post: post._id }); - data.path.should.eql(join(post.path, data.slug)); + data.path.should.eql(posix.join(post.path, data.slug)); PostAsset.removeById(data._id); }); @@ -78,7 +78,7 @@ describe('PostAsset', () => { slug: 'foo.htm', post: post._id }); - data.path.should.eql(join(post.path, data.slug)); + data.path.should.eql(posix.join(post.path, data.slug)); PostAsset.removeById(data._id); }); @@ -90,7 +90,7 @@ describe('PostAsset', () => { slug: 'foo.html', post: post._id }); - data.path.should.eql(join(post.path + '.htm-foo/', data.slug)); + data.path.should.eql(posix.join(post.path + '.htm-foo/', data.slug)); PostAsset.removeById(data._id); }); diff --git a/test/scripts/tags/post_link.ts b/test/scripts/tags/post_link.ts index c2e990d007..f72f910da7 100644 --- a/test/scripts/tags/post_link.ts +++ b/test/scripts/tags/post_link.ts @@ -81,7 +81,7 @@ describe('post_link', () => { }); it('should keep subdir', () => { - hexo.config.url = 'http://example.com/subdir'; + hexo.config.root = '/subdir/'; postLink(['foo']).should.eql('Hello world'); }); });