50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { promises as fs } from "fs";
|
|
import path from "path";
|
|
import dateFns from "date-fns";
|
|
import slugify from "@sindresorhus/slugify";
|
|
import * as util from "../util";
|
|
import * as metadata from "../metadata";
|
|
import { Page, PostMetadata } from "../metadata";
|
|
import * as markdown from "../markdown";
|
|
import layout from "../layout";
|
|
|
|
export default async function posts(): Promise<Page[]> {
|
|
const posts: Page[] = [];
|
|
|
|
const files = await fs.readdir("site/posts");
|
|
for (const f of files) {
|
|
let page = await metadata.get(path.join("site/posts", f));
|
|
|
|
if (!(<PostMetadata>page.metadata).permalink) {
|
|
let postMeta = <PostMetadata>page.metadata;
|
|
postMeta.date = dateFns.parse(postMeta.date);
|
|
postMeta.slug = postMeta.slug || slugify(postMeta.title);
|
|
postMeta.permalink = `/${postMeta.category}/${postMeta.date.getFullYear()}/${postMeta.slug}/`;
|
|
}
|
|
|
|
if (page.metadata.source && page.metadata.source!.endsWith(".md")) {
|
|
(<PostMetadata>page.metadata).readingTime = util.getReadingTime(page.text);
|
|
|
|
page.text = markdown.render(page.text);
|
|
}
|
|
|
|
if (!(<PostMetadata>page.metadata).excerpt) {
|
|
const parts = page.text.split("<!-- excerpt-end -->");
|
|
(<PostMetadata>page.metadata).excerpt = parts[0];
|
|
}
|
|
|
|
const renderedText = await layout(page.text, page.metadata, page.metadata.layout || "article.html.ejs");
|
|
|
|
let dest = page.metadata.permalink;
|
|
if (dest.endsWith("/")) {
|
|
dest += "index.html";
|
|
}
|
|
|
|
util.write(dest, renderedText);
|
|
|
|
posts.push(page);
|
|
}
|
|
|
|
return posts;
|
|
}
|