summaryrefslogtreecommitdiff
path: root/source/blog.js
blob: cc654dda4329740fa637596759f8a62e5f41730d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require('fs')
const path = require('path')
const markdownit = require('markdown-it')
const md = markdownit({
    html: true,
    breaks: true,
    typographer: {
        whitespace: 'all'
    },
})
const logger = require('./logger')()

// Parse markdown
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(null)
                } else {
                    this.blog = new Map()
                    files.forEach(file => {
                        try {
                            this.blog.set(file.replace('.md', ''), fs.readFileSync(path.resolve(`./blog/${file}`), 'utf-8', (err, data) => {
                                if (err) {
                                    logger.Error(err)
                                    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)

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

    #OrderByDate(x) {
        const entries =[]
        x.forEach((value, key) => {entries.push([key, value])})
        entries.sort((a, b) => {
            const dateA = b[1].date
            const dateB = a[1].date
            return dateA - dateB
        })
        return new Map(entries)
    }
}

module.exports = () => {return new Blog()}