diff options
Diffstat (limited to 'source/blog.js')
| -rw-r--r-- | source/blog.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/source/blog.js b/source/blog.js new file mode 100644 index 0000000..2f8d12b --- /dev/null +++ b/source/blog.js @@ -0,0 +1,61 @@ +const fs = require('fs') +const path = require('path') +const markdownit = require('markdown-it') +const md = markdownit() +const logger = require('./logger')() + +// Parse markdown +// ! Organize by date +class Blog { + constructor() {} + + async ReadBlogs() {return this.#LoadBlogs().then(output => {return output})} + + async #LoadBlogs() { + return new Promise((res, rej) => { + fs.readdir('./blog/', (err, files) => { + if (err) { + logger.Error(`Error reading directory ${err}`) + rej(false) + } + + 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) + return + } + return data + })) + }) + this.blog.forEach((value, key) => { + this.regex = /# (.*)\nDate: (.*)\n---(.*?)---/s + this.match = value.match(this.regex) + + if (this.match) { + const [, title, date, description] = this.match + this.object = { + title: title, + date: new Date(date), + short: description, + body: md.render(value) + } + this.blog.set(key, this.object) + } else { + this.blog.set(key, { + title: undefined, + date: undefined, + short: undefined, + body: md.render(value) + }) + } + //this.blog.set(key, md.render(value)) + }) + res(this.blog) + }) + }) + } +} + +module.exports = () => {return new Blog()} |
