]>
git.r.bdr.sh - rbdr/blog/blob - lib/blog.js
98efabdf1fc618636390ef5874704aa0f9028bc7
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');
15 // Promisified functions
18 debuglog: debuglog('blog'),
22 kFileNotFoundError: 'ENOENT',
23 kMarkdownRe: /\.md$/i,
24 kMetadataFilename: 'metadata.json',
29 markdownNotFound: 'Markdown file was not found in blog directory. Please update.'
34 * The Blog class is the blog generator, it's in charge of adding and
35 * updating posts, and handling the publishing.
38 * @param {Blog.tConfiguration} config the initialization options to
41 module
.exports
= class Blog
{
45 Object
.assign(this, config
);
49 * Shifts the blog posts, adds the passed path to slot 0, and
54 * @param {string} postLocation the path to the directory containing
56 * @return {Promise<undefined>} empty promise, returns no value
59 async
add(postLocation
) {
61 await
this._ensurePostsDirectoryExists();
63 await
this.update(postLocation
);
67 * Adds the passed path to slot 0, and generates files.
71 * @param {string} postLocation the path to the directory containing
73 * @return {Promise<undefined>} empty promise, returns no value
76 async
update(postLocation
) {
78 const metadata
= await
this._getMetadata();
79 await
this._ensurePostsDirectoryExists();
80 await
this._copyPost(postLocation
);
81 await
this._writeMetadata(metadata
);
83 await
this._generate();
87 * Publishes the files to a static host.
91 * @return {Promise<undefined>} empty promise, returns no value
96 console
.error('Publishing not yet implemented');
97 return Promise
.resolve();
100 // Parses markdown for each page, copies assets and generates index.
104 internals
.debuglog('Generating output');
106 const posts
= await
this._readPosts(this.postsDirectory
);
108 await
StaticGenerator(this.postsDirectory
, this.staticDirectory
, posts
);
109 await
HTMLGenerator(this.templatesDirectory
, this.staticDirectory
, posts
);
110 await
RSSGenerator(this.templatesDirectory
, this.staticDirectory
, posts
);
113 // Reads the posts into an array
115 async
_readPosts(source
) {
117 internals
.debuglog('Reading posts');
120 for (let i
= 0; i
< this.maxPosts
; ++i
) {
121 const postSourcePath
= join(source
, `${i}`);
123 internals
.debuglog(`Reading ${postSourcePath} into posts array`);
126 await
access(postSourcePath
);
128 const metadata
= await
this._getMetadata(i
);
130 const postContentPath
= await
this._findBlogContent(postSourcePath
);
131 internals
.debuglog(`Reading ${postContentPath}`);
132 const postContent
= await
readFile(postContentPath
, { encoding: 'utf8' });
134 internals
.debuglog('Parsing markdown');
137 html: Marked(postContent
)
141 if (error
.code
=== internals
.kFileNotFoundError
) {
142 internals
.debuglog(`Skipping ${i}`);
153 // Shift the posts, delete any remainder.
158 for (let i
= this.maxPosts
- 1; i
>= 0; --i
) {
159 const targetPath
= join(this.postsDirectory
, `${i}`);
160 const sourcePath
= join(this.postsDirectory
, `${i - 1}`);
163 internals
.debuglog(`Removing ${targetPath}`);
164 await
rmdir(targetPath
, { recursive: true });
166 await
access(sourcePath
); // check the source path
168 internals
.debuglog(`Shifting blog post ${sourcePath} to ${targetPath}`);
169 await internals
.ncp(sourcePath
, targetPath
);
172 if (error
.code
=== internals
.kFileNotFoundError
) {
173 internals
.debuglog(`Skipping ${sourcePath}: Does not exist.`);
182 // Attempts to read existing metadata. Otherwise generates new set.
184 async
_getMetadata(index
= 0) {
186 const metadataTarget
= join(this.postsDirectory
, String(index
), internals
.kMetadataFilename
);
189 internals
.debuglog(`Looking for metadata at ${metadataTarget}`);
190 return JSON
.parse(await
readFile(metadataTarget
, { encoding: 'utf8' }));
193 internals
.debuglog(`Metadata not found or unreadable. Generating new set.`);
194 const createdOn
= Date
.now();
196 id: String(createdOn
),
204 // Writes metadata. Assumes post 0 since it only gets written
207 async
_writeMetadata(metadata
) {
209 const metadataTarget
= join(this.postsDirectory
, '0', internals
.kMetadataFilename
);
210 internals
.debuglog(`Writing ${metadataTarget}`);
211 await
writeFile(metadataTarget
, JSON
.stringify(metadata
, null, 2));
214 // Copies a post directory to the latest slot.
216 async
_copyPost(postLocation
) {
218 const targetPath
= join(this.postsDirectory
, '0');
220 internals
.debuglog(`Removing ${targetPath}`);
221 await
rmdir(targetPath
, { recursive: true });
223 internals
.debuglog(`Adding ${postLocation} to ${targetPath}`);
224 await internals
.ncp(postLocation
, targetPath
);
227 // Ensures the posts directory exists.
229 async
_ensurePostsDirectoryExists() {
231 internals
.debuglog(`Checking if ${this.postsDirectory} exists.`);
233 await
access(this.postsDirectory
);
236 if (error
.code
=== internals
.kFileNotFoundError
) {
237 internals
.debuglog('Creating posts directory');
238 await
mkdir(this.postsDirectory
);
246 // Looks for a `.md` file in the blog directory, and returns the path
248 async
_findBlogContent(directory
) {
250 const entries
= await
readdir(directory
);
252 const markdownEntries
= entries
253 .filter((entry
) => internals
.kMarkdownRe
.test(entry
))
254 .map((entry
) => join(directory
, entry
));
256 if (markdownEntries
.length
> 0) {
257 internals
.debuglog(`Found markdown file: ${markdownEntries[0]}`);
258 return markdownEntries
[0];
261 throw new Error(internals
.strings
.markdownNotFound
);