Add date formatter, language and text direction settings

This commit is contained in:
prototypa
2023-01-08 12:28:59 -05:00
parent a210c72431
commit 8ce8421334
12 changed files with 121 additions and 65 deletions

View File

@ -4,14 +4,14 @@ import type { Post } from '~/types';
import { cleanSlug } from './permalinks';
const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise<Post> => {
const { id, slug, data } = post;
const { id, slug = '', data } = post;
const { Content, injectedFrontmatter } = await post.render();
const { tags = [], category = 'default', author = 'Anonymous', publishDate, ...rest } = data;
const { tags = [], category = 'default', author = 'Anonymous', publishDate = new Date(), ...rest } = data;
return {
id: id,
slug: slug,
slug: cleanSlug(slug.split('/').pop() ?? ''),
publishDate: new Date(publishDate),
category: cleanSlug(category),

View File

@ -1,22 +1,18 @@
import slugify from 'limax';
import { SITE, BLOG } from '~/config.mjs';
const trim = (str = '', ch?: string) => {
let start = 0,
end = str.length || 0;
while (start < end && str[start] === ch) ++start;
while (end > start && str[end - 1] === ch) --end;
return start > 0 || end < str.length ? str.substring(start, end) : str;
};
import { trim } from '~/utils/utils';
const trimSlash = (s: string) => trim(trim(s, '/'));
const createPath = (...params: string[]) => {
const paths = params.filter((el) => !!el).join('/');
const paths = params
.map((el) => trimSlash(el))
.filter((el) => !!el)
.join('/');
return '/' + paths + (SITE.trailingSlash && paths ? '/' : '');
};
const basePathname = trimSlash(SITE.basePathname);
const BASE_PATHNAME = SITE.basePathname;
export const cleanSlug = (text: string) =>
trimSlash(text)
@ -34,25 +30,35 @@ export const getCanonical = (path = ''): string | URL => new URL(path, SITE.orig
/** */
export const getPermalink = (slug = '', type = 'page'): string => {
let permalink: string;
switch (type) {
case 'category':
return createPath(basePathname, CATEGORY_BASE, cleanSlug(slug));
permalink = createPath(CATEGORY_BASE, cleanSlug(slug));
break;
case 'tag':
return createPath(basePathname, TAG_BASE, cleanSlug(slug));
permalink = createPath(TAG_BASE, cleanSlug(slug));
break;
case 'post':
return createPath(basePathname, POST_BASE, cleanSlug(slug));
permalink = createPath(POST_BASE, cleanSlug(slug));
break;
case 'page':
default:
permalink = createPath(slug);
break;
}
return createPath(basePathname, trimSlash(slug));
return definitivePermalink(permalink);
};
/** */
export const getHomePermalink = (): string => {
const permalink = getPermalink();
return !permalink.startsWith('/') ? '/' + permalink : permalink;
};
export const getHomePermalink = (): string => getPermalink('/');
/** */
export const getBlogPermalink = (): string => getPermalink(BLOG_BASE);
/** */
const definitivePermalink = (permalink: string): string => createPath(BASE_PATHNAME, permalink);

View File

@ -1,9 +1,21 @@
import { DATE_FORMATTER } from '~/config.mjs';
const formatter =
DATE_FORMATTER ||
new Intl.DateTimeFormat('en', {
year: 'numeric',
month: 'short',
day: 'numeric',
timeZone: 'UTC'
});
/* eslint-disable no-mixed-spaces-and-tabs */
export const getFormattedDate = (date: Date) =>
date
? new Date(date).toLocaleDateString('en-us', {
year: 'numeric',
month: 'short',
day: 'numeric',
})
: '';
export const getFormattedDate = (date: Date) => (date ? formatter.format(date) : '');
export const trim = (str = '', ch?: string) => {
let start = 0,
end = str.length || 0;
while (start < end && str[start] === ch) ++start;
while (end > start && str[end - 1] === ch) --end;
return start > 0 || end < str.length ? str.substring(start, end) : str;
};