Webpack hook for jsongen, [somewhat broken] UI changes

This commit is contained in:
2022-04-24 00:27:51 -04:00
parent 103eec9551
commit d189096e96
23 changed files with 199 additions and 96 deletions

1
util/post-cache.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
export default function cachePostLinkData(): any;

16
util/post-cache.js Normal file
View File

@@ -0,0 +1,16 @@
const fs = require('fs');
const { getAllPosts } = require('./slug');
const { join } = require('path');
const publicDir = join(process.cwd(), 'public');
module.exports = {
cachePostLinkData: () => {
const posts = getAllPosts(['title', 'slug', 'created_at', 'last_updated']);
fs.writeFile(`${publicDir}/posts.json`, JSON.stringify(posts), (e) => {
if (e)
console.error(e);
});
return posts;
}
}

View File

@@ -1,14 +0,0 @@
import fs from 'fs';
import { getAllPosts } from './slug';
import { join } from 'path';
const publicDir = join(process.cwd(), 'public');
export default function cachePostLinkData() {
const posts = getAllPosts(['title', 'slug', 'last_updated']);
fs.writeFile(`${publicDir}/posts.json`, JSON.stringify(posts), (e) => {
if (e)
console.error(e);
});
return posts;
}

50
util/pretty-date.ts Normal file
View File

@@ -0,0 +1,50 @@
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
const suffixes = ['','st','nd','rd','th'];
export default function prettyDatePrint(date: Date) {
const now = new Date();
const diff = now.getTime() - date.getTime();
let tdiff = Math.floor(diff/1000);
if (tdiff < 60) {
return `${tdiff} seconds ago`;
}
tdiff = Math.floor(tdiff/60);
if (tdiff < 60) {
return `${tdiff} minute${tdiff === 1? '' : 's'} ago`;
}
tdiff = Math.floor(tdiff/60);
if (tdiff < 24) {
return `${tdiff} hour${tdiff === 1? '' : 's'} ago`;
}
if (tdiff < 48) {
return `Yesterday`;
}
const year = date.getFullYear();
const month = months[date.getMonth()];
const day = date.getDate();
let sfx;
if (day >= 1 && day <= 3)
sfx = suffixes[day];
else
sfx = suffixes[4];
return `${day}${sfx} ${month} ${year}`;
}

View File

@@ -31,16 +31,16 @@ export function toListItem(record: Record<string, any>): listItem | null {
children = schildren;
}
else {
children = [...lchildren, ... schildren.map((s: string): listItem => {
children = [...lchildren, ...schildren.map((s: string): listItem => {
return { title: s };
}) ];
})];
}
}
return {
title: record.title,
url: record.url,
children: children.length? children : undefined,
children: children.length ? children : undefined,
description: record.description,
};
}

4
util/slug.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
interface Post { slug?: string, rawslug?: string, content?: string, title?: string };
export function getAllPosts(filter: Array<any> = []): Post[];
export function getPost(rawslug: string, filter: Array<any> = []): Post;

View File

@@ -1,10 +1,10 @@
import fs from 'fs'
import matter from 'gray-matter';
import { join } from 'path';
const fs = require('fs');
const matter = require('gray-matter');
const { join } = require('path');
const postsDirectory = join(process.cwd(), 'posts');
export function getPost(rawslug: string, filter: Array<any> = []) {
function getPost(rawslug, filter = []) {
const slug = rawslug.replace(/\.md$/, '');
const path = join(postsDirectory, `${slug}.md`);
const file = fs.readFileSync(path, 'utf-8');
@@ -16,7 +16,7 @@ export function getPost(rawslug: string, filter: Array<any> = []) {
if (filter.length === 0)
return { ...data, content, slug, rawslug };
let post: { slug?: string, rawslug?: string, content?: string, title?: string } | any = {};
let post = {};
for (const [_, entry] of filter.entries()) {
if (entry === 'slug')
post[entry] = slug;
@@ -27,8 +27,6 @@ export function getPost(rawslug: string, filter: Array<any> = []) {
if (entry === 'content')
post[entry] = content;
if (typeof data[entry] !== 'undefined') {
post[entry] = data[entry]
}
@@ -36,12 +34,14 @@ export function getPost(rawslug: string, filter: Array<any> = []) {
return post;
}
export function getAllPosts(filter: Array<any> = []) {
function getAllPosts(filter = []) {
const files = fs.readdirSync(postsDirectory);
return files
.filter(c => !c.match(/^\./))
.map(file => {
return getPost(file, filter)
});
}
.filter(c => !c.match(/^\./))
.map(file => {
return getPost(file, filter)
});
}
module.exports = { getAllPosts, getPost };