diff --git a/src/commands/Preview.ts b/src/commands/Preview.ts index 6c1959de..2604eff9 100644 --- a/src/commands/Preview.ts +++ b/src/commands/Preview.ts @@ -80,7 +80,7 @@ export class Preview { } else if (crntFolders && crntFolders.length > 1) { selectedFolder = await Preview.askUserToPickFolder(crntFolders); } else { - selectedFolder = await Preview.askUserToPickFolder(folders); + selectedFolder = await Preview.askUserToPickFolder(folders.filter((f) => f.previewPath)); } } @@ -118,7 +118,7 @@ export class Preview { // Get relative file path const folderPath = wsFolder ? parseWinPath(wsFolder.fsPath) : ''; const relativePath = filePath.replace(folderPath, ''); - pathname = processPathPlaceholders(pathname, relativePath); + pathname = processPathPlaceholders(pathname, relativePath, filePath, selectedFolder); } // Support front matter placeholders - {{fm.}} @@ -265,6 +265,10 @@ export class Preview { ): Promise { let selectedFolder: ContentFolder | undefined = undefined; + if (crntFolders.length === 0) { + return undefined; + } + // Ask the user to select the folder const folderNames = crntFolders.map((folder) => folder.title); const selectedFolderName = await window.showQuickPick(folderNames, { diff --git a/src/helpers/processPathPlaceholders.ts b/src/helpers/processPathPlaceholders.ts index 4baba7ab..32e9a980 100644 --- a/src/helpers/processPathPlaceholders.ts +++ b/src/helpers/processPathPlaceholders.ts @@ -1,15 +1,26 @@ -export const processPathPlaceholders = (value: string, path: string) => { - if (value && value.includes('{{pathToken.')) { - const regex = new RegExp('{{pathToken.(\\d+)}}', 'g'); - const matches = value.match(regex); +import { ContentFolder } from '../models'; +export const processPathPlaceholders = ( + value: string, + path: string, + filePath: string, + contentFolder: ContentFolder | null | undefined +) => { + if (value && value.includes('{{pathToken.')) { + const regex = /{{pathToken.(\d+|relPath)}}/g; + const matches = value.match(regex); if (matches) { for (const match of matches) { - const index = parseInt(match.replace('{{pathToken.', '').replace('}}', ''), 10); - const pathTokens = path.split('/'); + const tokenIndex = match.replace('{{pathToken.', '').replace('}}', ''); - if (pathTokens.length >= index) { - value = value.replace(match, pathTokens[index - 1]); + const pathTokens = path.split('/'); + if (tokenIndex === 'relPath') { + // Return path without the file name + const relFilePath = filePath.replace(contentFolder?.path || '', ''); + value = value.replace(match, relFilePath.substring(0, relFilePath.lastIndexOf('/'))); + } else { + // Get the token from the path + value = value.replace(match, pathTokens[Number(tokenIndex)]); } } }