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