Skip to content

Commit

Permalink
#844 - new {{filePrefix.index}} placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Sep 13, 2024
1 parent 83abff6 commit 6615108
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### ✨ New features

- [#844](https://github.com/estruyf/vscode-front-matter/issues/844): New `{{filePrefix.index}}` placeholder to add the index number of the file in the folder

### 🎨 Enhancements

- [#833](https://github.com/estruyf/vscode-front-matter/issues/833): Added support for Asciidoc files
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/ArticleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
parseWinPath,
processArticlePlaceholdersFromPath,
processDateTimePlaceholders,
processFilePrefixPlaceholders,
processI18nPlaceholders,
processTimePlaceholders
} from '.';
Expand Down Expand Up @@ -631,6 +632,7 @@ export class ArticleHelper {

if (prefix && typeof prefix === 'string') {
prefix = await ArticleHelper.processCustomPlaceholders(prefix, title, filePath, true);
prefix = await processFilePrefixPlaceholders(prefix, filePath);

let selectedFolder: ContentFolder | undefined | null = null;
if (filePath) {
Expand Down
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export * from './openFileInEditor';
export * from './parseWinPath';
export * from './processArticlePlaceholders';
export * from './processDateTimePlaceholders';
export * from './processFilePrefixPlaceholders';
export * from './processFmPlaceholders';
export * from './processI18nPlaceholders';
export * from './processPathPlaceholders';
Expand Down
55 changes: 55 additions & 0 deletions src/helpers/processFilePrefixPlaceholders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { FileType, Uri, workspace } from 'vscode';
import { parse } from 'path';

/**
* Processes file prefix placeholders in a given string value.
*
* This function replaces placeholders in the format `{{filePrefix.index}}` or `{{filePrefix.index|zeros:4}}`
* with the appropriate index number based on the number of files in the directory of the given file path.
*
* @param value - The string containing the placeholders to be replaced.
* @param folderPath - The path of the file whose directory will be used to determine the index number.
* @returns A promise that resolves to the string with the placeholders replaced by the index number.
*/
export const processFilePrefixPlaceholders = async (value: string, folderPath?: string) => {
// Example: {{filePrefix.index}} or {{filePrefix.index|chars:4,zeros:true}}
if (value && value.includes('{{filePrefix.index') && folderPath) {
const dirContent = await workspace.fs.readDirectory(Uri.file(folderPath));
const files = dirContent.filter(([_, type]) => type === FileType.File);

let chars = 3;
let idxValue = files.length + 1;

if (value.includes('{{filePrefix.index}}')) {
const regex = new RegExp('{{filePrefix.index}}', 'g');
const placeholderValue = idxValue.toString().padStart(chars, '0');
value = value.replace(regex, placeholderValue);
}
// Example: {{filePrefix.index|zeros:4}}
else if (value.includes('{{filePrefix.index')) {
const regex = /{{filePrefix.index[^}]*}}/g;
const matches = value.match(regex);
if (matches) {
for (const match of matches) {
const placeholderParts = match.split('|');
if (placeholderParts.length > 1) {
let options = placeholderParts[1].trim().replace('}}', '').split(',');

for (const option of options) {
if (option.startsWith('zeros:')) {
chars = parseInt(option.replace('zeros:', ''));
}
}

const placeholderValue = chars
? idxValue.toString().padStart(chars, '0')
: idxValue.toString();
value = value.replace(match, placeholderValue);
}
}
}
}
}

return value;
};

0 comments on commit 6615108

Please sign in to comment.