summaryrefslogtreecommitdiff
path: root/source/blog.js
diff options
context:
space:
mode:
authorMhykol <mchaeldonald62@pm.me>2024-06-03 05:26:09 -0400
committerMhykol <mchaeldonald62@pm.me>2024-06-03 05:26:09 -0400
commitcd760f0cf83cb00b9c4b372ed1b6ed5baf1b6272 (patch)
tree94cf37e9c3dcf017fc65a50b52045856a6100537 /source/blog.js
parent3135b036ce787d00228e627d9af58dfd9b5a1b08 (diff)
Added error handling for missing directories and examples
Diffstat (limited to 'source/blog.js')
-rw-r--r--source/blog.js76
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')
+ }
})
}