about summary refs log tree commit diff
path: root/api/api.js
blob: 1675d7e3aaad2d19924249ac92e8103d00f71cbd (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
const fs = require('fs')
const frontmatter = require('front-matter')

const POSTS_PATH = './content/wpisy'

function getPostAttributes(filePath) {
  const fileContent = fs.readFileSync(filePath, 'utf-8')

  const post = frontmatter(fileContent)
  return post.attributes
}

function getPosts() {
  const routesArray = []
  try {
    const years = fs.readdirSync(`${POSTS_PATH}`)
    years.forEach(year => {
      const months = fs.readdirSync(`${POSTS_PATH}/${year}`)
      months.forEach(month => {
        const days = fs.readdirSync(`${POSTS_PATH}/${year}/${month}`)
        days.forEach(day => {
          const files = fs.readdirSync(`${POSTS_PATH}/${year}/${month}/${day}`)
          files.forEach(file => {
            const title = file.substr(0, file.lastIndexOf('.'))
            const route = `/kronika/${year}/${month}/${day}/${title}/`
            const fsRoute = `${POSTS_PATH}/${year}/${month}/${day}/${file}`

            const attributes = getPostAttributes(fsRoute)

            routesArray.push({
              year,
              month,
              day,
              title,
              attributes,
              file,
              fsRoute,
              route
            })
          })
        })
      })
    })
  } finally {
    return routesArray
  }
}

function createRoutesArray() {
  let posts = getPosts()
  return posts.map(post => post.route)
}

module.exports = {
  getPosts,
  createRoutesArray
}