public/shared/metadata.ts

40 lines
905 B
TypeScript
Raw Permalink Normal View History

2022-08-10 16:31:25 -04:00
import type { BlogParsedContent, StoryParsedContent } from "./types";
import readingTime from "reading-time";
2022-08-10 17:22:08 -04:00
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc.js";
dayjs.extend(utc);
2022-08-10 16:31:25 -04:00
function countWords(str: string) {
let words = 0;
for (const c of str) {
if (c === " " || c === "/") {
words++;
}
}
return words;
}
function search(obj: Record<string, any>, results: string[] = []) {
if (obj.value) {
results.push(obj.value);
}
if (obj.children) {
for (const el of obj.children) {
search(el, results);
}
}
return results;
}
export function calcReadingTime(doc: BlogParsedContent | StoryParsedContent) {
let body: string[] = search(doc.body);
return readingTime(body.join(" "));
}
2022-08-10 17:22:08 -04:00
export function getPrettyDate(doc: BlogParsedContent | StoryParsedContent) {
const date = dayjs(doc.date).utc();
return date.format("DD MMM YYYY");
}