mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-03-28 17:42:40 +01:00
#812 - Fix date formatting with date placeholder
This commit is contained in:
@@ -13,7 +13,6 @@ import { join, parse } from 'path';
|
||||
import { commands, env, Uri, ViewColumn, window, WebviewPanel, extensions } from 'vscode';
|
||||
import {
|
||||
ArticleHelper,
|
||||
DateHelper,
|
||||
Extension,
|
||||
parseWinPath,
|
||||
processI18nPlaceholders,
|
||||
@@ -21,10 +20,10 @@ import {
|
||||
processFmPlaceholders,
|
||||
processPathPlaceholders,
|
||||
Settings,
|
||||
Telemetry
|
||||
Telemetry,
|
||||
processDateTimePlaceholders
|
||||
} from '../helpers';
|
||||
import { ContentFolder, ContentType, PreviewSettings } from '../models';
|
||||
import { format } from 'date-fns';
|
||||
import { Article } from '.';
|
||||
import { WebviewHelper } from '@estruyf/vscode';
|
||||
import { Folders } from './Folders';
|
||||
@@ -342,10 +341,8 @@ export class Preview {
|
||||
|
||||
try {
|
||||
const articleDate = await ArticleHelper.getDate(article);
|
||||
slug = join(
|
||||
format(articleDate || new Date(), DateHelper.formatUpdate(pathname) as string),
|
||||
slug
|
||||
);
|
||||
pathname = processDateTimePlaceholders(pathname, dateFormat, articleDate);
|
||||
slug = join(pathname, slug);
|
||||
} catch (error) {
|
||||
slug = join(pathname, slug);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ export * from './isValidFile';
|
||||
export * from './openFileInEditor';
|
||||
export * from './parseWinPath';
|
||||
export * from './processArticlePlaceholders';
|
||||
export * from './processDateTimePlaceholders';
|
||||
export * from './processFmPlaceholders';
|
||||
export * from './processI18nPlaceholders';
|
||||
export * from './processPathPlaceholders';
|
||||
|
||||
43
src/helpers/processDateTimePlaceholders.ts
Normal file
43
src/helpers/processDateTimePlaceholders.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { format } from 'date-fns';
|
||||
import { DateHelper } from './DateHelper';
|
||||
|
||||
/**
|
||||
* Replace the datetime placeholders
|
||||
* @param value
|
||||
* @param dateFormat
|
||||
* @param articleDate
|
||||
* @returns
|
||||
*/
|
||||
export const processDateTimePlaceholders = (
|
||||
value: string,
|
||||
dateFormat?: string,
|
||||
articleDate?: Date
|
||||
) => {
|
||||
if (value && typeof value === 'string') {
|
||||
if (value.includes(`{{date|`)) {
|
||||
const regex = /{{date\|[^}]*}}/g;
|
||||
const matches = value.match(regex);
|
||||
if (matches) {
|
||||
for (const match of matches) {
|
||||
const placeholderParts = match.split('|');
|
||||
if (placeholderParts.length > 1) {
|
||||
let dateFormat = placeholderParts[1].trim().replace('}}', '');
|
||||
|
||||
if (dateFormat) {
|
||||
if (dateFormat && typeof dateFormat === 'string') {
|
||||
value = value.replace(
|
||||
match,
|
||||
format(articleDate || new Date(), DateHelper.formatUpdate(dateFormat) as string)
|
||||
);
|
||||
} else {
|
||||
value = value.replace(match, (articleDate || new Date()).toISOString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
@@ -4,10 +4,10 @@ import { DateHelper } from './DateHelper';
|
||||
/**
|
||||
* Replace the time placeholders
|
||||
* @param value
|
||||
* @param title
|
||||
* @param dateFormat
|
||||
* @returns
|
||||
*/
|
||||
export const processTimePlaceholders = (value: string, dateFormat?: string) => {
|
||||
export const processTimePlaceholders = (value: string, dateFormat?: string, articleDate?: Date) => {
|
||||
if (value && typeof value === 'string') {
|
||||
if (value.includes('{{now}}')) {
|
||||
const regex = new RegExp('{{now}}', 'g');
|
||||
@@ -15,46 +15,46 @@ export const processTimePlaceholders = (value: string, dateFormat?: string) => {
|
||||
if (dateFormat && typeof dateFormat === 'string') {
|
||||
value = value.replace(
|
||||
regex,
|
||||
format(new Date(), DateHelper.formatUpdate(dateFormat) as string)
|
||||
format(articleDate || new Date(), DateHelper.formatUpdate(dateFormat) as string)
|
||||
);
|
||||
} else {
|
||||
value = value.replace(regex, new Date().toISOString());
|
||||
value = value.replace(regex, (articleDate || new Date()).toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
if (value.includes('{{year}}')) {
|
||||
const regex = new RegExp('{{year}}', 'g');
|
||||
value = value.replace(regex, format(new Date(), 'yyyy'));
|
||||
value = value.replace(regex, format(articleDate || new Date(), 'yyyy'));
|
||||
}
|
||||
|
||||
if (value.includes('{{month}}')) {
|
||||
const regex = new RegExp('{{month}}', 'g');
|
||||
value = value.replace(regex, format(new Date(), 'MM'));
|
||||
value = value.replace(regex, format(articleDate || new Date(), 'MM'));
|
||||
}
|
||||
|
||||
if (value.includes('{{day}}')) {
|
||||
const regex = new RegExp('{{day}}', 'g');
|
||||
value = value.replace(regex, format(new Date(), 'dd'));
|
||||
value = value.replace(regex, format(articleDate || new Date(), 'dd'));
|
||||
}
|
||||
|
||||
if (value.includes('{{hour12}}')) {
|
||||
const regex = new RegExp('{{hour12}}', 'g');
|
||||
value = value.replace(regex, format(new Date(), 'hh'));
|
||||
value = value.replace(regex, format(articleDate || new Date(), 'hh'));
|
||||
}
|
||||
|
||||
if (value.includes('{{hour24}}')) {
|
||||
const regex = new RegExp('{{hour24}}', 'g');
|
||||
value = value.replace(regex, format(new Date(), 'HH'));
|
||||
value = value.replace(regex, format(articleDate || new Date(), 'HH'));
|
||||
}
|
||||
|
||||
if (value.includes('{{ampm}}')) {
|
||||
const regex = new RegExp('{{ampm}}', 'g');
|
||||
value = value.replace(regex, format(new Date(), 'aaa'));
|
||||
value = value.replace(regex, format(articleDate || new Date(), 'aaa'));
|
||||
}
|
||||
|
||||
if (value.includes('{{minute}}')) {
|
||||
const regex = new RegExp('{{minute}}', 'g');
|
||||
value = value.replace(regex, format(new Date(), 'mm'));
|
||||
value = value.replace(regex, format(articleDate || new Date(), 'mm'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user