]> git.r.bdr.sh - rbdr/blog/blobdiff - lib/blog.js
Add TXT support
[rbdr/blog] / lib / blog.js
index 6b3397fd7d2f008ac1de39cb4552583fd0723124..541d6b35da767e6771b7a48dbac9e079253b70c2 100644 (file)
@@ -1,25 +1,25 @@
 'use strict';
 
-const { exec } = require('child_process');
 const { access, mkdir, readdir, readFile, rmdir, writeFile } = require('fs/promises');
-const { template } = require('dot');
 const { ncp } = require('ncp');
 const { join } = require('path');
 const Marked = require('marked');
 const { debuglog, promisify } = require('util');
 
+const StaticGenerator = require('./generators/static');
+const HTMLGenerator = require('./generators/html');
+const RSSGenerator = require('./generators/rss');
+const TXTGenerator = require('./generators/txt');
+
 const internals = {
 
   // Promisified functions
   ncp: promisify(ncp),
 
-  exec: promisify(exec),
   debuglog: debuglog('blog'),
 
   // constants
 
-  kAssetsDirectoryName: 'assets',
-  kIndexName: 'index.html',
   kFileNotFoundError: 'ENOENT',
   kMarkdownRe: /\.md$/i,
   kMetadataFilename: 'metadata.json',
@@ -36,7 +36,7 @@ const internals = {
  * updating posts, and handling the publishing.
  *
  * @class Blog
- * @param {Potluck.tConfiguration} config the initialization options to
+ * @param {Blog.tConfiguration} config the initialization options to
  * extend the instance
  */
 module.exports = class Blog {
@@ -102,33 +102,42 @@ module.exports = class Blog {
 
   async _generate() {
 
-    const assetsTarget = join(this.staticDirectory, internals.kAssetsDirectoryName);
-    const indexTarget = join(this.staticDirectory, internals.kIndexName);
-    const indexLocation = join(this.templatesDirectory, internals.kIndexName);
-    const posts = [];
+    internals.debuglog('Generating output');
+
+    const posts = await this._readPosts(this.postsDirectory);
+
+    await StaticGenerator(this.postsDirectory, this.staticDirectory, posts);
+    await HTMLGenerator(this.templatesDirectory, this.staticDirectory, posts);
+    await RSSGenerator(this.templatesDirectory, this.staticDirectory, posts);
+    await TXTGenerator(this.templatesDirectory, this.staticDirectory, posts);
+  }
+
+  // Reads the posts into an array
 
-    internals.debuglog(`Removing ${assetsTarget}`);
-    await rmdir(assetsTarget, { recursive: true });
+  async _readPosts(source) {
+
+    internals.debuglog('Reading posts');
+    const posts = [];
 
     for (let i = 0; i < this.maxPosts; ++i) {
-      const sourcePath = join(this.postsDirectory, `${i}`);
+      const postSourcePath = join(source, `${i}`);
 
-      try {
-        await access(this.postsDirectory);
+      internals.debuglog(`Reading ${postSourcePath} into posts array`);
 
-        const assetsSource = join(sourcePath, internals.kAssetsDirectoryName);
-        const postContentPath = await this._findBlogContent(sourcePath);
+      try {
+        await access(postSourcePath);
 
-        internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`);
-        await internals.ncp(assetsSource, assetsTarget);
+        const metadata = await this._getMetadata(i);
 
+        const postContentPath = await this._findBlogContent(postSourcePath);
         internals.debuglog(`Reading ${postContentPath}`);
         const postContent = await readFile(postContentPath, { encoding: 'utf8' });
 
         internals.debuglog('Parsing markdown');
         posts.push({
+          ...metadata,
           html: Marked(postContent),
-          id: i + 1
+          raw: postContent
         });
       }
       catch (error) {
@@ -141,12 +150,7 @@ module.exports = class Blog {
       }
     }
 
-    internals.debuglog(`Reading ${indexLocation}`);
-    const indexTemplate = await readFile(indexLocation, { encoding: 'utf8' });
-
-    internals.debuglog('Generating HTML');
-    const indexHtml = template(indexTemplate)({ posts });
-    await writeFile(indexTarget, indexHtml);
+    return posts;
   }
 
   // Shift the posts, delete any remainder.
@@ -180,13 +184,13 @@ module.exports = class Blog {
 
   // Attempts to read existing metadata. Otherwise generates new set.
 
-  async _getMetadata() {
+  async _getMetadata(index = 0) {
 
-    const metadataTarget = join(this.postsDirectory, '0', internals.kMetadataFilename);
+    const metadataTarget = join(this.postsDirectory, String(index), internals.kMetadataFilename);
 
     try {
       internals.debuglog(`Looking for metadata at ${metadataTarget}`);
-      return await readFile(metadataTarget);
+      return JSON.parse(await readFile(metadataTarget, { encoding: 'utf8' }));
     }
     catch (e) {
       internals.debuglog(`Metadata not found or unreadable. Generating new set.`);
@@ -196,7 +200,7 @@ module.exports = class Blog {
         createdOn
       };
 
-      return JSON.stringify(metadata, null, 2);
+      return metadata;
     }
   }
 
@@ -207,7 +211,7 @@ module.exports = class Blog {
 
     const metadataTarget = join(this.postsDirectory, '0', internals.kMetadataFilename);
     internals.debuglog(`Writing ${metadataTarget}`);
-    await writeFile(metadataTarget, metadata);
+    await writeFile(metadataTarget, JSON.stringify(metadata, null, 2));
   }
 
   // Copies a post directory to the latest slot.