mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-09 02:12:50 +02:00
#425 - added new placeholders for previews
This commit is contained in:
@@ -368,6 +368,7 @@ export class Folders {
|
||||
|
||||
return {
|
||||
...folder,
|
||||
originalPath: folder.path,
|
||||
path: folderPath
|
||||
};
|
||||
});
|
||||
|
||||
+31
-2
@@ -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
|
||||
|
||||
@@ -32,3 +32,5 @@ export * from './getTaxonomyField';
|
||||
export * from './isValidFile';
|
||||
export * from './openFileInEditor';
|
||||
export * from './parseWinPath';
|
||||
export * from './processFmPlaceholders';
|
||||
export * from './processPathPlaceholders';
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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;
|
||||
};
|
||||
@@ -6,4 +6,5 @@ export interface ContentFolder {
|
||||
previewPath?: string;
|
||||
filePrefix?: string;
|
||||
contentTypes?: string[];
|
||||
originalPath?: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user