From 19a0f4b53f978cf3610461ca79a4636b79c7294f Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Thu, 23 May 2024 22:02:15 +0200 Subject: [PATCH] #812 - Added `{{locale}}` placeholder --- CHANGELOG.md | 1 + src/commands/Preview.ts | 19 +++++++++---- src/helpers/index.ts | 1 + src/helpers/processI18nPlaceholders.ts | 39 ++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/helpers/processI18nPlaceholders.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5043e458..c0bf402f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/commands/Preview.ts b/src/commands/Preview.ts index 8a1a991b..568b5e61 100644 --- a/src/commands/Preview.ts +++ b/src/commands/Preview.ts @@ -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') { diff --git a/src/helpers/index.ts b/src/helpers/index.ts index 7d58a086..2c1a7f16 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -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'; diff --git a/src/helpers/processI18nPlaceholders.ts b/src/helpers/processI18nPlaceholders.ts new file mode 100644 index 00000000..64dbb14e --- /dev/null +++ b/src/helpers/processI18nPlaceholders.ts @@ -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; +};