From 4f2dde488cf6caec871c0b25681d11f5cc61ae22 Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Sat, 31 Dec 2022 17:44:28 +0900 Subject: [PATCH] refactor: use the `WHATWG URL API` instead of `url.resolve` (#5136) --- lib/plugins/tag/asset_img.js | 3 +-- lib/plugins/tag/asset_link.js | 3 +-- lib/plugins/tag/asset_path.js | 3 +-- lib/plugins/tag/post_link.js | 3 +-- lib/plugins/tag/post_path.js | 3 +-- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/plugins/tag/asset_img.js b/lib/plugins/tag/asset_img.js index df1b67bd34..5189c28a27 100644 --- a/lib/plugins/tag/asset_img.js +++ b/lib/plugins/tag/asset_img.js @@ -1,6 +1,5 @@ 'use strict'; -const { resolve } = require('url'); const img = require('./img'); const { encodeURL } = require('hexo-util'); @@ -20,7 +19,7 @@ module.exports = ctx => { for (let i = 0; i < len; i++) { const asset = PostAsset.findOne({post: this._id, slug: args[i]}); if (asset) { - args[i] = encodeURL(resolve('/', asset.path)); + args[i] = encodeURL(new URL(asset.path, ctx.config.url).pathname); return img(ctx)(args); } } diff --git a/lib/plugins/tag/asset_link.js b/lib/plugins/tag/asset_link.js index c51dc3e005..8f3225e297 100644 --- a/lib/plugins/tag/asset_link.js +++ b/lib/plugins/tag/asset_link.js @@ -1,7 +1,6 @@ 'use strict'; const { encodeURL, escapeHTML } = require('hexo-util'); -const { resolve } = require('url'); /** * Asset link tag @@ -30,7 +29,7 @@ module.exports = ctx => { const attrTitle = escapeHTML(title); if (escape === 'true') title = attrTitle; - const link = encodeURL(resolve(ctx.config.root, asset.path)); + const link = encodeURL(new URL(asset.path, ctx.config.url).pathname); return `${title}`; }; diff --git a/lib/plugins/tag/asset_path.js b/lib/plugins/tag/asset_path.js index 88be887673..d988176d30 100644 --- a/lib/plugins/tag/asset_path.js +++ b/lib/plugins/tag/asset_path.js @@ -1,6 +1,5 @@ 'use strict'; -const { resolve } = require('url'); const { encodeURL } = require('hexo-util'); /** @@ -19,7 +18,7 @@ module.exports = ctx => { const asset = PostAsset.findOne({post: this._id, slug}); if (!asset) return; - const path = encodeURL(resolve(ctx.config.root, asset.path)); + const path = encodeURL(new URL(asset.path, ctx.config.url).pathname); return path; }; diff --git a/lib/plugins/tag/post_link.js b/lib/plugins/tag/post_link.js index 553e7599ce..838b046560 100644 --- a/lib/plugins/tag/post_link.js +++ b/lib/plugins/tag/post_link.js @@ -1,7 +1,6 @@ 'use strict'; const { encodeURL, escapeHTML } = require('hexo-util'); -const { resolve } = require('url'); const { postFindOneFactory } = require('./'); /** @@ -35,7 +34,7 @@ module.exports = ctx => { const attrTitle = escapeHTML(post.title); if (escape === 'true') title = escapeHTML(title); - const link = encodeURL(resolve(ctx.config.root, post.path)); + const link = encodeURL(new URL(post.path, ctx.config.url).pathname); return `${title}`; }; diff --git a/lib/plugins/tag/post_path.js b/lib/plugins/tag/post_path.js index 89346560f4..679f65039f 100644 --- a/lib/plugins/tag/post_path.js +++ b/lib/plugins/tag/post_path.js @@ -1,6 +1,5 @@ 'use strict'; -const { resolve } = require('url'); const { encodeURL } = require('hexo-util'); const { postFindOneFactory } = require('./'); @@ -19,7 +18,7 @@ module.exports = ctx => { const post = factory({ slug }) || factory({ title: slug }); if (!post) return; - const link = encodeURL(resolve(ctx.config.root, post.path)); + const link = encodeURL(new URL(post.path, ctx.config.url).pathname); return link; };