diff options
Diffstat (limited to 'source/blog.js')
| -rw-r--r-- | source/blog.js | 76 |
1 files changed, 39 insertions, 37 deletions
diff --git a/source/blog.js b/source/blog.js index cc654dd..56f76a4 100644 --- a/source/blog.js +++ b/source/blog.js @@ -16,52 +16,54 @@ class Blog { async ReadBlogs() {return this.#LoadBlogs().then(output => {return output})} + // ! Check if file is a markdown file async #LoadBlogs() { return new Promise((res, rej) => { - fs.readdir('./blog/', (err, files) => { - if (err) { - logger.Error(`Error reading directory ${err}`) - rej(null) - } else { - this.blog = new Map() - files.forEach(file => { - try { + if (fs.existsSync('./blog/')) { + fs.readdir('./blog/', (err, files) => { + if (err) { + logger.Error(`${err.code}: Error reading directory`) + rej(`${err.code}: Error reading directory`) + } else { + this.blog = new Map() + files.forEach(file => { this.blog.set(file.replace('.md', ''), fs.readFileSync(path.resolve(`./blog/${file}`), 'utf-8', (err, data) => { if (err) { - logger.Error(err) + logger.Error(`${err.code}: Failed to read ${file}`) + rej(`${err.code}: Failed to read ${file}`) return } return data })) - } catch (err) { - logger.Error(err) - rej(null) - } - }) - this.blog.forEach((value, key) => { - const regex = /# (.*)\nDate: (.*)\n---(.*?)---/s - const match = value.match(regex) + }) + this.blog.forEach((value, key) => { + const regex = /# (.*)\nDate: (.*)\n---(.*?)---/s + const match = value.match(regex) - if (match) { - const [, title, date, description] = match - this.blog.set(key, { - title: title, - date: new Date(date), - short: description, - body: md.render(value) - }) - } else { - this.blog.set(key, { - title: undefined, - date: undefined, - short: undefined, - body: md.render(value) - }) - } - }) - res(this.#OrderByDate(this.blog)) - } - }) + if (match) { + const [, title, date, description] = match + this.blog.set(key, { + title: title, + date: new Date(date), + short: description, + body: md.render(value) + }) + } else { + this.blog.set(key, { + title: undefined, + date: undefined, + short: undefined, + body: md.render(value) + }) + } + }) + res(this.#OrderByDate(this.blog)) + } + }) + } else { + logger.Error('ERROR: blog directory does not exist') + rej('ERROR: blog directory does not exist') + } }) } |
