]>
git.r.bdr.sh - rbdr/blog/blob - lib/blog.js
541d6b35da767e6771b7a48dbac9e079253b70c2
3 const { access
, mkdir
, readdir
, readFile
, rmdir
, writeFile
} = require('fs/promises');
4 const { ncp
} = require('ncp');
5 const { join
} = require('path');
6 const Marked
= require('marked');
7 const { debuglog
, promisify
} = require('util');
9 const StaticGenerator
= require('./generators/static');
10 const HTMLGenerator
= require('./generators/html');
11 const RSSGenerator
= require('./generators/rss');
12 const TXTGenerator
= require('./generators/txt');
16 // Promisified functions
19 debuglog: debuglog('blog'),
23 kFileNotFoundError: 'ENOENT',
24 kMarkdownRe: /\.md$/i,
25 kMetadataFilename: 'metadata.json',
30 markdownNotFound: 'Markdown file was not found in blog directory. Please update.'
35 * The Blog class is the blog generator, it's in charge of adding and
36 * updating posts, and handling the publishing.
39 * @param {Blog.tConfiguration} config the initialization options to
42 module
.exports
= class Blog
{
46 Object
.assign(this, config
);
50 * Shifts the blog posts, adds the passed path to slot 0, and
55 * @param {string} postLocation the path to the directory containing
57 * @return {Promise<undefined>} empty promise, returns no value
60 async
add(postLocation
) {
62 await
this._ensurePostsDirectoryExists();
64 await
this.update(postLocation
);
68 * Adds the passed path to slot 0, and generates files.
72 * @param {string} postLocation the path to the directory containing
74 * @return {Promise<undefined>} empty promise, returns no value
77 async
update(postLocation
) {
79 const metadata
= await
this._getMetadata();
80 await
this._ensurePostsDirectoryExists();
81 await
this._copyPost(postLocation
);
82 await
this._writeMetadata(metadata
);
84 await
this._generate();
88 * Publishes the files to a static host.
92 * @return {Promise<undefined>} empty promise, returns no value
97 console
.error('Publishing not yet implemented');
98 return Promise
.resolve();
101 // Parses markdown for each page, copies assets and generates index.
105 internals
.debuglog('Generating output');
107 const posts
= await
this._readPosts(this.postsDirectory
);
109 await
StaticGenerator(this.postsDirectory
, this.staticDirectory
, posts
);
110 await
HTMLGenerator(this.templatesDirectory
, this.staticDirectory
, posts
);
111 await
RSSGenerator(this.templatesDirectory
, this.staticDirectory
, posts
);
112 await
TXTGenerator(this.templatesDirectory
, this.staticDirectory
, posts
);
115 // Reads the posts into an array
117 async
_readPosts(source
) {
119 internals
.debuglog('Reading posts');
122 for (let i
= 0; i
< this.maxPosts
; ++i
) {
123 const postSourcePath
= join(source
, `${i}`);
125 internals
.debuglog(`Reading ${postSourcePath} into posts array`);
128 await
access(postSourcePath
);
130 const metadata
= await
this._getMetadata(i
);
132 const postContentPath
= await
this._findBlogContent(postSourcePath
);
133 internals
.debuglog(`Reading ${postContentPath}`);
134 const postContent
= await
readFile(postContentPath
, { encoding: 'utf8' });
136 internals
.debuglog('Parsing markdown');
139 html: Marked(postContent
),
144 if (error
.code
=== internals
.kFileNotFoundError
) {
145 internals
.debuglog(`Skipping ${i}`);
156 // Shift the posts, delete any remainder.
161 for (let i
= this.maxPosts
- 1; i
>= 0; --i
) {
162 const targetPath
= join(this.postsDirectory
, `${i}`);
163 const sourcePath
= join(this.postsDirectory
, `${i - 1}`);
166 internals
.debuglog(`Removing ${targetPath}`);
167 await
rmdir(targetPath
, { recursive: true });
169 await
access(sourcePath
); // check the source path
171 internals
.debuglog(`Shifting blog post ${sourcePath} to ${targetPath}`);
172 await internals
.ncp(sourcePath
, targetPath
);
175 if (error
.code
=== internals
.kFileNotFoundError
) {
176 internals
.debuglog(`Skipping ${sourcePath}: Does not exist.`);
185 // Attempts to read existing metadata. Otherwise generates new set.
187 async
_getMetadata(index
= 0) {
189 const metadataTarget
= join(this.postsDirectory
, String(index
), internals
.kMetadataFilename
);
192 internals
.debuglog(`Looking for metadata at ${metadataTarget}`);
193 return JSON
.parse(await
readFile(metadataTarget
, { encoding: 'utf8' }));
196 internals
.debuglog(`Metadata not found or unreadable. Generating new set.`);
197 const createdOn
= Date
.now();
199 id: String(createdOn
),
207 // Writes metadata. Assumes post 0 since it only gets written
210 async
_writeMetadata(metadata
) {
212 const metadataTarget
= join(this.postsDirectory
, '0', internals
.kMetadataFilename
);
213 internals
.debuglog(`Writing ${metadataTarget}`);
214 await
writeFile(metadataTarget
, JSON
.stringify(metadata
, null, 2));
217 // Copies a post directory to the latest slot.
219 async
_copyPost(postLocation
) {
221 const targetPath
= join(this.postsDirectory
, '0');
223 internals
.debuglog(`Removing ${targetPath}`);
224 await
rmdir(targetPath
, { recursive: true });
226 internals
.debuglog(`Adding ${postLocation} to ${targetPath}`);
227 await internals
.ncp(postLocation
, targetPath
);
230 // Ensures the posts directory exists.
232 async
_ensurePostsDirectoryExists() {
234 internals
.debuglog(`Checking if ${this.postsDirectory} exists.`);
236 await
access(this.postsDirectory
);
239 if (error
.code
=== internals
.kFileNotFoundError
) {
240 internals
.debuglog('Creating posts directory');
241 await
mkdir(this.postsDirectory
);
249 // Looks for a `.md` file in the blog directory, and returns the path
251 async
_findBlogContent(directory
) {
253 const entries
= await
readdir(directory
);
255 const markdownEntries
= entries
256 .filter((entry
) => internals
.kMarkdownRe
.test(entry
))
257 .map((entry
) => join(directory
, entry
));
259 if (markdownEntries
.length
> 0) {
260 internals
.debuglog(`Found markdown file: ${markdownEntries[0]}`);
261 return markdownEntries
[0];
264 throw new Error(internals
.strings
.markdownNotFound
);