#812 - Added {{locale}} placeholder

This commit is contained in:
Elio Struyf
2024-05-23 22:02:15 +02:00
parent 0bde5610c5
commit 19a0f4b53f
4 changed files with 54 additions and 6 deletions
+1
View File
@@ -17,6 +17,7 @@
- [#808](https://github.com/estruyf/vscode-front-matter/issues/808): Add support to generate field groups and `block` fields in content type generation
- [#810](https://github.com/estruyf/vscode-front-matter/issues/810): Update the tab title based on the view
- [#811](https://github.com/estruyf/vscode-front-matter/issues/811): Added `panel.gitActions` view mode option to hide the Git actions in the panel
- [#812](https://github.com/estruyf/vscode-front-matter/issues/812): Added the `{{locale}}` placeholder which can be used in the `previewPath` property
### ⚡️ Optimizations
+13 -6
View File
@@ -1,6 +1,3 @@
import { processFmPlaceholders } from './../helpers/processFmPlaceholders';
import { processPathPlaceholders } from './../helpers/processPathPlaceholders';
import { Telemetry } from './../helpers/Telemetry';
import {
SETTING_PREVIEW_HOST,
SETTING_PREVIEW_PATHNAME,
@@ -12,13 +9,22 @@ import {
GeneralCommands,
SETTING_PREVIEW_TRAILING_SLASH
} from './../constants';
import { ArticleHelper } from './../helpers/ArticleHelper';
import { join, parse } from 'path';
import { commands, env, Uri, ViewColumn, window, WebviewPanel, extensions } from 'vscode';
import { Extension, parseWinPath, processTimePlaceholders, Settings } from '../helpers';
import {
ArticleHelper,
DateHelper,
Extension,
parseWinPath,
processI18nPlaceholders,
processTimePlaceholders,
processFmPlaceholders,
processPathPlaceholders,
Settings,
Telemetry
} from '../helpers';
import { ContentFolder, ContentType, PreviewSettings } from '../models';
import { format } from 'date-fns';
import { DateHelper } from '../helpers/DateHelper';
import { Article } from '.';
import { WebviewHelper } from '@estruyf/vscode';
import { Folders } from './Folders';
@@ -311,6 +317,7 @@ export class Preview {
const folderPath = wsFolder ? parseWinPath(wsFolder.fsPath) : '';
const relativePath = filePath.replace(folderPath, '');
pathname = processPathPlaceholders(pathname, relativePath, filePath, selectedFolder);
pathname = processI18nPlaceholders(pathname, selectedFolder);
const file = parse(filePath);
if (file.name.toLowerCase() === 'index') {
+1
View File
@@ -33,5 +33,6 @@ export * from './openFileInEditor';
export * from './parseWinPath';
export * from './processArticlePlaceholders';
export * from './processFmPlaceholders';
export * from './processI18nPlaceholders';
export * from './processPathPlaceholders';
export * from './processTimePlaceholders';
+39
View File
@@ -0,0 +1,39 @@
import { ContentFolder } from '../models';
export const processI18nPlaceholders = (
value: string,
contentFolder: ContentFolder | null | undefined
) => {
// Example: {{locale}}
if (value && contentFolder?.locale) {
if (value.includes('{{locale}}')) {
const regex = new RegExp('{{locale}}', 'g');
value = value.replace(regex, contentFolder.locale);
}
// Example: {{locale | ignore:en}}
else if (value.includes('{{locale')) {
const regex = /{{locale[^}]*}}/g;
const matches = value.match(regex);
if (matches) {
for (const match of matches) {
const placeholderParts = match.split('|');
if (placeholderParts.length > 1) {
let ignore = placeholderParts[1].trim().replace('}}', '');
if (ignore.startsWith('ignore:')) {
ignore = ignore.replace('ignore:', '');
if (ignore.includes(contentFolder.locale)) {
value = value.replace(match, '');
} else {
value = value.replace(match, contentFolder.locale);
}
}
}
}
}
}
}
return value;
};