]>
Commit | Line | Data |
---|---|---|
cf630290 BB |
1 | 'use strict'; |
2 | ||
d92ac8cc | 3 | const { access, mkdir, readdir, readFile, rmdir, writeFile } = require('fs/promises'); |
d92ac8cc BB |
4 | const { ncp } = require('ncp'); |
5 | const { join } = require('path'); | |
6 | const Marked = require('marked'); | |
7 | const { debuglog, promisify } = require('util'); | |
cf630290 | 8 | |
67fdfa7c BB |
9 | const StaticGenerator = require('./generators/static'); |
10 | const HTMLGenerator = require('./generators/html'); | |
11 | const RSSGenerator = require('./generators/rss'); | |
12 | ||
cf630290 BB |
13 | const internals = { |
14 | ||
15 | // Promisified functions | |
d92ac8cc | 16 | ncp: promisify(ncp), |
cf630290 | 17 | |
d92ac8cc | 18 | debuglog: debuglog('blog'), |
cf630290 BB |
19 | |
20 | // constants | |
21 | ||
cf630290 BB |
22 | kFileNotFoundError: 'ENOENT', |
23 | kMarkdownRe: /\.md$/i, | |
6f72ad0f | 24 | kMetadataFilename: 'metadata.json', |
cf630290 BB |
25 | |
26 | // Strings | |
27 | ||
28 | strings: { | |
29 | markdownNotFound: 'Markdown file was not found in blog directory. Please update.' | |
30 | } | |
31 | }; | |
32 | ||
33 | /** | |
34 | * The Blog class is the blog generator, it's in charge of adding and | |
35 | * updating posts, and handling the publishing. | |
36 | * | |
37 | * @class Blog | |
67fdfa7c | 38 | * @param {Blog.tConfiguration} config the initialization options to |
cf630290 BB |
39 | * extend the instance |
40 | */ | |
41 | module.exports = class Blog { | |
42 | ||
43 | constructor(config) { | |
44 | ||
45 | Object.assign(this, config); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Shifts the blog posts, adds the passed path to slot 0, and | |
50 | * generates files. | |
51 | * | |
52 | * @function add | |
53 | * @memberof Blog | |
54 | * @param {string} postLocation the path to the directory containing | |
55 | * the post structure | |
56 | * @return {Promise<undefined>} empty promise, returns no value | |
57 | * @instance | |
58 | */ | |
59 | async add(postLocation) { | |
60 | ||
6f72ad0f | 61 | await this._ensurePostsDirectoryExists(); |
cf630290 BB |
62 | await this._shift(); |
63 | await this.update(postLocation); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Adds the passed path to slot 0, and generates files. | |
68 | * | |
69 | * @function update | |
70 | * @memberof Blog | |
71 | * @param {string} postLocation the path to the directory containing | |
72 | * the post structure | |
73 | * @return {Promise<undefined>} empty promise, returns no value | |
74 | * @instance | |
75 | */ | |
76 | async update(postLocation) { | |
77 | ||
6f72ad0f BB |
78 | const metadata = await this._getMetadata(); |
79 | await this._ensurePostsDirectoryExists(); | |
cf630290 | 80 | await this._copyPost(postLocation); |
6f72ad0f BB |
81 | await this._writeMetadata(metadata); |
82 | ||
cf630290 BB |
83 | await this._generate(); |
84 | } | |
85 | ||
86 | /** | |
87 | * Publishes the files to a static host. | |
88 | * | |
89 | * @function publish | |
90 | * @memberof Blog | |
91 | * @return {Promise<undefined>} empty promise, returns no value | |
92 | * @instance | |
93 | */ | |
d92ac8cc | 94 | publish() { |
cf630290 BB |
95 | |
96 | console.error('Publishing not yet implemented'); | |
d92ac8cc | 97 | return Promise.resolve(); |
cf630290 BB |
98 | } |
99 | ||
100 | // Parses markdown for each page, copies assets and generates index. | |
101 | ||
102 | async _generate() { | |
103 | ||
67fdfa7c BB |
104 | internals.debuglog('Generating output'); |
105 | ||
106 | const posts = await this._readPosts(this.postsDirectory); | |
107 | ||
108 | await StaticGenerator(this.postsDirectory, this.staticDirectory, posts); | |
109 | await HTMLGenerator(this.templatesDirectory, this.staticDirectory, posts); | |
110 | await RSSGenerator(this.templatesDirectory, this.staticDirectory, posts); | |
111 | } | |
112 | ||
113 | // Reads the posts into an array | |
cf630290 | 114 | |
67fdfa7c BB |
115 | async _readPosts(source) { |
116 | ||
117 | internals.debuglog('Reading posts'); | |
118 | const posts = []; | |
cf630290 BB |
119 | |
120 | for (let i = 0; i < this.maxPosts; ++i) { | |
67fdfa7c | 121 | const postSourcePath = join(source, `${i}`); |
cf630290 | 122 | |
67fdfa7c | 123 | internals.debuglog(`Reading ${postSourcePath} into posts array`); |
cf630290 | 124 | |
67fdfa7c BB |
125 | try { |
126 | await access(postSourcePath); | |
cf630290 | 127 | |
67fdfa7c | 128 | const metadata = await this._getMetadata(i); |
eccb3cc4 | 129 | |
67fdfa7c | 130 | const postContentPath = await this._findBlogContent(postSourcePath); |
eccb3cc4 | 131 | internals.debuglog(`Reading ${postContentPath}`); |
d92ac8cc | 132 | const postContent = await readFile(postContentPath, { encoding: 'utf8' }); |
eccb3cc4 BB |
133 | |
134 | internals.debuglog('Parsing markdown'); | |
135 | posts.push({ | |
67fdfa7c BB |
136 | ...metadata, |
137 | html: Marked(postContent) | |
eccb3cc4 BB |
138 | }); |
139 | } | |
140 | catch (error) { | |
141 | if (error.code === internals.kFileNotFoundError) { | |
142 | internals.debuglog(`Skipping ${i}`); | |
143 | continue; | |
144 | } | |
145 | ||
146 | throw error; | |
147 | } | |
cf630290 BB |
148 | } |
149 | ||
67fdfa7c | 150 | return posts; |
cf630290 BB |
151 | } |
152 | ||
153 | // Shift the posts, delete any remainder. | |
154 | ||
155 | async _shift() { | |
156 | ||
cf630290 | 157 | |
6f72ad0f | 158 | for (let i = this.maxPosts - 1; i >= 0; --i) { |
d92ac8cc BB |
159 | const targetPath = join(this.postsDirectory, `${i}`); |
160 | const sourcePath = join(this.postsDirectory, `${i - 1}`); | |
cf630290 BB |
161 | |
162 | try { | |
cf630290 | 163 | internals.debuglog(`Removing ${targetPath}`); |
d92ac8cc | 164 | await rmdir(targetPath, { recursive: true }); |
cf630290 | 165 | |
6f72ad0f BB |
166 | await access(sourcePath); // check the source path |
167 | ||
cf630290 BB |
168 | internals.debuglog(`Shifting blog post ${sourcePath} to ${targetPath}`); |
169 | await internals.ncp(sourcePath, targetPath); | |
170 | } | |
171 | catch (error) { | |
172 | if (error.code === internals.kFileNotFoundError) { | |
173 | internals.debuglog(`Skipping ${sourcePath}: Does not exist.`); | |
174 | continue; | |
175 | } | |
176 | ||
177 | throw error; | |
178 | } | |
179 | } | |
180 | } | |
181 | ||
6f72ad0f BB |
182 | // Attempts to read existing metadata. Otherwise generates new set. |
183 | ||
67fdfa7c | 184 | async _getMetadata(index = 0) { |
6f72ad0f | 185 | |
67fdfa7c | 186 | const metadataTarget = join(this.postsDirectory, String(index), internals.kMetadataFilename); |
6f72ad0f BB |
187 | |
188 | try { | |
189 | internals.debuglog(`Looking for metadata at ${metadataTarget}`); | |
67fdfa7c | 190 | return JSON.parse(await readFile(metadataTarget, { encoding: 'utf8' })); |
6f72ad0f BB |
191 | } |
192 | catch (e) { | |
193 | internals.debuglog(`Metadata not found or unreadable. Generating new set.`); | |
194 | const createdOn = Date.now(); | |
195 | const metadata = { | |
196 | id: String(createdOn), | |
197 | createdOn | |
198 | }; | |
199 | ||
67fdfa7c | 200 | return metadata; |
6f72ad0f BB |
201 | } |
202 | } | |
203 | ||
204 | // Writes metadata. Assumes post 0 since it only gets written | |
205 | // on create | |
206 | ||
207 | async _writeMetadata(metadata) { | |
208 | ||
209 | const metadataTarget = join(this.postsDirectory, '0', internals.kMetadataFilename); | |
210 | internals.debuglog(`Writing ${metadataTarget}`); | |
67fdfa7c | 211 | await writeFile(metadataTarget, JSON.stringify(metadata, null, 2)); |
6f72ad0f BB |
212 | } |
213 | ||
cf630290 BB |
214 | // Copies a post directory to the latest slot. |
215 | ||
216 | async _copyPost(postLocation) { | |
217 | ||
d92ac8cc | 218 | const targetPath = join(this.postsDirectory, '0'); |
cf630290 BB |
219 | |
220 | internals.debuglog(`Removing ${targetPath}`); | |
d92ac8cc | 221 | await rmdir(targetPath, { recursive: true }); |
cf630290 BB |
222 | |
223 | internals.debuglog(`Adding ${postLocation} to ${targetPath}`); | |
224 | await internals.ncp(postLocation, targetPath); | |
225 | } | |
226 | ||
227 | // Ensures the posts directory exists. | |
228 | ||
229 | async _ensurePostsDirectoryExists() { | |
230 | ||
231 | internals.debuglog(`Checking if ${this.postsDirectory} exists.`); | |
232 | try { | |
d92ac8cc | 233 | await access(this.postsDirectory); |
cf630290 BB |
234 | } |
235 | catch (error) { | |
236 | if (error.code === internals.kFileNotFoundError) { | |
237 | internals.debuglog('Creating posts directory'); | |
d92ac8cc | 238 | await mkdir(this.postsDirectory); |
cf630290 BB |
239 | return; |
240 | } | |
241 | ||
242 | throw error; | |
243 | } | |
244 | } | |
245 | ||
246 | // Looks for a `.md` file in the blog directory, and returns the path | |
247 | ||
248 | async _findBlogContent(directory) { | |
249 | ||
d92ac8cc | 250 | const entries = await readdir(directory); |
cf630290 BB |
251 | |
252 | const markdownEntries = entries | |
253 | .filter((entry) => internals.kMarkdownRe.test(entry)) | |
d92ac8cc | 254 | .map((entry) => join(directory, entry)); |
cf630290 BB |
255 | |
256 | if (markdownEntries.length > 0) { | |
257 | internals.debuglog(`Found markdown file: ${markdownEntries[0]}`); | |
258 | return markdownEntries[0]; | |
259 | } | |
260 | ||
261 | throw new Error(internals.strings.markdownNotFound); | |
262 | } | |
263 | }; |