From f2f39ddd40689595c7b04c519ae075db636c1b9a Mon Sep 17 00:00:00 2001 From: Kohei Yoshino Date: Sat, 4 Jan 2025 10:40:50 -0500 Subject: [PATCH] Fix some issues with assets with root dir path Close https://github.com/sveltia/sveltia-cms/issues/289 --- src/lib/services/assets/index.js | 19 ++++++++----------- src/lib/services/contents/draft/save.js | 7 +++++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib/services/assets/index.js b/src/lib/services/assets/index.js index 9758e03c..b8e22c43 100644 --- a/src/lib/services/assets/index.js +++ b/src/lib/services/assets/index.js @@ -342,17 +342,14 @@ export const getAssetByPath = (savedPath, { entry, collection } = {}) => { return exactMatch; } - const { dirname: publicPath, basename: fileName } = getPathInfo(savedPath); - - if (!publicPath) { - return undefined; - } + const { dirname: publicPath = '', basename: fileName } = getPathInfo(savedPath); // eslint-disable-next-line prefer-const - let { internalPath, collectionName } = - get(allAssetFolders).findLast((folder) => - publicPath.match(`^${folder.publicPath.replace(/{{.+?}}/g, '.+?')}\\b`), - ) ?? {}; + let { internalPath, collectionName } = !publicPath + ? get(allAssetFolders)[0] // Global asset folder + : (get(allAssetFolders).findLast((folder) => + publicPath.match(`^${folder.publicPath.replace(/{{.+?}}/g, '.+?')}\\b`), + ) ?? {}); if (internalPath === undefined) { return undefined; @@ -434,7 +431,7 @@ export const getAssetPublicURL = ( return undefined; } - const path = asset.path.replace(asset.folder, publicPath ?? ''); + const path = asset.path.replace(asset.folder, publicPath === '/' ? '' : (publicPath ?? '')); // Path starting with `@`, etc. cannot be linked if (!path.startsWith('/') && !allowSpecial) { @@ -445,7 +442,7 @@ export const getAssetPublicURL = ( return path; } - const baseURL = get(siteConfig)?.site_url ?? ''; + const baseURL = stripSlashes(get(siteConfig)?.site_url ?? ''); return `${baseURL}${path}`; }; diff --git a/src/lib/services/contents/draft/save.js b/src/lib/services/contents/draft/save.js index 348109b3..38f00a3e 100644 --- a/src/lib/services/contents/draft/save.js +++ b/src/lib/services/contents/draft/save.js @@ -647,7 +647,6 @@ const replaceBlobURL = async ({ const sha = await getHash(file); const dupFile = savingAssets.find((f) => f.sha === sha); - const useSubFolder = !!publicAssetFolder && publicAssetFolder !== '/'; let assetName = ''; // Check if the file has already been added for other field or locale @@ -676,7 +675,11 @@ const replaceBlobURL = async ({ blobURL, (_match, /** @type {number} */ offset) => { if (offset === index) { - return useSubFolder ? `${publicAssetFolder}/${assetName}` : assetName; + if (publicAssetFolder) { + return `${publicAssetFolder === '/' ? '' : publicAssetFolder}/${assetName}`; + } + + return assetName; } return _match;