#425 - added new placeholders for previews

This commit is contained in:
Elio Struyf
2023-02-10 13:24:59 +01:00
parent 327559fdd7
commit 558845a8d2
6 changed files with 73 additions and 2 deletions
+1
View File
@@ -368,6 +368,7 @@ export class Folders {
return {
...folder,
originalPath: folder.path,
path: folderPath
};
});
+31 -2
View File
@@ -1,3 +1,5 @@
import { processFmPlaceholders } from './../helpers/processFmPlaceholders';
import { processPathPlaceholders } from './../helpers/processPathPlaceholders';
import { Telemetry } from './../helpers/Telemetry';
import {
SETTING_PREVIEW_HOST,
@@ -5,12 +7,13 @@ import {
CONTEXT,
TelemetryEvent,
PreviewCommands,
SETTING_EXPERIMENTAL
SETTING_EXPERIMENTAL,
SETTING_DATE_FORMAT
} from './../constants';
import { ArticleHelper } from './../helpers/ArticleHelper';
import { join } from 'path';
import { commands, env, Uri, ViewColumn, window } from 'vscode';
import { Extension, parseWinPath, Settings } from '../helpers';
import { Extension, parseWinPath, processKnownPlaceholders, Settings } from '../helpers';
import { ContentFolder, PreviewSettings } from '../models';
import { format } from 'date-fns';
import { DateHelper } from '../helpers/DateHelper';
@@ -63,6 +66,32 @@ export class Preview {
if (selectedFolder) {
pathname = selectedFolder.previewPath;
}
if (pathname) {
// Known placeholders
const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string;
pathname = processKnownPlaceholders(pathname, article?.data?.title, dateFormat);
// Custom placeholders
pathname = await ArticleHelper.processCustomPlaceholders(
pathname,
article?.data?.title,
filePath
);
// Process the path placeholders - {{pathToken.<integer>}}
if (selectedFolder?.originalPath) {
const folderPath = processKnownPlaceholders(
selectedFolder.originalPath,
article?.data?.title,
dateFormat
);
pathname = processPathPlaceholders(pathname, folderPath);
}
// Support front matter placeholders - {{fm.<field>}}
pathname = processFmPlaceholders(pathname, article?.data);
}
}
// Check if there is a pathname defined on content type level
+2
View File
@@ -32,3 +32,5 @@ export * from './getTaxonomyField';
export * from './isValidFile';
export * from './openFileInEditor';
export * from './parseWinPath';
export * from './processFmPlaceholders';
export * from './processPathPlaceholders';
+19
View File
@@ -0,0 +1,19 @@
export const processFmPlaceholders = (value: string, fmData: any) => {
if (value && value.includes('{{fm.')) {
const regex = new RegExp('{{fm.(\\w+)}}', 'g');
const matches = value.match(regex);
if (matches) {
for (const match of matches) {
const field = match.replace('{{fm.', '').replace('}}', '');
const fieldValue = fmData[field];
if (fieldValue) {
value = value.replace(match, fieldValue);
}
}
}
}
return value;
};
+19
View File
@@ -0,0 +1,19 @@
export const processPathPlaceholders = (value: string, path: string) => {
if (value && value.includes('{{pathToken.')) {
const regex = new RegExp('{{pathToken.(\\d+)}}', '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('/');
if (pathTokens.length >= index) {
value = value.replace(match, pathTokens[index - 1]);
}
}
}
}
return value;
};
+1
View File
@@ -6,4 +6,5 @@ export interface ContentFolder {
previewPath?: string;
filePrefix?: string;
contentTypes?: string[];
originalPath?: string;
}