mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-12 03:43:01 +02:00
#841 - enable placeholders in file prefixes
This commit is contained in:
@@ -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
|
||||
|
||||
+1
-1
@@ -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"
|
||||
},
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -31,6 +31,8 @@ import {
|
||||
isValidFile,
|
||||
parseWinPath,
|
||||
processArticlePlaceholdersFromPath,
|
||||
processDateTimePlaceholders,
|
||||
processI18nPlaceholders,
|
||||
processTimePlaceholders
|
||||
} from '.';
|
||||
import { format, parse } from 'date-fns';
|
||||
@@ -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';
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user