From 92542b595ed0ea0e21e341660a6c9b92e94b7298 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 9 Jul 2020 14:14:42 -0400 Subject: [PATCH] Add directory-based posts --- lib/generate/posts.ts | 78 ++++++++++++++----- lib/metadata.ts | 1 + lib/util.ts | 1 + ...07-03-uipreviewparameters-textlinerects.md | 4 +- 4 files changed, 64 insertions(+), 20 deletions(-) diff --git a/lib/generate/posts.ts b/lib/generate/posts.ts index 0566061..3923766 100644 --- a/lib/generate/posts.ts +++ b/lib/generate/posts.ts @@ -8,33 +8,73 @@ import { Page, PostMetadata } from "../metadata"; import * as markdown from "../markdown"; import layout from "../layout"; +async function generatePostFromFile(postPath: string, additionalMetadata: object = {}): Promise { + const page = await metadata.get(postPath); + + for (const k in additionalMetadata) { + if (additionalMetadata.hasOwnProperty(k)) page.metadata[k] = additionalMetadata[k]; + } + + if (!(page.metadata).permalink) { + let postMeta = page.metadata; + postMeta.date = dateFns.parse(postMeta.date); + postMeta.slug = postMeta.slug || slugify(postMeta.title); + postMeta.permalink = `/${postMeta.date.getFullYear()}/${postMeta.slug}/`; + } + + if (page.metadata.source && page.metadata.source!.endsWith(".md")) { + (page.metadata).wordCount = util.getWordCount(page.text); + (page.metadata).readingTime = util.getReadingTime((page.metadata).wordCount); + + page.text = markdown.render(page.text); + } + + page.text = util.render(page.text, { metadata: page.metadata }, postPath); + + // todo: handle word count if not .md + + if (!(page.metadata).excerpt) { + const parts = page.text.split(""); + (page.metadata).excerpt = parts[0]; + } + + return page; +} + +async function generatePostFromDirectory(dir: string): Promise { + 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 { const posts: Page[] = []; 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); + const stats = await fs.stat(postPath); - if (!(page.metadata).permalink) { - let postMeta = page.metadata; - postMeta.date = dateFns.parse(postMeta.date); - postMeta.slug = postMeta.slug || slugify(postMeta.title); - postMeta.permalink = `/${postMeta.date.getFullYear()}/${postMeta.slug}/`; - } + let page: Page; - page.text = util.render(page.text, { metadata: page.metadata }, postPath); - - if (page.metadata.source && page.metadata.source!.endsWith(".md")) { - (page.metadata).wordCount = util.getWordCount(page.text); - (page.metadata).readingTime = util.getReadingTime((page.metadata).wordCount); - - page.text = markdown.render(page.text); - } - - if (!(page.metadata).excerpt) { - const parts = page.text.split(""); - (page.metadata).excerpt = parts[0]; + 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"); diff --git a/lib/metadata.ts b/lib/metadata.ts index bd7ea7c..4dc7050 100644 --- a/lib/metadata.ts +++ b/lib/metadata.ts @@ -8,6 +8,7 @@ export interface Page { export interface Metadata { permalink: string; source?: string; + sourceDir?: string; layout?: string; oldPermalink?: string; } diff --git a/lib/util.ts b/lib/util.ts index bcfdf78..acd15a8 100644 --- a/lib/util.ts +++ b/lib/util.ts @@ -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; diff --git a/site/posts/2020-07-03-uipreviewparameters-textlinerects.md b/site/posts/2020-07-03-uipreviewparameters-textlinerects.md index 7515a5a..8663743 100644 --- a/site/posts/2020-07-03-uipreviewparameters-textlinerects.md +++ b/site/posts/2020-07-03-uipreviewparameters-textlinerects.md @@ -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: -<%- 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."}) %> +
+ <%- 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."}) %> +
\ No newline at end of file