More changes to structure and UI

This commit is contained in:
2022-04-27 21:55:18 -04:00
parent 2d4a2809b4
commit 6ba7d561fb
22 changed files with 257 additions and 218 deletions

View File

@@ -13,11 +13,13 @@ const months = [
'December'
];
const suffixes = ['','st','nd','rd','th'];
const ordSfx = ['','st','nd','rd','th'];
function prettyPrint(date: Date | string): string {
const oDate = (typeof date === 'string')? new Date(date): date;
export default function prettyDatePrint(date: Date) {
const now = new Date();
const diff = now.getTime() - date.getTime();
const diff = now.getTime() - oDate.getTime();
let tdiff = Math.floor(diff/1000);
@@ -38,13 +40,27 @@ export default function prettyDatePrint(date: Date) {
return `Yesterday`;
}
const year = date.getFullYear();
const month = months[date.getMonth()];
const day = date.getDate();
const year = oDate.getFullYear();
const month = months[oDate.getMonth()];
const day = oDate.getDate();
let sfx;
if (day >= 1 && day <= 3)
sfx = suffixes[day];
sfx = ordSfx[day];
else
sfx = suffixes[4];
return `${day}${sfx} ${month} ${year}`;
}
sfx = ordSfx[4];
if (year != now.getFullYear())
return `${day}${sfx} ${month} ${year}`;
return `${day}${sfx} ${month}`;
}
function isValid(date: any) {
return (new Date(date)).toString() === 'Invalid Date';
}
const d = {
prettyPrint,
isValid
};
export default d;

View File

@@ -1,91 +0,0 @@
import style from '../styles/lists.module.css';
import React, { ReactElement } from 'react';
interface listItem {
children?: listItem[] | string[];
url?: string;
title: string;
description?: string;
};
export function toListItem(record: Record<string, any>): listItem | null {
if (!record.title)
return null;
let children: listItem[] | string[] = [];
if (Array.isArray(record.children) && record.children.length) {
let lchildren: listItem[] = [];
let schildren: string[] = [];
for (const child of record.children) {
if (typeof child === 'string') {
schildren.push(child);
continue;
}
const lChild = toListItem(child);
if (lChild)
lchildren.push(lChild);
}
if (!lchildren.length) {
children = schildren;
}
else {
children = [...lchildren, ...schildren.map((s: string): listItem => {
return { title: s };
})];
}
}
return {
title: record.title,
url: record.url,
children: children.length ? children : undefined,
description: record.description,
};
}
export function mapChild(obj: listItem | string, level: number) {
if (typeof obj === 'string') {
if (obj === '')
return <></>
return <span className={style.listItem}>{obj}</span>
}
if (obj.title === '')
return <></>
const desc = obj.description
? <span className={style.listItemDesc}>{obj.description}</span>
: <></>;
if (obj.url)
return (
<>
<span className={style.listItem}><a href={obj.url}>{obj.title}</a></span>
{desc}
</>);
if (!obj.children)
return (
<>
<span className={style.listItem}>{obj.title}</span>
{desc}
</>);
let title: ReactElement;
if (level >= 0 && level <= 4)
title = React.createElement(`h${level + 2}`, {}, obj.title);
else
title = React.createElement('strong', {}, obj.title);
return (
<section className={level < 4 ? 'block' : ''}>
{title}
{obj.description ? <p className={style.desc}>{obj.description}</p> : <></>}
<div>
{obj.children.map(l => mapChild(l, level + 1))}
</div>
</section>
);
}

View File

@@ -3,6 +3,7 @@ const matter = require('gray-matter');
const { join } = require('path');
const postsDir = join(process.cwd(), 'posts');
const cacheDir = join(process.cwd(), '.cache');
function getPost(rawslug, filter = []) {
const slug = rawslug.replace(/\.md$/, '');
@@ -11,7 +12,7 @@ function getPost(rawslug, filter = []) {
const { data, content } = matter(file);
if (data['last_updated'] === undefined)
data['last_updated'] = data['created_at'];
data['last_updated'] = '';
if (filter.length === 0)
return { ...data, content, slug, rawslug };
@@ -41,13 +42,24 @@ function getAllPosts(filter = []) {
.filter(c => (!c.match(/^\.]/) && c.match(/\.md$/)))
.map(file => {
return getPost(file, filter)
})
.sort((a, b) => {
const dA = new Date(a['created_at']);
const dB = new Date(b['created_at']);
return dB - dA;
});
}
const postMetaCacheFile = join(cacheDir, 'posts.meta.json');
function cachePostsMeta() { // public access cache
const posts = getAllPosts(['title', 'slug', 'created_at', 'last_updated']);
fs.writeFile(join(postsDir, 'meta.json'), JSON.stringify(posts), (e) => {
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir);
}
fs.writeFile(postMetaCacheFile, JSON.stringify(posts), (e) => {
if (e)
console.error(e);
});
@@ -55,13 +67,16 @@ function cachePostsMeta() { // public access cache
}
function getPostsMeta() {
const file = fs.readFileSync(join(postsDir, 'meta.json'), 'utf-8');
if (!file) {
try {
const file = fs.readFileSync(postMetaCacheFile, 'utf-8');
return JSON.parse(file);
} catch (e) {
return cachePostsMeta();
}
return JSON.parse(file);
}
module.exports = { getAllPosts, getPost, getPostsMeta, cachePostsMeta };
function cache() {
cachePostsMeta();
}
module.exports = { getAllPosts, getPost, getPostsMeta, cache };