From f1ae60f280b6a93c834d7aa4e81e85a15c1c2ce7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 20 Jun 2025 12:46:44 +0000 Subject: [PATCH] Fix variable frontmatter error - handle both string and object formats for preview image fields Co-authored-by: estruyf <2900833+estruyf@users.noreply.github.com> --- src/services/PagesParser.ts | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/services/PagesParser.ts b/src/services/PagesParser.ts index bd2e339f..87d7fc33 100644 --- a/src/services/PagesParser.ts +++ b/src/services/PagesParser.ts @@ -333,19 +333,32 @@ export class PagesParser { // Revalidate as the array could have been empty if (fieldValue) { - // Check if the value already starts with https - if that is the case, it is an external image - if (fieldValue.startsWith('http')) { - page.fmPreviewImage = fieldValue; + // Handle both string and object formats for the field value + let imageValue: string; + if (typeof fieldValue === 'string') { + imageValue = fieldValue; + } else if (typeof fieldValue === 'object' && fieldValue.src) { + // Handle object format like { src: "filename.jpg", title: "title" } + imageValue = fieldValue.src; } else { - let staticPath = join(wsFolder.fsPath, staticFolder || '', fieldValue); + // Skip processing if the value is neither a string nor an object with src + imageValue = null; + } - if (staticFolder === STATIC_FOLDER_PLACEHOLDER.hexo.placeholder) { - const crntFilePath = parseWinPath(filePath); - const pathWithoutExtension = crntFilePath.replace(extname(crntFilePath), ''); - staticPath = join(pathWithoutExtension, fieldValue); - } + if (imageValue) { + // Check if the value already starts with https - if that is the case, it is an external image + if (imageValue.startsWith('http')) { + page.fmPreviewImage = imageValue; + } else { + let staticPath = join(wsFolder.fsPath, staticFolder || '', imageValue); - const contentFolderPath = join(dirname(filePath), fieldValue); + if (staticFolder === STATIC_FOLDER_PLACEHOLDER.hexo.placeholder) { + const crntFilePath = parseWinPath(filePath); + const pathWithoutExtension = crntFilePath.replace(extname(crntFilePath), ''); + staticPath = join(pathWithoutExtension, imageValue); + } + + const contentFolderPath = join(dirname(filePath), imageValue); let previewUri = null; if (await existsAsync(staticPath)) { @@ -367,6 +380,7 @@ export class PagesParser { page['fmPreviewImage'] = previewPath || ''; } } + } } } }