Skip to content

Commit

Permalink
#841 - enable placeholders in file prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Sep 9, 2024
1 parent d240e8f commit 431a83b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- [#833](https://github.com/estruyf/vscode-front-matter/issues/833): Added support for Asciidoc files
- [#834](https://github.com/estruyf/vscode-front-matter/issues/834): Added the ability to create new data files for a data folder
- [#841](https://github.com/estruyf/vscode-front-matter/issues/841): Enable placeholders for file prefixes
- [#846](https://github.com/estruyf/vscode-front-matter/issues/846): Added GitHub Copilot action for title field

### 🐞 Fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,7 @@
},
"frontMatter.templates.prefix": {
"type": "string",
"default": "yyyy-MM-dd",
"default": "{{date|yyyy-MM-dd}}",
"markdownDescription": "%setting.frontMatter.templates.prefix.markdownDescription%",
"scope": "Templates"
},
Expand Down
10 changes: 7 additions & 3 deletions src/commands/Article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,20 @@ export class Article {
return;
}

const titleField = getTitleField();
const articleTitle: string = article.data[titleField];
const articleDate = await ArticleHelper.getDate(article);

let filePrefix = Settings.get<string>(SETTING_TEMPLATES_PREFIX);
const contentType = await ArticleHelper.getContentType(article);
filePrefix = await ArticleHelper.getFilePrefix(
filePrefix,
editor.document.uri.fsPath,
contentType
contentType,
articleTitle,
articleDate
);

const titleField = getTitleField();
const articleTitle: string = article.data[titleField];
const slugInfo = Article.generateSlug(articleTitle, article, contentType.slugTemplate);

if (
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export class Preview {

try {
const articleDate = await ArticleHelper.getDate(article);
pathname = processDateTimePlaceholders(pathname, dateFormat, articleDate);
pathname = processDateTimePlaceholders(pathname, articleDate);
slug = join(pathname, slug);
} catch (error) {
slug = join(pathname, slug);
Expand Down
48 changes: 40 additions & 8 deletions src/helpers/ArticleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
isValidFile,
parseWinPath,
processArticlePlaceholdersFromPath,
processDateTimePlaceholders,
processI18nPlaceholders,
processTimePlaceholders
} from '.';
import { format, parse } from 'date-fns';
Expand All @@ -39,8 +41,7 @@ import { Article } from '../commands';
import { dirname, join, parse as parseFile } from 'path';
import { EditorHelper } from '@estruyf/vscode';
import sanitize from '../helpers/Sanitize';
import { Field, ContentType as IContentType } from '../models';
import { DateHelper } from './DateHelper';
import { ContentFolder, Field, ContentType as IContentType } from '../models';
import { DiagnosticSeverity, Position, window, Range } from 'vscode';
import { DEFAULT_FILE_TYPES } from '../constants/DefaultFileTypes';
import { fromMarkdown } from 'mdast-util-from-markdown';
Expand Down Expand Up @@ -534,7 +535,13 @@ export class ArticleHelper {
const fileType = Settings.get<string>(SETTING_CONTENT_DEFAULT_FILETYPE);

let prefix = Settings.get<string>(SETTING_TEMPLATES_PREFIX);
prefix = await ArticleHelper.getFilePrefix(prefix, folderPath, contentType);
prefix = await ArticleHelper.getFilePrefix(
prefix,
folderPath,
contentType,
titleValue,
new Date()
);

// Name of the file or folder to create
let sanitizedName = ArticleHelper.sanitize(titleValue);
Expand Down Expand Up @@ -596,12 +603,19 @@ export class ArticleHelper {
public static async getFilePrefix(
prefix: string | null | undefined,
filePath?: string,
contentType?: IContentType
contentType?: IContentType,
title?: string,
articleDate?: Date
): Promise<string | undefined> {
if (!prefix) {
prefix = undefined;
}

// Replace the default date format
if (prefix === 'yyyy-MM-dd') {
prefix = '{{date|yyyy-MM-dd}}';
}

// Retrieve the file prefix from the folder
if (filePath) {
const filePrefixOnFolder = await Folders.getFilePrefixBeFilePath(filePath);
Expand All @@ -615,9 +629,26 @@ export class ArticleHelper {
prefix = contentType.filePrefix;
}

// Process the prefix date formatting
if (prefix && typeof prefix === 'string') {
prefix = `${format(new Date(), DateHelper.formatUpdate(prefix) as string)}`;
prefix = await ArticleHelper.processCustomPlaceholders(prefix, title, filePath, true);

let selectedFolder: ContentFolder | undefined | null = null;
if (filePath) {
// Get the folder of the article by the file path
selectedFolder = await Folders.getPageFolderByFilePath(filePath);

if (!selectedFolder && contentType) {
selectedFolder = await Folders.getFolderByContentType(contentType, filePath);
}

if (selectedFolder) {
prefix = processI18nPlaceholders(prefix, selectedFolder);
}
}

const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string;
prefix = processTimePlaceholders(prefix, dateFormat);
prefix = processDateTimePlaceholders(prefix, articleDate);
}

return prefix;
Expand Down Expand Up @@ -667,7 +698,8 @@ export class ArticleHelper {
public static async processCustomPlaceholders(
value: string,
title: string | undefined,
filePath: string | undefined
filePath: string | undefined,
skipFileCheck = false
) {
if (value && typeof value === 'string') {
const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string;
Expand Down Expand Up @@ -711,7 +743,7 @@ export class ArticleHelper {
let updatedValue = placeHolderValue;

// Check if the file already exists, during creation it might not exist yet
if (filePath && (await existsAsync(filePath))) {
if (filePath && (await existsAsync(filePath)) && !skipFileCheck) {
updatedValue = await processArticlePlaceholdersFromPath(placeHolderValue, filePath);
}

Expand Down
6 changes: 1 addition & 5 deletions src/helpers/processDateTimePlaceholders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import { DateHelper } from './DateHelper';
* @param articleDate
* @returns
*/
export const processDateTimePlaceholders = (
value: string,
dateFormat?: string,
articleDate?: Date
) => {
export const processDateTimePlaceholders = (value: string, articleDate?: Date) => {
if (value && typeof value === 'string') {
if (value.includes(`{{date|`)) {
const regex = /{{date\|[^}]*}}/g;
Expand Down

0 comments on commit 431a83b

Please sign in to comment.