Fixes in preview paths

This commit is contained in:
Elio Struyf
2023-02-14 16:12:39 +01:00
parent 3a8854b060
commit 8ae105fd6e
2 changed files with 25 additions and 10 deletions
+6 -2
View File
@@ -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.<field>}}
@@ -265,6 +265,10 @@ export class Preview {
): Promise<ContentFolder | undefined> {
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, {
+19 -8
View File
@@ -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)]);
}
}
}