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()}