2023-03-10 22:44:54 +01:00
|
|
|
import {
|
|
|
|
format as formatBase,
|
|
|
|
formatDistanceToNow as formatDistanceBase,
|
|
|
|
} from 'date-fns';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the formatted date string in the given format.
|
|
|
|
*
|
|
|
|
* @see https://date-fns.org/docs/format
|
|
|
|
*/
|
|
|
|
export const format = (date: string, pattern = 'd MMMM yyyy') =>
|
|
|
|
formatBase(new Date(date), pattern);
|
2023-03-08 21:59:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the distance between the given date and now in words.
|
|
|
|
*
|
|
|
|
* @see https://date-fns.org/docs/formatDistanceToNow
|
|
|
|
*/
|
|
|
|
export const formatDistanceToNow = (date: string) =>
|
2023-03-10 22:44:54 +01:00
|
|
|
formatDistanceBase(new Date(date));
|
2023-03-09 23:32:49 +01:00
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access,
|
|
|
|
@typescript-eslint/no-unsafe-call,
|
|
|
|
@typescript-eslint/no-explicit-any */
|
|
|
|
export default function datesPlugin(eleventyConfig: any) {
|
|
|
|
// filters...
|
2023-03-10 22:44:54 +01:00
|
|
|
eleventyConfig.addLiquidFilter('format', format);
|
2023-03-09 23:32:49 +01:00
|
|
|
eleventyConfig.addLiquidFilter('formatDistanceToNow', formatDistanceToNow);
|
|
|
|
}
|