Add directory-based posts

This commit is contained in:
Shadowfacts 2020-07-09 14:14:42 -04:00
parent 88bfad10db
commit 92542b595e
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 64 additions and 20 deletions

View File

@ -8,13 +8,12 @@ import { Page, PostMetadata } from "../metadata";
import * as markdown from "../markdown";
import layout from "../layout";
export default async function posts(): Promise<Page[]> {
const posts: Page[] = [];
async function generatePostFromFile(postPath: string, additionalMetadata: object = {}): Promise<Page> {
const page = await metadata.get(postPath);
const files = await fs.readdir("site/posts");
for (const f of files) {
const postPath = path.join("site/posts", f);
let page = await metadata.get(postPath);
for (const k in additionalMetadata) {
if (additionalMetadata.hasOwnProperty(k)) page.metadata[k] = additionalMetadata[k];
}
if (!(<PostMetadata>page.metadata).permalink) {
let postMeta = <PostMetadata>page.metadata;
@ -23,8 +22,6 @@ export default async function posts(): Promise<Page[]> {
postMeta.permalink = `/${postMeta.date.getFullYear()}/${postMeta.slug}/`;
}
page.text = util.render(page.text, { metadata: page.metadata }, postPath);
if (page.metadata.source && page.metadata.source!.endsWith(".md")) {
(<PostMetadata>page.metadata).wordCount = util.getWordCount(page.text);
(<PostMetadata>page.metadata).readingTime = util.getReadingTime((<PostMetadata>page.metadata).wordCount);
@ -32,11 +29,54 @@ export default async function posts(): Promise<Page[]> {
page.text = markdown.render(page.text);
}
page.text = util.render(page.text, { metadata: page.metadata }, postPath);
// todo: handle word count if not .md
if (!(<PostMetadata>page.metadata).excerpt) {
const parts = page.text.split("<!-- excerpt-end -->");
(<PostMetadata>page.metadata).excerpt = parts[0];
}
return page;
}
async function generatePostFromDirectory(dir: string): Promise<Page> {
const files = await fs.readdir(dir);
let indexPath: string;
for (const f of files) {
if (f.split(".")[0] === "index") {
indexPath = path.join(dir, f);
break;
}
}
if (!indexPath) {
throw new Error(`Unable to find index file for post: ${dir}`);
}
return await generatePostFromFile(indexPath, {
sourceDir: dir
});
}
export default async function posts(): Promise<Page[]> {
const posts: Page[] = [];
const files = await fs.readdir("site/posts");
for (const f of files) {
const postPath = path.join("site/posts", f);
const stats = await fs.stat(postPath);
let page: Page;
if (stats.isDirectory()) {
page = await generatePostFromDirectory(postPath);
} else {
page = await generatePostFromFile(postPath);
}
const renderedText = await layout(page.text, page.metadata, page.metadata.layout || "article.html.ejs");
let dest = page.metadata.permalink;

View File

@ -8,6 +8,7 @@ export interface Page {
export interface Metadata {
permalink: string;
source?: string;
sourceDir?: string;
layout?: string;
oldPermalink?: string;
}

View File

@ -77,6 +77,7 @@ export function video(metadata: Metadata, name: string, attributes: object): str
}
export function render(template: string, data: any, filename?: string): string {
data.require = require;
data.fancyLink = fancyLink;
data.formatDate = formatDate;
data.video = video;

View File

@ -143,4 +143,6 @@ func contextMenuInteraction(_ interaction: UIContextMenuInteraction, previewForD
Now, when dismissing the preview, it animates back into the link text where it originally came from:
<div>
<%- video(metadata, "dismiss", {style: "width: 50%; margin: 0 auto; display: block;", title: "Screen recording of a link being previewed and dismissed with the link text animating back to its starting position upon dismissal."}) %>
</div>